1a7dea167SDimitry Andric //===-- SemaConcept.cpp - Semantic Analysis for Constraints and Concepts --===// 2a7dea167SDimitry Andric // 3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a7dea167SDimitry Andric // 7a7dea167SDimitry Andric //===----------------------------------------------------------------------===// 8a7dea167SDimitry Andric // 9a7dea167SDimitry Andric // This file implements semantic analysis for C++ constraints and concepts. 10a7dea167SDimitry Andric // 11a7dea167SDimitry Andric //===----------------------------------------------------------------------===// 12a7dea167SDimitry Andric 13480093f4SDimitry Andric #include "clang/Sema/SemaConcept.h" 14*06c3fb27SDimitry Andric #include "TreeTransform.h" 15bdd1243dSDimitry Andric #include "clang/AST/ASTLambda.h" 1655e4f9d5SDimitry Andric #include "clang/AST/ExprConcepts.h" 17480093f4SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h" 18480093f4SDimitry Andric #include "clang/Basic/OperatorPrecedence.h" 19*06c3fb27SDimitry Andric #include "clang/Sema/EnterExpressionEvaluationContext.h" 20*06c3fb27SDimitry Andric #include "clang/Sema/Initialization.h" 21*06c3fb27SDimitry Andric #include "clang/Sema/Overload.h" 22*06c3fb27SDimitry Andric #include "clang/Sema/Sema.h" 23*06c3fb27SDimitry Andric #include "clang/Sema/SemaDiagnostic.h" 24*06c3fb27SDimitry Andric #include "clang/Sema/SemaInternal.h" 25*06c3fb27SDimitry Andric #include "clang/Sema/Template.h" 26*06c3fb27SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 27480093f4SDimitry Andric #include "llvm/ADT/DenseMap.h" 28480093f4SDimitry Andric #include "llvm/ADT/PointerUnion.h" 29fe6060f1SDimitry Andric #include "llvm/ADT/StringExtras.h" 30bdd1243dSDimitry Andric #include <optional> 31fe6060f1SDimitry Andric 32a7dea167SDimitry Andric using namespace clang; 33a7dea167SDimitry Andric using namespace sema; 34a7dea167SDimitry Andric 355ffd83dbSDimitry Andric namespace { 365ffd83dbSDimitry Andric class LogicalBinOp { 37bdd1243dSDimitry Andric SourceLocation Loc; 385ffd83dbSDimitry Andric OverloadedOperatorKind Op = OO_None; 395ffd83dbSDimitry Andric const Expr *LHS = nullptr; 405ffd83dbSDimitry Andric const Expr *RHS = nullptr; 415ffd83dbSDimitry Andric 425ffd83dbSDimitry Andric public: 435ffd83dbSDimitry Andric LogicalBinOp(const Expr *E) { 445ffd83dbSDimitry Andric if (auto *BO = dyn_cast<BinaryOperator>(E)) { 455ffd83dbSDimitry Andric Op = BinaryOperator::getOverloadedOperator(BO->getOpcode()); 465ffd83dbSDimitry Andric LHS = BO->getLHS(); 475ffd83dbSDimitry Andric RHS = BO->getRHS(); 48bdd1243dSDimitry Andric Loc = BO->getExprLoc(); 495ffd83dbSDimitry Andric } else if (auto *OO = dyn_cast<CXXOperatorCallExpr>(E)) { 50fe6060f1SDimitry Andric // If OO is not || or && it might not have exactly 2 arguments. 51fe6060f1SDimitry Andric if (OO->getNumArgs() == 2) { 525ffd83dbSDimitry Andric Op = OO->getOperator(); 535ffd83dbSDimitry Andric LHS = OO->getArg(0); 545ffd83dbSDimitry Andric RHS = OO->getArg(1); 55bdd1243dSDimitry Andric Loc = OO->getOperatorLoc(); 565ffd83dbSDimitry Andric } 575ffd83dbSDimitry Andric } 58fe6060f1SDimitry Andric } 595ffd83dbSDimitry Andric 605ffd83dbSDimitry Andric bool isAnd() const { return Op == OO_AmpAmp; } 615ffd83dbSDimitry Andric bool isOr() const { return Op == OO_PipePipe; } 625ffd83dbSDimitry Andric explicit operator bool() const { return isAnd() || isOr(); } 635ffd83dbSDimitry Andric 645ffd83dbSDimitry Andric const Expr *getLHS() const { return LHS; } 655ffd83dbSDimitry Andric const Expr *getRHS() const { return RHS; } 66bdd1243dSDimitry Andric 67bdd1243dSDimitry Andric ExprResult recreateBinOp(Sema &SemaRef, ExprResult LHS) const { 68bdd1243dSDimitry Andric return recreateBinOp(SemaRef, LHS, const_cast<Expr *>(getRHS())); 69bdd1243dSDimitry Andric } 70bdd1243dSDimitry Andric 71bdd1243dSDimitry Andric ExprResult recreateBinOp(Sema &SemaRef, ExprResult LHS, 72bdd1243dSDimitry Andric ExprResult RHS) const { 73bdd1243dSDimitry Andric assert((isAnd() || isOr()) && "Not the right kind of op?"); 74bdd1243dSDimitry Andric assert((!LHS.isInvalid() && !RHS.isInvalid()) && "not good expressions?"); 75bdd1243dSDimitry Andric 76bdd1243dSDimitry Andric if (!LHS.isUsable() || !RHS.isUsable()) 77bdd1243dSDimitry Andric return ExprEmpty(); 78bdd1243dSDimitry Andric 79bdd1243dSDimitry Andric // We should just be able to 'normalize' these to the builtin Binary 80bdd1243dSDimitry Andric // Operator, since that is how they are evaluated in constriant checks. 81bdd1243dSDimitry Andric return BinaryOperator::Create(SemaRef.Context, LHS.get(), RHS.get(), 82bdd1243dSDimitry Andric BinaryOperator::getOverloadedOpcode(Op), 83bdd1243dSDimitry Andric SemaRef.Context.BoolTy, VK_PRValue, 84bdd1243dSDimitry Andric OK_Ordinary, Loc, FPOptionsOverride{}); 85bdd1243dSDimitry Andric } 865ffd83dbSDimitry Andric }; 875ffd83dbSDimitry Andric } 885ffd83dbSDimitry Andric 895ffd83dbSDimitry Andric bool Sema::CheckConstraintExpression(const Expr *ConstraintExpression, 905ffd83dbSDimitry Andric Token NextToken, bool *PossibleNonPrimary, 91480093f4SDimitry Andric bool IsTrailingRequiresClause) { 92a7dea167SDimitry Andric // C++2a [temp.constr.atomic]p1 93a7dea167SDimitry Andric // ..E shall be a constant expression of type bool. 94a7dea167SDimitry Andric 95a7dea167SDimitry Andric ConstraintExpression = ConstraintExpression->IgnoreParenImpCasts(); 96a7dea167SDimitry Andric 975ffd83dbSDimitry Andric if (LogicalBinOp BO = ConstraintExpression) { 985ffd83dbSDimitry Andric return CheckConstraintExpression(BO.getLHS(), NextToken, 99480093f4SDimitry Andric PossibleNonPrimary) && 1005ffd83dbSDimitry Andric CheckConstraintExpression(BO.getRHS(), NextToken, 101480093f4SDimitry Andric PossibleNonPrimary); 102a7dea167SDimitry Andric } else if (auto *C = dyn_cast<ExprWithCleanups>(ConstraintExpression)) 103480093f4SDimitry Andric return CheckConstraintExpression(C->getSubExpr(), NextToken, 104480093f4SDimitry Andric PossibleNonPrimary); 105a7dea167SDimitry Andric 106a7dea167SDimitry Andric QualType Type = ConstraintExpression->getType(); 107480093f4SDimitry Andric 108480093f4SDimitry Andric auto CheckForNonPrimary = [&] { 109*06c3fb27SDimitry Andric if (!PossibleNonPrimary) 110*06c3fb27SDimitry Andric return; 111*06c3fb27SDimitry Andric 112480093f4SDimitry Andric *PossibleNonPrimary = 113480093f4SDimitry Andric // We have the following case: 114480093f4SDimitry Andric // template<typename> requires func(0) struct S { }; 115480093f4SDimitry Andric // The user probably isn't aware of the parentheses required around 116480093f4SDimitry Andric // the function call, and we're only going to parse 'func' as the 117480093f4SDimitry Andric // primary-expression, and complain that it is of non-bool type. 118*06c3fb27SDimitry Andric // 119*06c3fb27SDimitry Andric // However, if we're in a lambda, this might also be: 120*06c3fb27SDimitry Andric // []<typename> requires var () {}; 121*06c3fb27SDimitry Andric // Which also looks like a function call due to the lambda parentheses, 122*06c3fb27SDimitry Andric // but unlike the first case, isn't an error, so this check is skipped. 123480093f4SDimitry Andric (NextToken.is(tok::l_paren) && 124480093f4SDimitry Andric (IsTrailingRequiresClause || 125480093f4SDimitry Andric (Type->isDependentType() && 126*06c3fb27SDimitry Andric isa<UnresolvedLookupExpr>(ConstraintExpression) && 127*06c3fb27SDimitry Andric !dyn_cast_if_present<LambdaScopeInfo>(getCurFunction())) || 128480093f4SDimitry Andric Type->isFunctionType() || 129480093f4SDimitry Andric Type->isSpecificBuiltinType(BuiltinType::Overload))) || 130480093f4SDimitry Andric // We have the following case: 131480093f4SDimitry Andric // template<typename T> requires size_<T> == 0 struct S { }; 132480093f4SDimitry Andric // The user probably isn't aware of the parentheses required around 133480093f4SDimitry Andric // the binary operator, and we're only going to parse 'func' as the 134480093f4SDimitry Andric // first operand, and complain that it is of non-bool type. 135480093f4SDimitry Andric getBinOpPrecedence(NextToken.getKind(), 136480093f4SDimitry Andric /*GreaterThanIsOperator=*/true, 137480093f4SDimitry Andric getLangOpts().CPlusPlus11) > prec::LogicalAnd; 138480093f4SDimitry Andric }; 139480093f4SDimitry Andric 140480093f4SDimitry Andric // An atomic constraint! 141480093f4SDimitry Andric if (ConstraintExpression->isTypeDependent()) { 142480093f4SDimitry Andric CheckForNonPrimary(); 143480093f4SDimitry Andric return true; 144480093f4SDimitry Andric } 145480093f4SDimitry Andric 146a7dea167SDimitry Andric if (!Context.hasSameUnqualifiedType(Type, Context.BoolTy)) { 147a7dea167SDimitry Andric Diag(ConstraintExpression->getExprLoc(), 148a7dea167SDimitry Andric diag::err_non_bool_atomic_constraint) << Type 149a7dea167SDimitry Andric << ConstraintExpression->getSourceRange(); 150480093f4SDimitry Andric CheckForNonPrimary(); 151a7dea167SDimitry Andric return false; 152a7dea167SDimitry Andric } 153480093f4SDimitry Andric 154480093f4SDimitry Andric if (PossibleNonPrimary) 155480093f4SDimitry Andric *PossibleNonPrimary = false; 156a7dea167SDimitry Andric return true; 157a7dea167SDimitry Andric } 158a7dea167SDimitry Andric 159bdd1243dSDimitry Andric namespace { 160bdd1243dSDimitry Andric struct SatisfactionStackRAII { 161bdd1243dSDimitry Andric Sema &SemaRef; 1621ac55f4cSDimitry Andric bool Inserted = false; 1631ac55f4cSDimitry Andric SatisfactionStackRAII(Sema &SemaRef, const NamedDecl *ND, 164*06c3fb27SDimitry Andric const llvm::FoldingSetNodeID &FSNID) 165bdd1243dSDimitry Andric : SemaRef(SemaRef) { 1661ac55f4cSDimitry Andric if (ND) { 1671ac55f4cSDimitry Andric SemaRef.PushSatisfactionStackEntry(ND, FSNID); 1681ac55f4cSDimitry Andric Inserted = true; 169bdd1243dSDimitry Andric } 1701ac55f4cSDimitry Andric } 1711ac55f4cSDimitry Andric ~SatisfactionStackRAII() { 1721ac55f4cSDimitry Andric if (Inserted) 1731ac55f4cSDimitry Andric SemaRef.PopSatisfactionStackEntry(); 1741ac55f4cSDimitry Andric } 175bdd1243dSDimitry Andric }; 176bdd1243dSDimitry Andric } // namespace 177bdd1243dSDimitry Andric 178480093f4SDimitry Andric template <typename AtomicEvaluator> 179bdd1243dSDimitry Andric static ExprResult 180480093f4SDimitry Andric calculateConstraintSatisfaction(Sema &S, const Expr *ConstraintExpr, 181480093f4SDimitry Andric ConstraintSatisfaction &Satisfaction, 182480093f4SDimitry Andric AtomicEvaluator &&Evaluator) { 183a7dea167SDimitry Andric ConstraintExpr = ConstraintExpr->IgnoreParenImpCasts(); 184a7dea167SDimitry Andric 1855ffd83dbSDimitry Andric if (LogicalBinOp BO = ConstraintExpr) { 186bdd1243dSDimitry Andric ExprResult LHSRes = calculateConstraintSatisfaction( 187bdd1243dSDimitry Andric S, BO.getLHS(), Satisfaction, Evaluator); 188bdd1243dSDimitry Andric 189bdd1243dSDimitry Andric if (LHSRes.isInvalid()) 190bdd1243dSDimitry Andric return ExprError(); 191480093f4SDimitry Andric 192480093f4SDimitry Andric bool IsLHSSatisfied = Satisfaction.IsSatisfied; 193480093f4SDimitry Andric 1945ffd83dbSDimitry Andric if (BO.isOr() && IsLHSSatisfied) 195480093f4SDimitry Andric // [temp.constr.op] p3 196480093f4SDimitry Andric // A disjunction is a constraint taking two operands. To determine if 197480093f4SDimitry Andric // a disjunction is satisfied, the satisfaction of the first operand 198480093f4SDimitry Andric // is checked. If that is satisfied, the disjunction is satisfied. 199480093f4SDimitry Andric // Otherwise, the disjunction is satisfied if and only if the second 200480093f4SDimitry Andric // operand is satisfied. 201bdd1243dSDimitry Andric // LHS is instantiated while RHS is not. Skip creating invalid BinaryOp. 202bdd1243dSDimitry Andric return LHSRes; 203480093f4SDimitry Andric 2045ffd83dbSDimitry Andric if (BO.isAnd() && !IsLHSSatisfied) 205480093f4SDimitry Andric // [temp.constr.op] p2 206480093f4SDimitry Andric // A conjunction is a constraint taking two operands. To determine if 207480093f4SDimitry Andric // a conjunction is satisfied, the satisfaction of the first operand 208480093f4SDimitry Andric // is checked. If that is not satisfied, the conjunction is not 209480093f4SDimitry Andric // satisfied. Otherwise, the conjunction is satisfied if and only if 210480093f4SDimitry Andric // the second operand is satisfied. 211bdd1243dSDimitry Andric // LHS is instantiated while RHS is not. Skip creating invalid BinaryOp. 212bdd1243dSDimitry Andric return LHSRes; 213480093f4SDimitry Andric 214bdd1243dSDimitry Andric ExprResult RHSRes = calculateConstraintSatisfaction( 2155ffd83dbSDimitry Andric S, BO.getRHS(), Satisfaction, std::forward<AtomicEvaluator>(Evaluator)); 216bdd1243dSDimitry Andric if (RHSRes.isInvalid()) 217bdd1243dSDimitry Andric return ExprError(); 218bdd1243dSDimitry Andric 219bdd1243dSDimitry Andric return BO.recreateBinOp(S, LHSRes, RHSRes); 220bdd1243dSDimitry Andric } 221bdd1243dSDimitry Andric 222bdd1243dSDimitry Andric if (auto *C = dyn_cast<ExprWithCleanups>(ConstraintExpr)) { 223bdd1243dSDimitry Andric // These aren't evaluated, so we don't care about cleanups, so we can just 224bdd1243dSDimitry Andric // evaluate these as if the cleanups didn't exist. 225bdd1243dSDimitry Andric return calculateConstraintSatisfaction( 226bdd1243dSDimitry Andric S, C->getSubExpr(), Satisfaction, 227480093f4SDimitry Andric std::forward<AtomicEvaluator>(Evaluator)); 2285ffd83dbSDimitry Andric } 229480093f4SDimitry Andric 230480093f4SDimitry Andric // An atomic constraint expression 231480093f4SDimitry Andric ExprResult SubstitutedAtomicExpr = Evaluator(ConstraintExpr); 232480093f4SDimitry Andric 233480093f4SDimitry Andric if (SubstitutedAtomicExpr.isInvalid()) 234bdd1243dSDimitry Andric return ExprError(); 235480093f4SDimitry Andric 236480093f4SDimitry Andric if (!SubstitutedAtomicExpr.isUsable()) 237480093f4SDimitry Andric // Evaluator has decided satisfaction without yielding an expression. 238bdd1243dSDimitry Andric return ExprEmpty(); 239bdd1243dSDimitry Andric 240bdd1243dSDimitry Andric // We don't have the ability to evaluate this, since it contains a 241bdd1243dSDimitry Andric // RecoveryExpr, so we want to fail overload resolution. Otherwise, 242bdd1243dSDimitry Andric // we'd potentially pick up a different overload, and cause confusing 243bdd1243dSDimitry Andric // diagnostics. SO, add a failure detail that will cause us to make this 244bdd1243dSDimitry Andric // overload set not viable. 245bdd1243dSDimitry Andric if (SubstitutedAtomicExpr.get()->containsErrors()) { 246bdd1243dSDimitry Andric Satisfaction.IsSatisfied = false; 247bdd1243dSDimitry Andric Satisfaction.ContainsErrors = true; 248bdd1243dSDimitry Andric 249bdd1243dSDimitry Andric PartialDiagnostic Msg = S.PDiag(diag::note_constraint_references_error); 250bdd1243dSDimitry Andric SmallString<128> DiagString; 251bdd1243dSDimitry Andric DiagString = ": "; 252bdd1243dSDimitry Andric Msg.EmitToString(S.getDiagnostics(), DiagString); 253bdd1243dSDimitry Andric unsigned MessageSize = DiagString.size(); 254bdd1243dSDimitry Andric char *Mem = new (S.Context) char[MessageSize]; 255bdd1243dSDimitry Andric memcpy(Mem, DiagString.c_str(), MessageSize); 256bdd1243dSDimitry Andric Satisfaction.Details.emplace_back( 257bdd1243dSDimitry Andric ConstraintExpr, 258bdd1243dSDimitry Andric new (S.Context) ConstraintSatisfaction::SubstitutionDiagnostic{ 259bdd1243dSDimitry Andric SubstitutedAtomicExpr.get()->getBeginLoc(), 260bdd1243dSDimitry Andric StringRef(Mem, MessageSize)}); 261bdd1243dSDimitry Andric return SubstitutedAtomicExpr; 262bdd1243dSDimitry Andric } 263a7dea167SDimitry Andric 264a7dea167SDimitry Andric EnterExpressionEvaluationContext ConstantEvaluated( 265480093f4SDimitry Andric S, Sema::ExpressionEvaluationContext::ConstantEvaluated); 266a7dea167SDimitry Andric SmallVector<PartialDiagnosticAt, 2> EvaluationDiags; 267a7dea167SDimitry Andric Expr::EvalResult EvalResult; 268a7dea167SDimitry Andric EvalResult.Diag = &EvaluationDiags; 269fe6060f1SDimitry Andric if (!SubstitutedAtomicExpr.get()->EvaluateAsConstantExpr(EvalResult, 270fe6060f1SDimitry Andric S.Context) || 271fe6060f1SDimitry Andric !EvaluationDiags.empty()) { 272a7dea167SDimitry Andric // C++2a [temp.constr.atomic]p1 273a7dea167SDimitry Andric // ...E shall be a constant expression of type bool. 274480093f4SDimitry Andric S.Diag(SubstitutedAtomicExpr.get()->getBeginLoc(), 275a7dea167SDimitry Andric diag::err_non_constant_constraint_expression) 276480093f4SDimitry Andric << SubstitutedAtomicExpr.get()->getSourceRange(); 277a7dea167SDimitry Andric for (const PartialDiagnosticAt &PDiag : EvaluationDiags) 278480093f4SDimitry Andric S.Diag(PDiag.first, PDiag.second); 279bdd1243dSDimitry Andric return ExprError(); 280a7dea167SDimitry Andric } 281a7dea167SDimitry Andric 282fe6060f1SDimitry Andric assert(EvalResult.Val.isInt() && 283fe6060f1SDimitry Andric "evaluating bool expression didn't produce int"); 284480093f4SDimitry Andric Satisfaction.IsSatisfied = EvalResult.Val.getInt().getBoolValue(); 285480093f4SDimitry Andric if (!Satisfaction.IsSatisfied) 286480093f4SDimitry Andric Satisfaction.Details.emplace_back(ConstraintExpr, 287480093f4SDimitry Andric SubstitutedAtomicExpr.get()); 288a7dea167SDimitry Andric 289bdd1243dSDimitry Andric return SubstitutedAtomicExpr; 290bdd1243dSDimitry Andric } 291bdd1243dSDimitry Andric 292bdd1243dSDimitry Andric static bool 2931ac55f4cSDimitry Andric DiagRecursiveConstraintEval(Sema &S, llvm::FoldingSetNodeID &ID, 2941ac55f4cSDimitry Andric const NamedDecl *Templ, const Expr *E, 295bdd1243dSDimitry Andric const MultiLevelTemplateArgumentList &MLTAL) { 296bdd1243dSDimitry Andric E->Profile(ID, S.Context, /*Canonical=*/true); 297bdd1243dSDimitry Andric for (const auto &List : MLTAL) 298bdd1243dSDimitry Andric for (const auto &TemplateArg : List.Args) 299bdd1243dSDimitry Andric TemplateArg.Profile(ID, S.Context); 300bdd1243dSDimitry Andric 301bdd1243dSDimitry Andric // Note that we have to do this with our own collection, because there are 302bdd1243dSDimitry Andric // times where a constraint-expression check can cause us to need to evaluate 303bdd1243dSDimitry Andric // other constriants that are unrelated, such as when evaluating a recovery 304bdd1243dSDimitry Andric // expression, or when trying to determine the constexpr-ness of special 305bdd1243dSDimitry Andric // members. Otherwise we could just use the 306bdd1243dSDimitry Andric // Sema::InstantiatingTemplate::isAlreadyBeingInstantiated function. 3071ac55f4cSDimitry Andric if (S.SatisfactionStackContains(Templ, ID)) { 308bdd1243dSDimitry Andric S.Diag(E->getExprLoc(), diag::err_constraint_depends_on_self) 309bdd1243dSDimitry Andric << const_cast<Expr *>(E) << E->getSourceRange(); 310bdd1243dSDimitry Andric return true; 311bdd1243dSDimitry Andric } 312bdd1243dSDimitry Andric 313a7dea167SDimitry Andric return false; 314a7dea167SDimitry Andric } 315480093f4SDimitry Andric 316bdd1243dSDimitry Andric static ExprResult calculateConstraintSatisfaction( 317bdd1243dSDimitry Andric Sema &S, const NamedDecl *Template, SourceLocation TemplateNameLoc, 318bdd1243dSDimitry Andric const MultiLevelTemplateArgumentList &MLTAL, const Expr *ConstraintExpr, 319bdd1243dSDimitry Andric ConstraintSatisfaction &Satisfaction) { 320480093f4SDimitry Andric return calculateConstraintSatisfaction( 321480093f4SDimitry Andric S, ConstraintExpr, Satisfaction, [&](const Expr *AtomicExpr) { 322480093f4SDimitry Andric EnterExpressionEvaluationContext ConstantEvaluated( 323bdd1243dSDimitry Andric S, Sema::ExpressionEvaluationContext::ConstantEvaluated, 324bdd1243dSDimitry Andric Sema::ReuseLambdaContextDecl); 325480093f4SDimitry Andric 326480093f4SDimitry Andric // Atomic constraint - substitute arguments and check satisfaction. 327480093f4SDimitry Andric ExprResult SubstitutedExpression; 328480093f4SDimitry Andric { 329480093f4SDimitry Andric TemplateDeductionInfo Info(TemplateNameLoc); 330480093f4SDimitry Andric Sema::InstantiatingTemplate Inst(S, AtomicExpr->getBeginLoc(), 33113138422SDimitry Andric Sema::InstantiatingTemplate::ConstraintSubstitution{}, 33213138422SDimitry Andric const_cast<NamedDecl *>(Template), Info, 33313138422SDimitry Andric AtomicExpr->getSourceRange()); 334480093f4SDimitry Andric if (Inst.isInvalid()) 335480093f4SDimitry Andric return ExprError(); 336bdd1243dSDimitry Andric 337bdd1243dSDimitry Andric llvm::FoldingSetNodeID ID; 3381ac55f4cSDimitry Andric if (Template && 3391ac55f4cSDimitry Andric DiagRecursiveConstraintEval(S, ID, Template, AtomicExpr, MLTAL)) { 340bdd1243dSDimitry Andric Satisfaction.IsSatisfied = false; 341bdd1243dSDimitry Andric Satisfaction.ContainsErrors = true; 342bdd1243dSDimitry Andric return ExprEmpty(); 343bdd1243dSDimitry Andric } 344bdd1243dSDimitry Andric 3451ac55f4cSDimitry Andric SatisfactionStackRAII StackRAII(S, Template, ID); 346bdd1243dSDimitry Andric 347480093f4SDimitry Andric // We do not want error diagnostics escaping here. 348480093f4SDimitry Andric Sema::SFINAETrap Trap(S); 349fe6060f1SDimitry Andric SubstitutedExpression = 350bdd1243dSDimitry Andric S.SubstConstraintExpr(const_cast<Expr *>(AtomicExpr), MLTAL); 351bdd1243dSDimitry Andric 352480093f4SDimitry Andric if (SubstitutedExpression.isInvalid() || Trap.hasErrorOccurred()) { 353480093f4SDimitry Andric // C++2a [temp.constr.atomic]p1 354480093f4SDimitry Andric // ...If substitution results in an invalid type or expression, the 355480093f4SDimitry Andric // constraint is not satisfied. 356480093f4SDimitry Andric if (!Trap.hasErrorOccurred()) 357349cc55cSDimitry Andric // A non-SFINAE error has occurred as a result of this 358480093f4SDimitry Andric // substitution. 359480093f4SDimitry Andric return ExprError(); 360480093f4SDimitry Andric 361480093f4SDimitry Andric PartialDiagnosticAt SubstDiag{SourceLocation(), 362480093f4SDimitry Andric PartialDiagnostic::NullDiagnostic()}; 363480093f4SDimitry Andric Info.takeSFINAEDiagnostic(SubstDiag); 364480093f4SDimitry Andric // FIXME: Concepts: This is an unfortunate consequence of there 365480093f4SDimitry Andric // being no serialization code for PartialDiagnostics and the fact 366480093f4SDimitry Andric // that serializing them would likely take a lot more storage than 367480093f4SDimitry Andric // just storing them as strings. We would still like, in the 368480093f4SDimitry Andric // future, to serialize the proper PartialDiagnostic as serializing 369480093f4SDimitry Andric // it as a string defeats the purpose of the diagnostic mechanism. 370480093f4SDimitry Andric SmallString<128> DiagString; 371480093f4SDimitry Andric DiagString = ": "; 372480093f4SDimitry Andric SubstDiag.second.EmitToString(S.getDiagnostics(), DiagString); 373480093f4SDimitry Andric unsigned MessageSize = DiagString.size(); 374480093f4SDimitry Andric char *Mem = new (S.Context) char[MessageSize]; 375480093f4SDimitry Andric memcpy(Mem, DiagString.c_str(), MessageSize); 376480093f4SDimitry Andric Satisfaction.Details.emplace_back( 377480093f4SDimitry Andric AtomicExpr, 378480093f4SDimitry Andric new (S.Context) ConstraintSatisfaction::SubstitutionDiagnostic{ 379480093f4SDimitry Andric SubstDiag.first, StringRef(Mem, MessageSize)}); 380480093f4SDimitry Andric Satisfaction.IsSatisfied = false; 381480093f4SDimitry Andric return ExprEmpty(); 382480093f4SDimitry Andric } 383480093f4SDimitry Andric } 384480093f4SDimitry Andric 385480093f4SDimitry Andric if (!S.CheckConstraintExpression(SubstitutedExpression.get())) 386480093f4SDimitry Andric return ExprError(); 387480093f4SDimitry Andric 388bdd1243dSDimitry Andric // [temp.constr.atomic]p3: To determine if an atomic constraint is 389bdd1243dSDimitry Andric // satisfied, the parameter mapping and template arguments are first 390bdd1243dSDimitry Andric // substituted into its expression. If substitution results in an 391bdd1243dSDimitry Andric // invalid type or expression, the constraint is not satisfied. 392bdd1243dSDimitry Andric // Otherwise, the lvalue-to-rvalue conversion is performed if necessary, 393bdd1243dSDimitry Andric // and E shall be a constant expression of type bool. 394bdd1243dSDimitry Andric // 395bdd1243dSDimitry Andric // Perform the L to R Value conversion if necessary. We do so for all 396bdd1243dSDimitry Andric // non-PRValue categories, else we fail to extend the lifetime of 397bdd1243dSDimitry Andric // temporaries, and that fails the constant expression check. 398bdd1243dSDimitry Andric if (!SubstitutedExpression.get()->isPRValue()) 399bdd1243dSDimitry Andric SubstitutedExpression = ImplicitCastExpr::Create( 400bdd1243dSDimitry Andric S.Context, SubstitutedExpression.get()->getType(), 401bdd1243dSDimitry Andric CK_LValueToRValue, SubstitutedExpression.get(), 402bdd1243dSDimitry Andric /*BasePath=*/nullptr, VK_PRValue, FPOptionsOverride()); 403bdd1243dSDimitry Andric 404480093f4SDimitry Andric return SubstitutedExpression; 405480093f4SDimitry Andric }); 406480093f4SDimitry Andric } 407480093f4SDimitry Andric 408bdd1243dSDimitry Andric static bool CheckConstraintSatisfaction( 409bdd1243dSDimitry Andric Sema &S, const NamedDecl *Template, ArrayRef<const Expr *> ConstraintExprs, 410bdd1243dSDimitry Andric llvm::SmallVectorImpl<Expr *> &Converted, 411bdd1243dSDimitry Andric const MultiLevelTemplateArgumentList &TemplateArgsLists, 412bdd1243dSDimitry Andric SourceRange TemplateIDRange, ConstraintSatisfaction &Satisfaction) { 413480093f4SDimitry Andric if (ConstraintExprs.empty()) { 414480093f4SDimitry Andric Satisfaction.IsSatisfied = true; 415480093f4SDimitry Andric return false; 416480093f4SDimitry Andric } 417480093f4SDimitry Andric 418bdd1243dSDimitry Andric if (TemplateArgsLists.isAnyArgInstantiationDependent()) { 419480093f4SDimitry Andric // No need to check satisfaction for dependent constraint expressions. 420480093f4SDimitry Andric Satisfaction.IsSatisfied = true; 421480093f4SDimitry Andric return false; 422480093f4SDimitry Andric } 423480093f4SDimitry Andric 424bdd1243dSDimitry Andric ArrayRef<TemplateArgument> TemplateArgs = 425bdd1243dSDimitry Andric TemplateArgsLists.getNumSubstitutedLevels() > 0 426bdd1243dSDimitry Andric ? TemplateArgsLists.getOutermost() 427bdd1243dSDimitry Andric : ArrayRef<TemplateArgument> {}; 428480093f4SDimitry Andric Sema::InstantiatingTemplate Inst(S, TemplateIDRange.getBegin(), 42913138422SDimitry Andric Sema::InstantiatingTemplate::ConstraintsCheck{}, 43013138422SDimitry Andric const_cast<NamedDecl *>(Template), TemplateArgs, TemplateIDRange); 431480093f4SDimitry Andric if (Inst.isInvalid()) 432480093f4SDimitry Andric return true; 433480093f4SDimitry Andric 434480093f4SDimitry Andric for (const Expr *ConstraintExpr : ConstraintExprs) { 435bdd1243dSDimitry Andric ExprResult Res = calculateConstraintSatisfaction( 436bdd1243dSDimitry Andric S, Template, TemplateIDRange.getBegin(), TemplateArgsLists, 437bdd1243dSDimitry Andric ConstraintExpr, Satisfaction); 438bdd1243dSDimitry Andric if (Res.isInvalid()) 439480093f4SDimitry Andric return true; 440bdd1243dSDimitry Andric 441bdd1243dSDimitry Andric Converted.push_back(Res.get()); 442bdd1243dSDimitry Andric if (!Satisfaction.IsSatisfied) { 443bdd1243dSDimitry Andric // Backfill the 'converted' list with nulls so we can keep the Converted 444bdd1243dSDimitry Andric // and unconverted lists in sync. 445bdd1243dSDimitry Andric Converted.append(ConstraintExprs.size() - Converted.size(), nullptr); 446480093f4SDimitry Andric // [temp.constr.op] p2 447480093f4SDimitry Andric // [...] To determine if a conjunction is satisfied, the satisfaction 448480093f4SDimitry Andric // of the first operand is checked. If that is not satisfied, the 449480093f4SDimitry Andric // conjunction is not satisfied. [...] 450480093f4SDimitry Andric return false; 451480093f4SDimitry Andric } 452bdd1243dSDimitry Andric } 453480093f4SDimitry Andric return false; 454480093f4SDimitry Andric } 455480093f4SDimitry Andric 45655e4f9d5SDimitry Andric bool Sema::CheckConstraintSatisfaction( 45713138422SDimitry Andric const NamedDecl *Template, ArrayRef<const Expr *> ConstraintExprs, 458bdd1243dSDimitry Andric llvm::SmallVectorImpl<Expr *> &ConvertedConstraints, 459bdd1243dSDimitry Andric const MultiLevelTemplateArgumentList &TemplateArgsLists, 460bdd1243dSDimitry Andric SourceRange TemplateIDRange, ConstraintSatisfaction &OutSatisfaction) { 46155e4f9d5SDimitry Andric if (ConstraintExprs.empty()) { 46255e4f9d5SDimitry Andric OutSatisfaction.IsSatisfied = true; 46355e4f9d5SDimitry Andric return false; 464480093f4SDimitry Andric } 46581ad6265SDimitry Andric if (!Template) { 466bdd1243dSDimitry Andric return ::CheckConstraintSatisfaction( 467bdd1243dSDimitry Andric *this, nullptr, ConstraintExprs, ConvertedConstraints, 468bdd1243dSDimitry Andric TemplateArgsLists, TemplateIDRange, OutSatisfaction); 46981ad6265SDimitry Andric } 470bdd1243dSDimitry Andric 471bdd1243dSDimitry Andric // A list of the template argument list flattened in a predictible manner for 472bdd1243dSDimitry Andric // the purposes of caching. The ConstraintSatisfaction type is in AST so it 473bdd1243dSDimitry Andric // has no access to the MultiLevelTemplateArgumentList, so this has to happen 474bdd1243dSDimitry Andric // here. 475bdd1243dSDimitry Andric llvm::SmallVector<TemplateArgument, 4> FlattenedArgs; 476bdd1243dSDimitry Andric for (auto List : TemplateArgsLists) 477bdd1243dSDimitry Andric FlattenedArgs.insert(FlattenedArgs.end(), List.Args.begin(), 478bdd1243dSDimitry Andric List.Args.end()); 479bdd1243dSDimitry Andric 48055e4f9d5SDimitry Andric llvm::FoldingSetNodeID ID; 481bdd1243dSDimitry Andric ConstraintSatisfaction::Profile(ID, Context, Template, FlattenedArgs); 48281ad6265SDimitry Andric void *InsertPos; 48381ad6265SDimitry Andric if (auto *Cached = SatisfactionCache.FindNodeOrInsertPos(ID, InsertPos)) { 48481ad6265SDimitry Andric OutSatisfaction = *Cached; 48555e4f9d5SDimitry Andric return false; 48655e4f9d5SDimitry Andric } 487bdd1243dSDimitry Andric 48881ad6265SDimitry Andric auto Satisfaction = 489bdd1243dSDimitry Andric std::make_unique<ConstraintSatisfaction>(Template, FlattenedArgs); 49013138422SDimitry Andric if (::CheckConstraintSatisfaction(*this, Template, ConstraintExprs, 491bdd1243dSDimitry Andric ConvertedConstraints, TemplateArgsLists, 492bdd1243dSDimitry Andric TemplateIDRange, *Satisfaction)) { 493bdd1243dSDimitry Andric OutSatisfaction = *Satisfaction; 49455e4f9d5SDimitry Andric return true; 495480093f4SDimitry Andric } 496bdd1243dSDimitry Andric 497bdd1243dSDimitry Andric if (auto *Cached = SatisfactionCache.FindNodeOrInsertPos(ID, InsertPos)) { 498bdd1243dSDimitry Andric // The evaluation of this constraint resulted in us trying to re-evaluate it 499bdd1243dSDimitry Andric // recursively. This isn't really possible, except we try to form a 500bdd1243dSDimitry Andric // RecoveryExpr as a part of the evaluation. If this is the case, just 501bdd1243dSDimitry Andric // return the 'cached' version (which will have the same result), and save 502bdd1243dSDimitry Andric // ourselves the extra-insert. If it ever becomes possible to legitimately 503bdd1243dSDimitry Andric // recursively check a constraint, we should skip checking the 'inner' one 504bdd1243dSDimitry Andric // above, and replace the cached version with this one, as it would be more 505bdd1243dSDimitry Andric // specific. 506bdd1243dSDimitry Andric OutSatisfaction = *Cached; 507bdd1243dSDimitry Andric return false; 508bdd1243dSDimitry Andric } 509bdd1243dSDimitry Andric 510bdd1243dSDimitry Andric // Else we can simply add this satisfaction to the list. 51155e4f9d5SDimitry Andric OutSatisfaction = *Satisfaction; 51281ad6265SDimitry Andric // We cannot use InsertPos here because CheckConstraintSatisfaction might have 51381ad6265SDimitry Andric // invalidated it. 51481ad6265SDimitry Andric // Note that entries of SatisfactionCache are deleted in Sema's destructor. 51581ad6265SDimitry Andric SatisfactionCache.InsertNode(Satisfaction.release()); 51655e4f9d5SDimitry Andric return false; 517480093f4SDimitry Andric } 518480093f4SDimitry Andric 519480093f4SDimitry Andric bool Sema::CheckConstraintSatisfaction(const Expr *ConstraintExpr, 520480093f4SDimitry Andric ConstraintSatisfaction &Satisfaction) { 521480093f4SDimitry Andric return calculateConstraintSatisfaction( 522480093f4SDimitry Andric *this, ConstraintExpr, Satisfaction, 52381ad6265SDimitry Andric [this](const Expr *AtomicExpr) -> ExprResult { 52481ad6265SDimitry Andric // We only do this to immitate lvalue-to-rvalue conversion. 525bdd1243dSDimitry Andric return PerformContextuallyConvertToBool( 526bdd1243dSDimitry Andric const_cast<Expr *>(AtomicExpr)); 527bdd1243dSDimitry Andric }) 528bdd1243dSDimitry Andric .isInvalid(); 529bdd1243dSDimitry Andric } 530bdd1243dSDimitry Andric 531*06c3fb27SDimitry Andric bool Sema::addInstantiatedCapturesToScope( 532*06c3fb27SDimitry Andric FunctionDecl *Function, const FunctionDecl *PatternDecl, 533*06c3fb27SDimitry Andric LocalInstantiationScope &Scope, 534*06c3fb27SDimitry Andric const MultiLevelTemplateArgumentList &TemplateArgs) { 535*06c3fb27SDimitry Andric const auto *LambdaClass = cast<CXXMethodDecl>(Function)->getParent(); 536*06c3fb27SDimitry Andric const auto *LambdaPattern = cast<CXXMethodDecl>(PatternDecl)->getParent(); 537*06c3fb27SDimitry Andric 538*06c3fb27SDimitry Andric unsigned Instantiated = 0; 539*06c3fb27SDimitry Andric 540*06c3fb27SDimitry Andric auto AddSingleCapture = [&](const ValueDecl *CapturedPattern, 541*06c3fb27SDimitry Andric unsigned Index) { 542*06c3fb27SDimitry Andric ValueDecl *CapturedVar = LambdaClass->getCapture(Index)->getCapturedVar(); 543*06c3fb27SDimitry Andric if (cast<CXXMethodDecl>(Function)->isConst()) { 544*06c3fb27SDimitry Andric QualType T = CapturedVar->getType(); 545*06c3fb27SDimitry Andric T.addConst(); 546*06c3fb27SDimitry Andric CapturedVar->setType(T); 547*06c3fb27SDimitry Andric } 548*06c3fb27SDimitry Andric if (CapturedVar->isInitCapture()) 549*06c3fb27SDimitry Andric Scope.InstantiatedLocal(CapturedPattern, CapturedVar); 550*06c3fb27SDimitry Andric }; 551*06c3fb27SDimitry Andric 552*06c3fb27SDimitry Andric for (const LambdaCapture &CapturePattern : LambdaPattern->captures()) { 553*06c3fb27SDimitry Andric if (!CapturePattern.capturesVariable()) { 554*06c3fb27SDimitry Andric Instantiated++; 555*06c3fb27SDimitry Andric continue; 556*06c3fb27SDimitry Andric } 557*06c3fb27SDimitry Andric const ValueDecl *CapturedPattern = CapturePattern.getCapturedVar(); 558*06c3fb27SDimitry Andric if (!CapturedPattern->isParameterPack()) { 559*06c3fb27SDimitry Andric AddSingleCapture(CapturedPattern, Instantiated++); 560*06c3fb27SDimitry Andric } else { 561*06c3fb27SDimitry Andric Scope.MakeInstantiatedLocalArgPack(CapturedPattern); 562*06c3fb27SDimitry Andric std::optional<unsigned> NumArgumentsInExpansion = 563*06c3fb27SDimitry Andric getNumArgumentsInExpansion(CapturedPattern->getType(), TemplateArgs); 564*06c3fb27SDimitry Andric if (!NumArgumentsInExpansion) 565*06c3fb27SDimitry Andric continue; 566*06c3fb27SDimitry Andric for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) 567*06c3fb27SDimitry Andric AddSingleCapture(CapturedPattern, Instantiated++); 568*06c3fb27SDimitry Andric } 569*06c3fb27SDimitry Andric } 570*06c3fb27SDimitry Andric return false; 571*06c3fb27SDimitry Andric } 572*06c3fb27SDimitry Andric 573bdd1243dSDimitry Andric bool Sema::SetupConstraintScope( 574bdd1243dSDimitry Andric FunctionDecl *FD, std::optional<ArrayRef<TemplateArgument>> TemplateArgs, 575bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL, LocalInstantiationScope &Scope) { 576bdd1243dSDimitry Andric if (FD->isTemplateInstantiation() && FD->getPrimaryTemplate()) { 577bdd1243dSDimitry Andric FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate(); 578bdd1243dSDimitry Andric InstantiatingTemplate Inst( 579bdd1243dSDimitry Andric *this, FD->getPointOfInstantiation(), 580bdd1243dSDimitry Andric Sema::InstantiatingTemplate::ConstraintsCheck{}, PrimaryTemplate, 581bdd1243dSDimitry Andric TemplateArgs ? *TemplateArgs : ArrayRef<TemplateArgument>{}, 582bdd1243dSDimitry Andric SourceRange()); 583bdd1243dSDimitry Andric if (Inst.isInvalid()) 584bdd1243dSDimitry Andric return true; 585bdd1243dSDimitry Andric 586bdd1243dSDimitry Andric // addInstantiatedParametersToScope creates a map of 'uninstantiated' to 587bdd1243dSDimitry Andric // 'instantiated' parameters and adds it to the context. For the case where 588bdd1243dSDimitry Andric // this function is a template being instantiated NOW, we also need to add 589bdd1243dSDimitry Andric // the list of current template arguments to the list so that they also can 590bdd1243dSDimitry Andric // be picked out of the map. 591bdd1243dSDimitry Andric if (auto *SpecArgs = FD->getTemplateSpecializationArgs()) { 592bdd1243dSDimitry Andric MultiLevelTemplateArgumentList JustTemplArgs(FD, SpecArgs->asArray(), 593bdd1243dSDimitry Andric /*Final=*/false); 594bdd1243dSDimitry Andric if (addInstantiatedParametersToScope( 595bdd1243dSDimitry Andric FD, PrimaryTemplate->getTemplatedDecl(), Scope, JustTemplArgs)) 596bdd1243dSDimitry Andric return true; 597bdd1243dSDimitry Andric } 598bdd1243dSDimitry Andric 599bdd1243dSDimitry Andric // If this is a member function, make sure we get the parameters that 600bdd1243dSDimitry Andric // reference the original primary template. 601bdd1243dSDimitry Andric if (const auto *FromMemTempl = 602bdd1243dSDimitry Andric PrimaryTemplate->getInstantiatedFromMemberTemplate()) { 603bdd1243dSDimitry Andric if (addInstantiatedParametersToScope(FD, FromMemTempl->getTemplatedDecl(), 604bdd1243dSDimitry Andric Scope, MLTAL)) 605bdd1243dSDimitry Andric return true; 606*06c3fb27SDimitry Andric // Make sure the captures are also added to the instantiation scope. 607*06c3fb27SDimitry Andric if (isLambdaCallOperator(FD) && 608*06c3fb27SDimitry Andric addInstantiatedCapturesToScope(FD, FromMemTempl->getTemplatedDecl(), 609*06c3fb27SDimitry Andric Scope, MLTAL)) 610*06c3fb27SDimitry Andric return true; 611bdd1243dSDimitry Andric } 612bdd1243dSDimitry Andric 613bdd1243dSDimitry Andric return false; 614bdd1243dSDimitry Andric } 615bdd1243dSDimitry Andric 616bdd1243dSDimitry Andric if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization || 617bdd1243dSDimitry Andric FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate) { 618bdd1243dSDimitry Andric FunctionDecl *InstantiatedFrom = 619bdd1243dSDimitry Andric FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization 620bdd1243dSDimitry Andric ? FD->getInstantiatedFromMemberFunction() 621bdd1243dSDimitry Andric : FD->getInstantiatedFromDecl(); 622bdd1243dSDimitry Andric 623bdd1243dSDimitry Andric InstantiatingTemplate Inst( 624bdd1243dSDimitry Andric *this, FD->getPointOfInstantiation(), 625bdd1243dSDimitry Andric Sema::InstantiatingTemplate::ConstraintsCheck{}, InstantiatedFrom, 626bdd1243dSDimitry Andric TemplateArgs ? *TemplateArgs : ArrayRef<TemplateArgument>{}, 627bdd1243dSDimitry Andric SourceRange()); 628bdd1243dSDimitry Andric if (Inst.isInvalid()) 629bdd1243dSDimitry Andric return true; 630bdd1243dSDimitry Andric 631bdd1243dSDimitry Andric // Case where this was not a template, but instantiated as a 632bdd1243dSDimitry Andric // child-function. 633bdd1243dSDimitry Andric if (addInstantiatedParametersToScope(FD, InstantiatedFrom, Scope, MLTAL)) 634bdd1243dSDimitry Andric return true; 635*06c3fb27SDimitry Andric 636*06c3fb27SDimitry Andric // Make sure the captures are also added to the instantiation scope. 637*06c3fb27SDimitry Andric if (isLambdaCallOperator(FD) && 638*06c3fb27SDimitry Andric addInstantiatedCapturesToScope(FD, InstantiatedFrom, Scope, MLTAL)) 639*06c3fb27SDimitry Andric return true; 640bdd1243dSDimitry Andric } 641bdd1243dSDimitry Andric 642bdd1243dSDimitry Andric return false; 643bdd1243dSDimitry Andric } 644bdd1243dSDimitry Andric 645bdd1243dSDimitry Andric // This function collects all of the template arguments for the purposes of 646bdd1243dSDimitry Andric // constraint-instantiation and checking. 647bdd1243dSDimitry Andric std::optional<MultiLevelTemplateArgumentList> 648bdd1243dSDimitry Andric Sema::SetupConstraintCheckingTemplateArgumentsAndScope( 649bdd1243dSDimitry Andric FunctionDecl *FD, std::optional<ArrayRef<TemplateArgument>> TemplateArgs, 650bdd1243dSDimitry Andric LocalInstantiationScope &Scope) { 651bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL; 652bdd1243dSDimitry Andric 653bdd1243dSDimitry Andric // Collect the list of template arguments relative to the 'primary' template. 654bdd1243dSDimitry Andric // We need the entire list, since the constraint is completely uninstantiated 655bdd1243dSDimitry Andric // at this point. 656bdd1243dSDimitry Andric MLTAL = 657bdd1243dSDimitry Andric getTemplateInstantiationArgs(FD, /*Final=*/false, /*Innermost=*/nullptr, 658bdd1243dSDimitry Andric /*RelativeToPrimary=*/true, 659bdd1243dSDimitry Andric /*Pattern=*/nullptr, 660bdd1243dSDimitry Andric /*ForConstraintInstantiation=*/true); 661bdd1243dSDimitry Andric if (SetupConstraintScope(FD, TemplateArgs, MLTAL, Scope)) 662bdd1243dSDimitry Andric return std::nullopt; 663bdd1243dSDimitry Andric 664bdd1243dSDimitry Andric return MLTAL; 665480093f4SDimitry Andric } 666480093f4SDimitry Andric 66713138422SDimitry Andric bool Sema::CheckFunctionConstraints(const FunctionDecl *FD, 66813138422SDimitry Andric ConstraintSatisfaction &Satisfaction, 669bdd1243dSDimitry Andric SourceLocation UsageLoc, 670bdd1243dSDimitry Andric bool ForOverloadResolution) { 671bdd1243dSDimitry Andric // Don't check constraints if the function is dependent. Also don't check if 672bdd1243dSDimitry Andric // this is a function template specialization, as the call to 673bdd1243dSDimitry Andric // CheckinstantiatedFunctionTemplateConstraints after this will check it 674bdd1243dSDimitry Andric // better. 675bdd1243dSDimitry Andric if (FD->isDependentContext() || 676bdd1243dSDimitry Andric FD->getTemplatedKind() == 677bdd1243dSDimitry Andric FunctionDecl::TK_FunctionTemplateSpecialization) { 67813138422SDimitry Andric Satisfaction.IsSatisfied = true; 67913138422SDimitry Andric return false; 68013138422SDimitry Andric } 681bdd1243dSDimitry Andric 682*06c3fb27SDimitry Andric // A lambda conversion operator has the same constraints as the call operator 683*06c3fb27SDimitry Andric // and constraints checking relies on whether we are in a lambda call operator 684*06c3fb27SDimitry Andric // (and may refer to its parameters), so check the call operator instead. 685*06c3fb27SDimitry Andric if (const auto *MD = dyn_cast<CXXConversionDecl>(FD); 686*06c3fb27SDimitry Andric MD && isLambdaConversionOperator(const_cast<CXXConversionDecl *>(MD))) 687*06c3fb27SDimitry Andric return CheckFunctionConstraints(MD->getParent()->getLambdaCallOperator(), 688*06c3fb27SDimitry Andric Satisfaction, UsageLoc, 689*06c3fb27SDimitry Andric ForOverloadResolution); 690*06c3fb27SDimitry Andric 691bdd1243dSDimitry Andric DeclContext *CtxToSave = const_cast<FunctionDecl *>(FD); 692bdd1243dSDimitry Andric 693bdd1243dSDimitry Andric while (isLambdaCallOperator(CtxToSave) || FD->isTransparentContext()) { 694bdd1243dSDimitry Andric if (isLambdaCallOperator(CtxToSave)) 695bdd1243dSDimitry Andric CtxToSave = CtxToSave->getParent()->getParent(); 696bdd1243dSDimitry Andric else 697bdd1243dSDimitry Andric CtxToSave = CtxToSave->getNonTransparentContext(); 698bdd1243dSDimitry Andric } 699bdd1243dSDimitry Andric 700bdd1243dSDimitry Andric ContextRAII SavedContext{*this, CtxToSave}; 701bdd1243dSDimitry Andric LocalInstantiationScope Scope(*this, !ForOverloadResolution || 702bdd1243dSDimitry Andric isLambdaCallOperator(FD)); 703bdd1243dSDimitry Andric std::optional<MultiLevelTemplateArgumentList> MLTAL = 704bdd1243dSDimitry Andric SetupConstraintCheckingTemplateArgumentsAndScope( 705bdd1243dSDimitry Andric const_cast<FunctionDecl *>(FD), {}, Scope); 706bdd1243dSDimitry Andric 707bdd1243dSDimitry Andric if (!MLTAL) 708bdd1243dSDimitry Andric return true; 709bdd1243dSDimitry Andric 71013138422SDimitry Andric Qualifiers ThisQuals; 71113138422SDimitry Andric CXXRecordDecl *Record = nullptr; 71213138422SDimitry Andric if (auto *Method = dyn_cast<CXXMethodDecl>(FD)) { 71313138422SDimitry Andric ThisQuals = Method->getMethodQualifiers(); 71413138422SDimitry Andric Record = const_cast<CXXRecordDecl *>(Method->getParent()); 71513138422SDimitry Andric } 71613138422SDimitry Andric CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr); 717*06c3fb27SDimitry Andric return CheckConstraintSatisfaction( 718*06c3fb27SDimitry Andric FD, {FD->getTrailingRequiresClause()}, *MLTAL, 71913138422SDimitry Andric SourceRange(UsageLoc.isValid() ? UsageLoc : FD->getLocation()), 720*06c3fb27SDimitry Andric Satisfaction); 721bdd1243dSDimitry Andric } 722bdd1243dSDimitry Andric 723bdd1243dSDimitry Andric 724bdd1243dSDimitry Andric // Figure out the to-translation-unit depth for this function declaration for 725bdd1243dSDimitry Andric // the purpose of seeing if they differ by constraints. This isn't the same as 726bdd1243dSDimitry Andric // getTemplateDepth, because it includes already instantiated parents. 727bdd1243dSDimitry Andric static unsigned 728bdd1243dSDimitry Andric CalculateTemplateDepthForConstraints(Sema &S, const NamedDecl *ND, 729bdd1243dSDimitry Andric bool SkipForSpecialization = false) { 730bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL = S.getTemplateInstantiationArgs( 731bdd1243dSDimitry Andric ND, /*Final=*/false, /*Innermost=*/nullptr, /*RelativeToPrimary=*/true, 732bdd1243dSDimitry Andric /*Pattern=*/nullptr, 733bdd1243dSDimitry Andric /*ForConstraintInstantiation=*/true, SkipForSpecialization); 734*06c3fb27SDimitry Andric return MLTAL.getNumLevels(); 735bdd1243dSDimitry Andric } 736bdd1243dSDimitry Andric 737bdd1243dSDimitry Andric namespace { 738bdd1243dSDimitry Andric class AdjustConstraintDepth : public TreeTransform<AdjustConstraintDepth> { 739bdd1243dSDimitry Andric unsigned TemplateDepth = 0; 740bdd1243dSDimitry Andric public: 741bdd1243dSDimitry Andric using inherited = TreeTransform<AdjustConstraintDepth>; 742bdd1243dSDimitry Andric AdjustConstraintDepth(Sema &SemaRef, unsigned TemplateDepth) 743bdd1243dSDimitry Andric : inherited(SemaRef), TemplateDepth(TemplateDepth) {} 744bdd1243dSDimitry Andric 745bdd1243dSDimitry Andric using inherited::TransformTemplateTypeParmType; 746bdd1243dSDimitry Andric QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, 747bdd1243dSDimitry Andric TemplateTypeParmTypeLoc TL, bool) { 748bdd1243dSDimitry Andric const TemplateTypeParmType *T = TL.getTypePtr(); 749bdd1243dSDimitry Andric 750bdd1243dSDimitry Andric TemplateTypeParmDecl *NewTTPDecl = nullptr; 751bdd1243dSDimitry Andric if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl()) 752bdd1243dSDimitry Andric NewTTPDecl = cast_or_null<TemplateTypeParmDecl>( 753bdd1243dSDimitry Andric TransformDecl(TL.getNameLoc(), OldTTPDecl)); 754bdd1243dSDimitry Andric 755bdd1243dSDimitry Andric QualType Result = getSema().Context.getTemplateTypeParmType( 756bdd1243dSDimitry Andric T->getDepth() + TemplateDepth, T->getIndex(), T->isParameterPack(), 757bdd1243dSDimitry Andric NewTTPDecl); 758bdd1243dSDimitry Andric TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); 759bdd1243dSDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 760bdd1243dSDimitry Andric return Result; 761bdd1243dSDimitry Andric } 762bdd1243dSDimitry Andric }; 763bdd1243dSDimitry Andric } // namespace 764bdd1243dSDimitry Andric 765*06c3fb27SDimitry Andric static const Expr *SubstituteConstraintExpression(Sema &S, const NamedDecl *ND, 766*06c3fb27SDimitry Andric const Expr *ConstrExpr) { 767*06c3fb27SDimitry Andric MultiLevelTemplateArgumentList MLTAL = S.getTemplateInstantiationArgs( 768*06c3fb27SDimitry Andric ND, /*Final=*/false, /*Innermost=*/nullptr, 769*06c3fb27SDimitry Andric /*RelativeToPrimary=*/true, 770*06c3fb27SDimitry Andric /*Pattern=*/nullptr, /*ForConstraintInstantiation=*/true, 771*06c3fb27SDimitry Andric /*SkipForSpecialization*/ false); 772*06c3fb27SDimitry Andric if (MLTAL.getNumSubstitutedLevels() == 0) 773*06c3fb27SDimitry Andric return ConstrExpr; 774*06c3fb27SDimitry Andric 775*06c3fb27SDimitry Andric Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/false); 776*06c3fb27SDimitry Andric 777*06c3fb27SDimitry Andric Sema::InstantiatingTemplate Inst( 778*06c3fb27SDimitry Andric S, ND->getLocation(), 779*06c3fb27SDimitry Andric Sema::InstantiatingTemplate::ConstraintNormalization{}, 780*06c3fb27SDimitry Andric const_cast<NamedDecl *>(ND), SourceRange{}); 781*06c3fb27SDimitry Andric 782*06c3fb27SDimitry Andric if (Inst.isInvalid()) 783*06c3fb27SDimitry Andric return nullptr; 784*06c3fb27SDimitry Andric 785*06c3fb27SDimitry Andric std::optional<Sema::CXXThisScopeRAII> ThisScope; 786*06c3fb27SDimitry Andric if (auto *RD = dyn_cast<CXXRecordDecl>(ND->getDeclContext())) 787*06c3fb27SDimitry Andric ThisScope.emplace(S, const_cast<CXXRecordDecl *>(RD), Qualifiers()); 788*06c3fb27SDimitry Andric ExprResult SubstConstr = 789*06c3fb27SDimitry Andric S.SubstConstraintExpr(const_cast<clang::Expr *>(ConstrExpr), MLTAL); 790*06c3fb27SDimitry Andric if (SFINAE.hasErrorOccurred() || !SubstConstr.isUsable()) 791*06c3fb27SDimitry Andric return nullptr; 792*06c3fb27SDimitry Andric return SubstConstr.get(); 793*06c3fb27SDimitry Andric } 794*06c3fb27SDimitry Andric 795bdd1243dSDimitry Andric bool Sema::AreConstraintExpressionsEqual(const NamedDecl *Old, 796bdd1243dSDimitry Andric const Expr *OldConstr, 797bdd1243dSDimitry Andric const NamedDecl *New, 798bdd1243dSDimitry Andric const Expr *NewConstr) { 799*06c3fb27SDimitry Andric if (OldConstr == NewConstr) 800*06c3fb27SDimitry Andric return true; 801*06c3fb27SDimitry Andric // C++ [temp.constr.decl]p4 802*06c3fb27SDimitry Andric if (Old && New && Old != New && 803*06c3fb27SDimitry Andric Old->getLexicalDeclContext() != New->getLexicalDeclContext()) { 804*06c3fb27SDimitry Andric if (const Expr *SubstConstr = 805*06c3fb27SDimitry Andric SubstituteConstraintExpression(*this, Old, OldConstr)) 806*06c3fb27SDimitry Andric OldConstr = SubstConstr; 807*06c3fb27SDimitry Andric else 808*06c3fb27SDimitry Andric return false; 809*06c3fb27SDimitry Andric if (const Expr *SubstConstr = 810*06c3fb27SDimitry Andric SubstituteConstraintExpression(*this, New, NewConstr)) 811*06c3fb27SDimitry Andric NewConstr = SubstConstr; 812*06c3fb27SDimitry Andric else 813*06c3fb27SDimitry Andric return false; 814bdd1243dSDimitry Andric } 815bdd1243dSDimitry Andric 816bdd1243dSDimitry Andric llvm::FoldingSetNodeID ID1, ID2; 817bdd1243dSDimitry Andric OldConstr->Profile(ID1, Context, /*Canonical=*/true); 818bdd1243dSDimitry Andric NewConstr->Profile(ID2, Context, /*Canonical=*/true); 819bdd1243dSDimitry Andric return ID1 == ID2; 820bdd1243dSDimitry Andric } 821bdd1243dSDimitry Andric 822bdd1243dSDimitry Andric bool Sema::FriendConstraintsDependOnEnclosingTemplate(const FunctionDecl *FD) { 823bdd1243dSDimitry Andric assert(FD->getFriendObjectKind() && "Must be a friend!"); 824bdd1243dSDimitry Andric 825bdd1243dSDimitry Andric // The logic for non-templates is handled in ASTContext::isSameEntity, so we 826bdd1243dSDimitry Andric // don't have to bother checking 'DependsOnEnclosingTemplate' for a 827bdd1243dSDimitry Andric // non-function-template. 828bdd1243dSDimitry Andric assert(FD->getDescribedFunctionTemplate() && 829bdd1243dSDimitry Andric "Non-function templates don't need to be checked"); 830bdd1243dSDimitry Andric 831bdd1243dSDimitry Andric SmallVector<const Expr *, 3> ACs; 832bdd1243dSDimitry Andric FD->getDescribedFunctionTemplate()->getAssociatedConstraints(ACs); 833bdd1243dSDimitry Andric 834bdd1243dSDimitry Andric unsigned OldTemplateDepth = CalculateTemplateDepthForConstraints(*this, FD); 835bdd1243dSDimitry Andric for (const Expr *Constraint : ACs) 836bdd1243dSDimitry Andric if (ConstraintExpressionDependsOnEnclosingTemplate(FD, OldTemplateDepth, 837bdd1243dSDimitry Andric Constraint)) 838bdd1243dSDimitry Andric return true; 839bdd1243dSDimitry Andric 840bdd1243dSDimitry Andric return false; 84113138422SDimitry Andric } 84213138422SDimitry Andric 843480093f4SDimitry Andric bool Sema::EnsureTemplateArgumentListConstraints( 844bdd1243dSDimitry Andric TemplateDecl *TD, const MultiLevelTemplateArgumentList &TemplateArgsLists, 845480093f4SDimitry Andric SourceRange TemplateIDRange) { 846480093f4SDimitry Andric ConstraintSatisfaction Satisfaction; 847480093f4SDimitry Andric llvm::SmallVector<const Expr *, 3> AssociatedConstraints; 848480093f4SDimitry Andric TD->getAssociatedConstraints(AssociatedConstraints); 849bdd1243dSDimitry Andric if (CheckConstraintSatisfaction(TD, AssociatedConstraints, TemplateArgsLists, 850480093f4SDimitry Andric TemplateIDRange, Satisfaction)) 851480093f4SDimitry Andric return true; 852480093f4SDimitry Andric 853480093f4SDimitry Andric if (!Satisfaction.IsSatisfied) { 854480093f4SDimitry Andric SmallString<128> TemplateArgString; 855480093f4SDimitry Andric TemplateArgString = " "; 856480093f4SDimitry Andric TemplateArgString += getTemplateArgumentBindingsText( 857bdd1243dSDimitry Andric TD->getTemplateParameters(), TemplateArgsLists.getInnermost().data(), 858bdd1243dSDimitry Andric TemplateArgsLists.getInnermost().size()); 859480093f4SDimitry Andric 860480093f4SDimitry Andric Diag(TemplateIDRange.getBegin(), 861480093f4SDimitry Andric diag::err_template_arg_list_constraints_not_satisfied) 862480093f4SDimitry Andric << (int)getTemplateNameKindForDiagnostics(TemplateName(TD)) << TD 863480093f4SDimitry Andric << TemplateArgString << TemplateIDRange; 864480093f4SDimitry Andric DiagnoseUnsatisfiedConstraint(Satisfaction); 865480093f4SDimitry Andric return true; 866480093f4SDimitry Andric } 867480093f4SDimitry Andric return false; 868480093f4SDimitry Andric } 869480093f4SDimitry Andric 87081ad6265SDimitry Andric bool Sema::CheckInstantiatedFunctionTemplateConstraints( 87181ad6265SDimitry Andric SourceLocation PointOfInstantiation, FunctionDecl *Decl, 87281ad6265SDimitry Andric ArrayRef<TemplateArgument> TemplateArgs, 87381ad6265SDimitry Andric ConstraintSatisfaction &Satisfaction) { 87481ad6265SDimitry Andric // In most cases we're not going to have constraints, so check for that first. 87581ad6265SDimitry Andric FunctionTemplateDecl *Template = Decl->getPrimaryTemplate(); 87681ad6265SDimitry Andric // Note - code synthesis context for the constraints check is created 87781ad6265SDimitry Andric // inside CheckConstraintsSatisfaction. 87881ad6265SDimitry Andric SmallVector<const Expr *, 3> TemplateAC; 87981ad6265SDimitry Andric Template->getAssociatedConstraints(TemplateAC); 88081ad6265SDimitry Andric if (TemplateAC.empty()) { 88181ad6265SDimitry Andric Satisfaction.IsSatisfied = true; 88281ad6265SDimitry Andric return false; 88381ad6265SDimitry Andric } 88481ad6265SDimitry Andric 88581ad6265SDimitry Andric // Enter the scope of this instantiation. We don't use 88681ad6265SDimitry Andric // PushDeclContext because we don't have a scope. 88781ad6265SDimitry Andric Sema::ContextRAII savedContext(*this, Decl); 88881ad6265SDimitry Andric LocalInstantiationScope Scope(*this); 88981ad6265SDimitry Andric 890bdd1243dSDimitry Andric std::optional<MultiLevelTemplateArgumentList> MLTAL = 891bdd1243dSDimitry Andric SetupConstraintCheckingTemplateArgumentsAndScope(Decl, TemplateArgs, 892bdd1243dSDimitry Andric Scope); 893bdd1243dSDimitry Andric 894bdd1243dSDimitry Andric if (!MLTAL) 89581ad6265SDimitry Andric return true; 896bdd1243dSDimitry Andric 89781ad6265SDimitry Andric Qualifiers ThisQuals; 89881ad6265SDimitry Andric CXXRecordDecl *Record = nullptr; 89981ad6265SDimitry Andric if (auto *Method = dyn_cast<CXXMethodDecl>(Decl)) { 90081ad6265SDimitry Andric ThisQuals = Method->getMethodQualifiers(); 90181ad6265SDimitry Andric Record = Method->getParent(); 90281ad6265SDimitry Andric } 90381ad6265SDimitry Andric CXXThisScopeRAII ThisScope(*this, Record, ThisQuals, Record != nullptr); 904bdd1243dSDimitry Andric FunctionScopeRAII FuncScope(*this); 905bdd1243dSDimitry Andric if (isLambdaCallOperator(Decl)) 906bdd1243dSDimitry Andric PushLambdaScope(); 907bdd1243dSDimitry Andric else 908bdd1243dSDimitry Andric FuncScope.disable(); 909bdd1243dSDimitry Andric 910bdd1243dSDimitry Andric llvm::SmallVector<Expr *, 1> Converted; 911bdd1243dSDimitry Andric return CheckConstraintSatisfaction(Template, TemplateAC, Converted, *MLTAL, 91281ad6265SDimitry Andric PointOfInstantiation, Satisfaction); 91381ad6265SDimitry Andric } 91481ad6265SDimitry Andric 91555e4f9d5SDimitry Andric static void diagnoseUnsatisfiedRequirement(Sema &S, 91655e4f9d5SDimitry Andric concepts::ExprRequirement *Req, 91755e4f9d5SDimitry Andric bool First) { 91855e4f9d5SDimitry Andric assert(!Req->isSatisfied() 91955e4f9d5SDimitry Andric && "Diagnose() can only be used on an unsatisfied requirement"); 92055e4f9d5SDimitry Andric switch (Req->getSatisfactionStatus()) { 92155e4f9d5SDimitry Andric case concepts::ExprRequirement::SS_Dependent: 92255e4f9d5SDimitry Andric llvm_unreachable("Diagnosing a dependent requirement"); 92355e4f9d5SDimitry Andric break; 92455e4f9d5SDimitry Andric case concepts::ExprRequirement::SS_ExprSubstitutionFailure: { 92555e4f9d5SDimitry Andric auto *SubstDiag = Req->getExprSubstitutionDiagnostic(); 92655e4f9d5SDimitry Andric if (!SubstDiag->DiagMessage.empty()) 92755e4f9d5SDimitry Andric S.Diag(SubstDiag->DiagLoc, 92855e4f9d5SDimitry Andric diag::note_expr_requirement_expr_substitution_error) 92955e4f9d5SDimitry Andric << (int)First << SubstDiag->SubstitutedEntity 93055e4f9d5SDimitry Andric << SubstDiag->DiagMessage; 93155e4f9d5SDimitry Andric else 93255e4f9d5SDimitry Andric S.Diag(SubstDiag->DiagLoc, 93355e4f9d5SDimitry Andric diag::note_expr_requirement_expr_unknown_substitution_error) 93455e4f9d5SDimitry Andric << (int)First << SubstDiag->SubstitutedEntity; 93555e4f9d5SDimitry Andric break; 93655e4f9d5SDimitry Andric } 93755e4f9d5SDimitry Andric case concepts::ExprRequirement::SS_NoexceptNotMet: 93855e4f9d5SDimitry Andric S.Diag(Req->getNoexceptLoc(), 93955e4f9d5SDimitry Andric diag::note_expr_requirement_noexcept_not_met) 94055e4f9d5SDimitry Andric << (int)First << Req->getExpr(); 94155e4f9d5SDimitry Andric break; 94255e4f9d5SDimitry Andric case concepts::ExprRequirement::SS_TypeRequirementSubstitutionFailure: { 94355e4f9d5SDimitry Andric auto *SubstDiag = 94455e4f9d5SDimitry Andric Req->getReturnTypeRequirement().getSubstitutionDiagnostic(); 94555e4f9d5SDimitry Andric if (!SubstDiag->DiagMessage.empty()) 94655e4f9d5SDimitry Andric S.Diag(SubstDiag->DiagLoc, 94755e4f9d5SDimitry Andric diag::note_expr_requirement_type_requirement_substitution_error) 94855e4f9d5SDimitry Andric << (int)First << SubstDiag->SubstitutedEntity 94955e4f9d5SDimitry Andric << SubstDiag->DiagMessage; 95055e4f9d5SDimitry Andric else 95155e4f9d5SDimitry Andric S.Diag(SubstDiag->DiagLoc, 95255e4f9d5SDimitry Andric diag::note_expr_requirement_type_requirement_unknown_substitution_error) 95355e4f9d5SDimitry Andric << (int)First << SubstDiag->SubstitutedEntity; 95455e4f9d5SDimitry Andric break; 95555e4f9d5SDimitry Andric } 95655e4f9d5SDimitry Andric case concepts::ExprRequirement::SS_ConstraintsNotSatisfied: { 95755e4f9d5SDimitry Andric ConceptSpecializationExpr *ConstraintExpr = 95855e4f9d5SDimitry Andric Req->getReturnTypeRequirementSubstitutedConstraintExpr(); 959fe6060f1SDimitry Andric if (ConstraintExpr->getTemplateArgsAsWritten()->NumTemplateArgs == 1) { 96055e4f9d5SDimitry Andric // A simple case - expr type is the type being constrained and the concept 96155e4f9d5SDimitry Andric // was not provided arguments. 962fe6060f1SDimitry Andric Expr *e = Req->getExpr(); 963fe6060f1SDimitry Andric S.Diag(e->getBeginLoc(), 96455e4f9d5SDimitry Andric diag::note_expr_requirement_constraints_not_satisfied_simple) 965349cc55cSDimitry Andric << (int)First << S.Context.getReferenceQualifiedType(e) 96655e4f9d5SDimitry Andric << ConstraintExpr->getNamedConcept(); 967fe6060f1SDimitry Andric } else { 96855e4f9d5SDimitry Andric S.Diag(ConstraintExpr->getBeginLoc(), 96955e4f9d5SDimitry Andric diag::note_expr_requirement_constraints_not_satisfied) 97055e4f9d5SDimitry Andric << (int)First << ConstraintExpr; 971fe6060f1SDimitry Andric } 97255e4f9d5SDimitry Andric S.DiagnoseUnsatisfiedConstraint(ConstraintExpr->getSatisfaction()); 97355e4f9d5SDimitry Andric break; 97455e4f9d5SDimitry Andric } 97555e4f9d5SDimitry Andric case concepts::ExprRequirement::SS_Satisfied: 97655e4f9d5SDimitry Andric llvm_unreachable("We checked this above"); 97755e4f9d5SDimitry Andric } 97855e4f9d5SDimitry Andric } 97955e4f9d5SDimitry Andric 98055e4f9d5SDimitry Andric static void diagnoseUnsatisfiedRequirement(Sema &S, 98155e4f9d5SDimitry Andric concepts::TypeRequirement *Req, 98255e4f9d5SDimitry Andric bool First) { 98355e4f9d5SDimitry Andric assert(!Req->isSatisfied() 98455e4f9d5SDimitry Andric && "Diagnose() can only be used on an unsatisfied requirement"); 98555e4f9d5SDimitry Andric switch (Req->getSatisfactionStatus()) { 98655e4f9d5SDimitry Andric case concepts::TypeRequirement::SS_Dependent: 98755e4f9d5SDimitry Andric llvm_unreachable("Diagnosing a dependent requirement"); 98855e4f9d5SDimitry Andric return; 98955e4f9d5SDimitry Andric case concepts::TypeRequirement::SS_SubstitutionFailure: { 99055e4f9d5SDimitry Andric auto *SubstDiag = Req->getSubstitutionDiagnostic(); 99155e4f9d5SDimitry Andric if (!SubstDiag->DiagMessage.empty()) 99255e4f9d5SDimitry Andric S.Diag(SubstDiag->DiagLoc, 99355e4f9d5SDimitry Andric diag::note_type_requirement_substitution_error) << (int)First 99455e4f9d5SDimitry Andric << SubstDiag->SubstitutedEntity << SubstDiag->DiagMessage; 99555e4f9d5SDimitry Andric else 99655e4f9d5SDimitry Andric S.Diag(SubstDiag->DiagLoc, 99755e4f9d5SDimitry Andric diag::note_type_requirement_unknown_substitution_error) 99855e4f9d5SDimitry Andric << (int)First << SubstDiag->SubstitutedEntity; 99955e4f9d5SDimitry Andric return; 100055e4f9d5SDimitry Andric } 100155e4f9d5SDimitry Andric default: 100255e4f9d5SDimitry Andric llvm_unreachable("Unknown satisfaction status"); 100355e4f9d5SDimitry Andric return; 100455e4f9d5SDimitry Andric } 100555e4f9d5SDimitry Andric } 1006bdd1243dSDimitry Andric static void diagnoseWellFormedUnsatisfiedConstraintExpr(Sema &S, 1007bdd1243dSDimitry Andric Expr *SubstExpr, 1008bdd1243dSDimitry Andric bool First = true); 100955e4f9d5SDimitry Andric 101055e4f9d5SDimitry Andric static void diagnoseUnsatisfiedRequirement(Sema &S, 101155e4f9d5SDimitry Andric concepts::NestedRequirement *Req, 101255e4f9d5SDimitry Andric bool First) { 1013bdd1243dSDimitry Andric using SubstitutionDiagnostic = std::pair<SourceLocation, StringRef>; 1014bdd1243dSDimitry Andric for (auto &Pair : Req->getConstraintSatisfaction()) { 1015bdd1243dSDimitry Andric if (auto *SubstDiag = Pair.second.dyn_cast<SubstitutionDiagnostic *>()) 1016bdd1243dSDimitry Andric S.Diag(SubstDiag->first, diag::note_nested_requirement_substitution_error) 1017bdd1243dSDimitry Andric << (int)First << Req->getInvalidConstraintEntity() << SubstDiag->second; 101855e4f9d5SDimitry Andric else 1019bdd1243dSDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr( 1020bdd1243dSDimitry Andric S, Pair.second.dyn_cast<Expr *>(), First); 1021bdd1243dSDimitry Andric First = false; 102255e4f9d5SDimitry Andric } 102355e4f9d5SDimitry Andric } 102455e4f9d5SDimitry Andric 1025480093f4SDimitry Andric static void diagnoseWellFormedUnsatisfiedConstraintExpr(Sema &S, 1026480093f4SDimitry Andric Expr *SubstExpr, 1027bdd1243dSDimitry Andric bool First) { 1028480093f4SDimitry Andric SubstExpr = SubstExpr->IgnoreParenImpCasts(); 1029480093f4SDimitry Andric if (BinaryOperator *BO = dyn_cast<BinaryOperator>(SubstExpr)) { 1030480093f4SDimitry Andric switch (BO->getOpcode()) { 1031480093f4SDimitry Andric // These two cases will in practice only be reached when using fold 1032480093f4SDimitry Andric // expressions with || and &&, since otherwise the || and && will have been 1033480093f4SDimitry Andric // broken down into atomic constraints during satisfaction checking. 1034480093f4SDimitry Andric case BO_LOr: 1035480093f4SDimitry Andric // Or evaluated to false - meaning both RHS and LHS evaluated to false. 1036480093f4SDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr(S, BO->getLHS(), First); 1037480093f4SDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr(S, BO->getRHS(), 1038480093f4SDimitry Andric /*First=*/false); 1039480093f4SDimitry Andric return; 1040fe6060f1SDimitry Andric case BO_LAnd: { 1041fe6060f1SDimitry Andric bool LHSSatisfied = 1042fe6060f1SDimitry Andric BO->getLHS()->EvaluateKnownConstInt(S.Context).getBoolValue(); 1043480093f4SDimitry Andric if (LHSSatisfied) { 1044480093f4SDimitry Andric // LHS is true, so RHS must be false. 1045480093f4SDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr(S, BO->getRHS(), First); 1046480093f4SDimitry Andric return; 1047480093f4SDimitry Andric } 1048480093f4SDimitry Andric // LHS is false 1049480093f4SDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr(S, BO->getLHS(), First); 1050480093f4SDimitry Andric 1051480093f4SDimitry Andric // RHS might also be false 1052fe6060f1SDimitry Andric bool RHSSatisfied = 1053fe6060f1SDimitry Andric BO->getRHS()->EvaluateKnownConstInt(S.Context).getBoolValue(); 1054480093f4SDimitry Andric if (!RHSSatisfied) 1055480093f4SDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr(S, BO->getRHS(), 1056480093f4SDimitry Andric /*First=*/false); 1057480093f4SDimitry Andric return; 1058fe6060f1SDimitry Andric } 1059480093f4SDimitry Andric case BO_GE: 1060480093f4SDimitry Andric case BO_LE: 1061480093f4SDimitry Andric case BO_GT: 1062480093f4SDimitry Andric case BO_LT: 1063480093f4SDimitry Andric case BO_EQ: 1064480093f4SDimitry Andric case BO_NE: 1065480093f4SDimitry Andric if (BO->getLHS()->getType()->isIntegerType() && 1066480093f4SDimitry Andric BO->getRHS()->getType()->isIntegerType()) { 1067480093f4SDimitry Andric Expr::EvalResult SimplifiedLHS; 1068480093f4SDimitry Andric Expr::EvalResult SimplifiedRHS; 1069fe6060f1SDimitry Andric BO->getLHS()->EvaluateAsInt(SimplifiedLHS, S.Context, 1070fe6060f1SDimitry Andric Expr::SE_NoSideEffects, 1071fe6060f1SDimitry Andric /*InConstantContext=*/true); 1072fe6060f1SDimitry Andric BO->getRHS()->EvaluateAsInt(SimplifiedRHS, S.Context, 1073fe6060f1SDimitry Andric Expr::SE_NoSideEffects, 1074fe6060f1SDimitry Andric /*InConstantContext=*/true); 1075480093f4SDimitry Andric if (!SimplifiedLHS.Diag && ! SimplifiedRHS.Diag) { 1076480093f4SDimitry Andric S.Diag(SubstExpr->getBeginLoc(), 1077480093f4SDimitry Andric diag::note_atomic_constraint_evaluated_to_false_elaborated) 1078480093f4SDimitry Andric << (int)First << SubstExpr 1079fe6060f1SDimitry Andric << toString(SimplifiedLHS.Val.getInt(), 10) 1080480093f4SDimitry Andric << BinaryOperator::getOpcodeStr(BO->getOpcode()) 1081fe6060f1SDimitry Andric << toString(SimplifiedRHS.Val.getInt(), 10); 1082480093f4SDimitry Andric return; 1083480093f4SDimitry Andric } 1084480093f4SDimitry Andric } 1085480093f4SDimitry Andric break; 1086480093f4SDimitry Andric 1087480093f4SDimitry Andric default: 1088480093f4SDimitry Andric break; 1089480093f4SDimitry Andric } 1090480093f4SDimitry Andric } else if (auto *CSE = dyn_cast<ConceptSpecializationExpr>(SubstExpr)) { 1091480093f4SDimitry Andric if (CSE->getTemplateArgsAsWritten()->NumTemplateArgs == 1) { 1092480093f4SDimitry Andric S.Diag( 1093480093f4SDimitry Andric CSE->getSourceRange().getBegin(), 1094480093f4SDimitry Andric diag:: 1095480093f4SDimitry Andric note_single_arg_concept_specialization_constraint_evaluated_to_false) 1096480093f4SDimitry Andric << (int)First 1097480093f4SDimitry Andric << CSE->getTemplateArgsAsWritten()->arguments()[0].getArgument() 1098480093f4SDimitry Andric << CSE->getNamedConcept(); 1099480093f4SDimitry Andric } else { 1100480093f4SDimitry Andric S.Diag(SubstExpr->getSourceRange().getBegin(), 1101480093f4SDimitry Andric diag::note_concept_specialization_constraint_evaluated_to_false) 1102480093f4SDimitry Andric << (int)First << CSE; 1103480093f4SDimitry Andric } 1104480093f4SDimitry Andric S.DiagnoseUnsatisfiedConstraint(CSE->getSatisfaction()); 1105480093f4SDimitry Andric return; 110655e4f9d5SDimitry Andric } else if (auto *RE = dyn_cast<RequiresExpr>(SubstExpr)) { 1107bdd1243dSDimitry Andric // FIXME: RequiresExpr should store dependent diagnostics. 110855e4f9d5SDimitry Andric for (concepts::Requirement *Req : RE->getRequirements()) 110955e4f9d5SDimitry Andric if (!Req->isDependent() && !Req->isSatisfied()) { 111055e4f9d5SDimitry Andric if (auto *E = dyn_cast<concepts::ExprRequirement>(Req)) 111155e4f9d5SDimitry Andric diagnoseUnsatisfiedRequirement(S, E, First); 111255e4f9d5SDimitry Andric else if (auto *T = dyn_cast<concepts::TypeRequirement>(Req)) 111355e4f9d5SDimitry Andric diagnoseUnsatisfiedRequirement(S, T, First); 111455e4f9d5SDimitry Andric else 111555e4f9d5SDimitry Andric diagnoseUnsatisfiedRequirement( 111655e4f9d5SDimitry Andric S, cast<concepts::NestedRequirement>(Req), First); 111755e4f9d5SDimitry Andric break; 111855e4f9d5SDimitry Andric } 111955e4f9d5SDimitry Andric return; 1120480093f4SDimitry Andric } 1121480093f4SDimitry Andric 1122480093f4SDimitry Andric S.Diag(SubstExpr->getSourceRange().getBegin(), 1123480093f4SDimitry Andric diag::note_atomic_constraint_evaluated_to_false) 1124480093f4SDimitry Andric << (int)First << SubstExpr; 1125480093f4SDimitry Andric } 1126480093f4SDimitry Andric 1127480093f4SDimitry Andric template<typename SubstitutionDiagnostic> 1128480093f4SDimitry Andric static void diagnoseUnsatisfiedConstraintExpr( 1129480093f4SDimitry Andric Sema &S, const Expr *E, 1130480093f4SDimitry Andric const llvm::PointerUnion<Expr *, SubstitutionDiagnostic *> &Record, 1131480093f4SDimitry Andric bool First = true) { 1132480093f4SDimitry Andric if (auto *Diag = Record.template dyn_cast<SubstitutionDiagnostic *>()){ 1133480093f4SDimitry Andric S.Diag(Diag->first, diag::note_substituted_constraint_expr_is_ill_formed) 1134480093f4SDimitry Andric << Diag->second; 1135480093f4SDimitry Andric return; 1136480093f4SDimitry Andric } 1137480093f4SDimitry Andric 1138480093f4SDimitry Andric diagnoseWellFormedUnsatisfiedConstraintExpr(S, 1139480093f4SDimitry Andric Record.template get<Expr *>(), First); 1140480093f4SDimitry Andric } 1141480093f4SDimitry Andric 114255e4f9d5SDimitry Andric void 114355e4f9d5SDimitry Andric Sema::DiagnoseUnsatisfiedConstraint(const ConstraintSatisfaction& Satisfaction, 114455e4f9d5SDimitry Andric bool First) { 1145480093f4SDimitry Andric assert(!Satisfaction.IsSatisfied && 1146480093f4SDimitry Andric "Attempted to diagnose a satisfied constraint"); 1147480093f4SDimitry Andric for (auto &Pair : Satisfaction.Details) { 1148480093f4SDimitry Andric diagnoseUnsatisfiedConstraintExpr(*this, Pair.first, Pair.second, First); 1149480093f4SDimitry Andric First = false; 1150480093f4SDimitry Andric } 1151480093f4SDimitry Andric } 1152480093f4SDimitry Andric 1153480093f4SDimitry Andric void Sema::DiagnoseUnsatisfiedConstraint( 115455e4f9d5SDimitry Andric const ASTConstraintSatisfaction &Satisfaction, 115555e4f9d5SDimitry Andric bool First) { 1156480093f4SDimitry Andric assert(!Satisfaction.IsSatisfied && 1157480093f4SDimitry Andric "Attempted to diagnose a satisfied constraint"); 1158480093f4SDimitry Andric for (auto &Pair : Satisfaction) { 1159480093f4SDimitry Andric diagnoseUnsatisfiedConstraintExpr(*this, Pair.first, Pair.second, First); 1160480093f4SDimitry Andric First = false; 1161480093f4SDimitry Andric } 1162480093f4SDimitry Andric } 1163480093f4SDimitry Andric 1164480093f4SDimitry Andric const NormalizedConstraint * 1165480093f4SDimitry Andric Sema::getNormalizedAssociatedConstraints( 1166480093f4SDimitry Andric NamedDecl *ConstrainedDecl, ArrayRef<const Expr *> AssociatedConstraints) { 1167*06c3fb27SDimitry Andric // In case the ConstrainedDecl comes from modules, it is necessary to use 1168*06c3fb27SDimitry Andric // the canonical decl to avoid different atomic constraints with the 'same' 1169*06c3fb27SDimitry Andric // declarations. 1170*06c3fb27SDimitry Andric ConstrainedDecl = cast<NamedDecl>(ConstrainedDecl->getCanonicalDecl()); 1171*06c3fb27SDimitry Andric 1172480093f4SDimitry Andric auto CacheEntry = NormalizationCache.find(ConstrainedDecl); 1173480093f4SDimitry Andric if (CacheEntry == NormalizationCache.end()) { 1174480093f4SDimitry Andric auto Normalized = 1175480093f4SDimitry Andric NormalizedConstraint::fromConstraintExprs(*this, ConstrainedDecl, 1176480093f4SDimitry Andric AssociatedConstraints); 1177480093f4SDimitry Andric CacheEntry = 1178480093f4SDimitry Andric NormalizationCache 1179480093f4SDimitry Andric .try_emplace(ConstrainedDecl, 1180480093f4SDimitry Andric Normalized 1181480093f4SDimitry Andric ? new (Context) NormalizedConstraint( 1182480093f4SDimitry Andric std::move(*Normalized)) 1183480093f4SDimitry Andric : nullptr) 1184480093f4SDimitry Andric .first; 1185480093f4SDimitry Andric } 1186480093f4SDimitry Andric return CacheEntry->second; 1187480093f4SDimitry Andric } 1188480093f4SDimitry Andric 1189bdd1243dSDimitry Andric static bool 1190bdd1243dSDimitry Andric substituteParameterMappings(Sema &S, NormalizedConstraint &N, 1191bdd1243dSDimitry Andric ConceptDecl *Concept, 1192bdd1243dSDimitry Andric const MultiLevelTemplateArgumentList &MLTAL, 1193480093f4SDimitry Andric const ASTTemplateArgumentListInfo *ArgsAsWritten) { 1194480093f4SDimitry Andric if (!N.isAtomic()) { 1195bdd1243dSDimitry Andric if (substituteParameterMappings(S, N.getLHS(), Concept, MLTAL, 1196480093f4SDimitry Andric ArgsAsWritten)) 1197480093f4SDimitry Andric return true; 1198bdd1243dSDimitry Andric return substituteParameterMappings(S, N.getRHS(), Concept, MLTAL, 1199480093f4SDimitry Andric ArgsAsWritten); 1200480093f4SDimitry Andric } 1201480093f4SDimitry Andric TemplateParameterList *TemplateParams = Concept->getTemplateParameters(); 1202480093f4SDimitry Andric 1203480093f4SDimitry Andric AtomicConstraint &Atomic = *N.getAtomicConstraint(); 1204480093f4SDimitry Andric TemplateArgumentListInfo SubstArgs; 1205480093f4SDimitry Andric if (!Atomic.ParameterMapping) { 1206480093f4SDimitry Andric llvm::SmallBitVector OccurringIndices(TemplateParams->size()); 1207480093f4SDimitry Andric S.MarkUsedTemplateParameters(Atomic.ConstraintExpr, /*OnlyDeduced=*/false, 1208480093f4SDimitry Andric /*Depth=*/0, OccurringIndices); 1209bdd1243dSDimitry Andric TemplateArgumentLoc *TempArgs = 1210bdd1243dSDimitry Andric new (S.Context) TemplateArgumentLoc[OccurringIndices.count()]; 1211480093f4SDimitry Andric for (unsigned I = 0, J = 0, C = TemplateParams->size(); I != C; ++I) 1212480093f4SDimitry Andric if (OccurringIndices[I]) 1213bdd1243dSDimitry Andric new (&(TempArgs)[J++]) 1214bdd1243dSDimitry Andric TemplateArgumentLoc(S.getIdentityTemplateArgumentLoc( 1215bdd1243dSDimitry Andric TemplateParams->begin()[I], 1216480093f4SDimitry Andric // Here we assume we do not support things like 1217480093f4SDimitry Andric // template<typename A, typename B> 1218480093f4SDimitry Andric // concept C = ...; 1219480093f4SDimitry Andric // 1220480093f4SDimitry Andric // template<typename... Ts> requires C<Ts...> 1221480093f4SDimitry Andric // struct S { }; 1222480093f4SDimitry Andric // The above currently yields a diagnostic. 1223480093f4SDimitry Andric // We still might have default arguments for concept parameters. 1224bdd1243dSDimitry Andric ArgsAsWritten->NumTemplateArgs > I 1225bdd1243dSDimitry Andric ? ArgsAsWritten->arguments()[I].getLocation() 1226bdd1243dSDimitry Andric : SourceLocation())); 1227bdd1243dSDimitry Andric Atomic.ParameterMapping.emplace(TempArgs, OccurringIndices.count()); 1228480093f4SDimitry Andric } 1229480093f4SDimitry Andric Sema::InstantiatingTemplate Inst( 1230480093f4SDimitry Andric S, ArgsAsWritten->arguments().front().getSourceRange().getBegin(), 1231480093f4SDimitry Andric Sema::InstantiatingTemplate::ParameterMappingSubstitution{}, Concept, 12321ac55f4cSDimitry Andric ArgsAsWritten->arguments().front().getSourceRange()); 1233480093f4SDimitry Andric if (S.SubstTemplateArguments(*Atomic.ParameterMapping, MLTAL, SubstArgs)) 1234480093f4SDimitry Andric return true; 1235bdd1243dSDimitry Andric 1236bdd1243dSDimitry Andric TemplateArgumentLoc *TempArgs = 1237bdd1243dSDimitry Andric new (S.Context) TemplateArgumentLoc[SubstArgs.size()]; 1238480093f4SDimitry Andric std::copy(SubstArgs.arguments().begin(), SubstArgs.arguments().end(), 1239bdd1243dSDimitry Andric TempArgs); 1240bdd1243dSDimitry Andric Atomic.ParameterMapping.emplace(TempArgs, SubstArgs.size()); 1241480093f4SDimitry Andric return false; 1242480093f4SDimitry Andric } 1243480093f4SDimitry Andric 1244bdd1243dSDimitry Andric static bool substituteParameterMappings(Sema &S, NormalizedConstraint &N, 1245bdd1243dSDimitry Andric const ConceptSpecializationExpr *CSE) { 1246bdd1243dSDimitry Andric TemplateArgumentList TAL{TemplateArgumentList::OnStack, 1247bdd1243dSDimitry Andric CSE->getTemplateArguments()}; 1248bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL = S.getTemplateInstantiationArgs( 1249bdd1243dSDimitry Andric CSE->getNamedConcept(), /*Final=*/false, &TAL, 1250bdd1243dSDimitry Andric /*RelativeToPrimary=*/true, 1251bdd1243dSDimitry Andric /*Pattern=*/nullptr, 1252bdd1243dSDimitry Andric /*ForConstraintInstantiation=*/true); 1253bdd1243dSDimitry Andric 1254bdd1243dSDimitry Andric return substituteParameterMappings(S, N, CSE->getNamedConcept(), MLTAL, 1255bdd1243dSDimitry Andric CSE->getTemplateArgsAsWritten()); 1256bdd1243dSDimitry Andric } 1257bdd1243dSDimitry Andric 1258bdd1243dSDimitry Andric std::optional<NormalizedConstraint> 1259480093f4SDimitry Andric NormalizedConstraint::fromConstraintExprs(Sema &S, NamedDecl *D, 1260480093f4SDimitry Andric ArrayRef<const Expr *> E) { 1261480093f4SDimitry Andric assert(E.size() != 0); 12626e75b2fbSDimitry Andric auto Conjunction = fromConstraintExpr(S, D, E[0]); 12636e75b2fbSDimitry Andric if (!Conjunction) 1264bdd1243dSDimitry Andric return std::nullopt; 12656e75b2fbSDimitry Andric for (unsigned I = 1; I < E.size(); ++I) { 1266480093f4SDimitry Andric auto Next = fromConstraintExpr(S, D, E[I]); 1267480093f4SDimitry Andric if (!Next) 1268bdd1243dSDimitry Andric return std::nullopt; 12696e75b2fbSDimitry Andric *Conjunction = NormalizedConstraint(S.Context, std::move(*Conjunction), 1270480093f4SDimitry Andric std::move(*Next), CCK_Conjunction); 1271480093f4SDimitry Andric } 1272480093f4SDimitry Andric return Conjunction; 1273480093f4SDimitry Andric } 1274480093f4SDimitry Andric 1275bdd1243dSDimitry Andric std::optional<NormalizedConstraint> 1276480093f4SDimitry Andric NormalizedConstraint::fromConstraintExpr(Sema &S, NamedDecl *D, const Expr *E) { 1277480093f4SDimitry Andric assert(E != nullptr); 1278480093f4SDimitry Andric 1279480093f4SDimitry Andric // C++ [temp.constr.normal]p1.1 1280480093f4SDimitry Andric // [...] 1281480093f4SDimitry Andric // - The normal form of an expression (E) is the normal form of E. 1282480093f4SDimitry Andric // [...] 1283480093f4SDimitry Andric E = E->IgnoreParenImpCasts(); 1284bdd1243dSDimitry Andric 1285bdd1243dSDimitry Andric // C++2a [temp.param]p4: 1286bdd1243dSDimitry Andric // [...] If T is not a pack, then E is E', otherwise E is (E' && ...). 1287bdd1243dSDimitry Andric // Fold expression is considered atomic constraints per current wording. 1288bdd1243dSDimitry Andric // See http://cplusplus.github.io/concepts-ts/ts-active.html#28 1289bdd1243dSDimitry Andric 12905ffd83dbSDimitry Andric if (LogicalBinOp BO = E) { 12915ffd83dbSDimitry Andric auto LHS = fromConstraintExpr(S, D, BO.getLHS()); 1292480093f4SDimitry Andric if (!LHS) 1293bdd1243dSDimitry Andric return std::nullopt; 12945ffd83dbSDimitry Andric auto RHS = fromConstraintExpr(S, D, BO.getRHS()); 1295480093f4SDimitry Andric if (!RHS) 1296bdd1243dSDimitry Andric return std::nullopt; 1297480093f4SDimitry Andric 12985ffd83dbSDimitry Andric return NormalizedConstraint(S.Context, std::move(*LHS), std::move(*RHS), 12995ffd83dbSDimitry Andric BO.isAnd() ? CCK_Conjunction : CCK_Disjunction); 1300480093f4SDimitry Andric } else if (auto *CSE = dyn_cast<const ConceptSpecializationExpr>(E)) { 1301480093f4SDimitry Andric const NormalizedConstraint *SubNF; 1302480093f4SDimitry Andric { 1303480093f4SDimitry Andric Sema::InstantiatingTemplate Inst( 1304480093f4SDimitry Andric S, CSE->getExprLoc(), 1305480093f4SDimitry Andric Sema::InstantiatingTemplate::ConstraintNormalization{}, D, 1306480093f4SDimitry Andric CSE->getSourceRange()); 1307480093f4SDimitry Andric // C++ [temp.constr.normal]p1.1 1308480093f4SDimitry Andric // [...] 1309480093f4SDimitry Andric // The normal form of an id-expression of the form C<A1, A2, ..., AN>, 1310480093f4SDimitry Andric // where C names a concept, is the normal form of the 1311480093f4SDimitry Andric // constraint-expression of C, after substituting A1, A2, ..., AN for C’s 1312480093f4SDimitry Andric // respective template parameters in the parameter mappings in each atomic 1313480093f4SDimitry Andric // constraint. If any such substitution results in an invalid type or 1314480093f4SDimitry Andric // expression, the program is ill-formed; no diagnostic is required. 1315480093f4SDimitry Andric // [...] 1316480093f4SDimitry Andric ConceptDecl *CD = CSE->getNamedConcept(); 1317480093f4SDimitry Andric SubNF = S.getNormalizedAssociatedConstraints(CD, 1318480093f4SDimitry Andric {CD->getConstraintExpr()}); 1319480093f4SDimitry Andric if (!SubNF) 1320bdd1243dSDimitry Andric return std::nullopt; 1321480093f4SDimitry Andric } 1322480093f4SDimitry Andric 1323bdd1243dSDimitry Andric std::optional<NormalizedConstraint> New; 1324480093f4SDimitry Andric New.emplace(S.Context, *SubNF); 1325480093f4SDimitry Andric 1326bdd1243dSDimitry Andric if (substituteParameterMappings(S, *New, CSE)) 1327bdd1243dSDimitry Andric return std::nullopt; 1328480093f4SDimitry Andric 1329480093f4SDimitry Andric return New; 1330480093f4SDimitry Andric } 1331480093f4SDimitry Andric return NormalizedConstraint{new (S.Context) AtomicConstraint(S, E)}; 1332480093f4SDimitry Andric } 1333480093f4SDimitry Andric 1334480093f4SDimitry Andric using NormalForm = 1335480093f4SDimitry Andric llvm::SmallVector<llvm::SmallVector<AtomicConstraint *, 2>, 4>; 1336480093f4SDimitry Andric 1337480093f4SDimitry Andric static NormalForm makeCNF(const NormalizedConstraint &Normalized) { 1338480093f4SDimitry Andric if (Normalized.isAtomic()) 1339480093f4SDimitry Andric return {{Normalized.getAtomicConstraint()}}; 1340480093f4SDimitry Andric 1341480093f4SDimitry Andric NormalForm LCNF = makeCNF(Normalized.getLHS()); 1342480093f4SDimitry Andric NormalForm RCNF = makeCNF(Normalized.getRHS()); 1343480093f4SDimitry Andric if (Normalized.getCompoundKind() == NormalizedConstraint::CCK_Conjunction) { 1344480093f4SDimitry Andric LCNF.reserve(LCNF.size() + RCNF.size()); 1345480093f4SDimitry Andric while (!RCNF.empty()) 1346480093f4SDimitry Andric LCNF.push_back(RCNF.pop_back_val()); 1347480093f4SDimitry Andric return LCNF; 1348480093f4SDimitry Andric } 1349480093f4SDimitry Andric 1350480093f4SDimitry Andric // Disjunction 1351480093f4SDimitry Andric NormalForm Res; 1352480093f4SDimitry Andric Res.reserve(LCNF.size() * RCNF.size()); 1353480093f4SDimitry Andric for (auto &LDisjunction : LCNF) 1354480093f4SDimitry Andric for (auto &RDisjunction : RCNF) { 1355480093f4SDimitry Andric NormalForm::value_type Combined; 1356480093f4SDimitry Andric Combined.reserve(LDisjunction.size() + RDisjunction.size()); 1357480093f4SDimitry Andric std::copy(LDisjunction.begin(), LDisjunction.end(), 1358480093f4SDimitry Andric std::back_inserter(Combined)); 1359480093f4SDimitry Andric std::copy(RDisjunction.begin(), RDisjunction.end(), 1360480093f4SDimitry Andric std::back_inserter(Combined)); 1361480093f4SDimitry Andric Res.emplace_back(Combined); 1362480093f4SDimitry Andric } 1363480093f4SDimitry Andric return Res; 1364480093f4SDimitry Andric } 1365480093f4SDimitry Andric 1366480093f4SDimitry Andric static NormalForm makeDNF(const NormalizedConstraint &Normalized) { 1367480093f4SDimitry Andric if (Normalized.isAtomic()) 1368480093f4SDimitry Andric return {{Normalized.getAtomicConstraint()}}; 1369480093f4SDimitry Andric 1370480093f4SDimitry Andric NormalForm LDNF = makeDNF(Normalized.getLHS()); 1371480093f4SDimitry Andric NormalForm RDNF = makeDNF(Normalized.getRHS()); 1372480093f4SDimitry Andric if (Normalized.getCompoundKind() == NormalizedConstraint::CCK_Disjunction) { 1373480093f4SDimitry Andric LDNF.reserve(LDNF.size() + RDNF.size()); 1374480093f4SDimitry Andric while (!RDNF.empty()) 1375480093f4SDimitry Andric LDNF.push_back(RDNF.pop_back_val()); 1376480093f4SDimitry Andric return LDNF; 1377480093f4SDimitry Andric } 1378480093f4SDimitry Andric 1379480093f4SDimitry Andric // Conjunction 1380480093f4SDimitry Andric NormalForm Res; 1381480093f4SDimitry Andric Res.reserve(LDNF.size() * RDNF.size()); 1382480093f4SDimitry Andric for (auto &LConjunction : LDNF) { 1383480093f4SDimitry Andric for (auto &RConjunction : RDNF) { 1384480093f4SDimitry Andric NormalForm::value_type Combined; 1385480093f4SDimitry Andric Combined.reserve(LConjunction.size() + RConjunction.size()); 1386480093f4SDimitry Andric std::copy(LConjunction.begin(), LConjunction.end(), 1387480093f4SDimitry Andric std::back_inserter(Combined)); 1388480093f4SDimitry Andric std::copy(RConjunction.begin(), RConjunction.end(), 1389480093f4SDimitry Andric std::back_inserter(Combined)); 1390480093f4SDimitry Andric Res.emplace_back(Combined); 1391480093f4SDimitry Andric } 1392480093f4SDimitry Andric } 1393480093f4SDimitry Andric return Res; 1394480093f4SDimitry Andric } 1395480093f4SDimitry Andric 1396480093f4SDimitry Andric template<typename AtomicSubsumptionEvaluator> 1397*06c3fb27SDimitry Andric static bool subsumes(const NormalForm &PDNF, const NormalForm &QCNF, 1398480093f4SDimitry Andric AtomicSubsumptionEvaluator E) { 1399480093f4SDimitry Andric // C++ [temp.constr.order] p2 1400480093f4SDimitry Andric // Then, P subsumes Q if and only if, for every disjunctive clause Pi in the 1401480093f4SDimitry Andric // disjunctive normal form of P, Pi subsumes every conjunctive clause Qj in 1402480093f4SDimitry Andric // the conjuctive normal form of Q, where [...] 1403480093f4SDimitry Andric for (const auto &Pi : PDNF) { 1404480093f4SDimitry Andric for (const auto &Qj : QCNF) { 1405480093f4SDimitry Andric // C++ [temp.constr.order] p2 1406480093f4SDimitry Andric // - [...] a disjunctive clause Pi subsumes a conjunctive clause Qj if 1407480093f4SDimitry Andric // and only if there exists an atomic constraint Pia in Pi for which 1408480093f4SDimitry Andric // there exists an atomic constraint, Qjb, in Qj such that Pia 1409480093f4SDimitry Andric // subsumes Qjb. 1410480093f4SDimitry Andric bool Found = false; 1411480093f4SDimitry Andric for (const AtomicConstraint *Pia : Pi) { 1412480093f4SDimitry Andric for (const AtomicConstraint *Qjb : Qj) { 1413480093f4SDimitry Andric if (E(*Pia, *Qjb)) { 1414480093f4SDimitry Andric Found = true; 1415480093f4SDimitry Andric break; 1416480093f4SDimitry Andric } 1417480093f4SDimitry Andric } 1418480093f4SDimitry Andric if (Found) 1419480093f4SDimitry Andric break; 1420480093f4SDimitry Andric } 1421480093f4SDimitry Andric if (!Found) 1422480093f4SDimitry Andric return false; 1423480093f4SDimitry Andric } 1424480093f4SDimitry Andric } 1425480093f4SDimitry Andric return true; 1426480093f4SDimitry Andric } 1427480093f4SDimitry Andric 1428480093f4SDimitry Andric template<typename AtomicSubsumptionEvaluator> 1429480093f4SDimitry Andric static bool subsumes(Sema &S, NamedDecl *DP, ArrayRef<const Expr *> P, 1430480093f4SDimitry Andric NamedDecl *DQ, ArrayRef<const Expr *> Q, bool &Subsumes, 1431480093f4SDimitry Andric AtomicSubsumptionEvaluator E) { 1432480093f4SDimitry Andric // C++ [temp.constr.order] p2 1433480093f4SDimitry Andric // In order to determine if a constraint P subsumes a constraint Q, P is 1434480093f4SDimitry Andric // transformed into disjunctive normal form, and Q is transformed into 1435480093f4SDimitry Andric // conjunctive normal form. [...] 1436480093f4SDimitry Andric auto *PNormalized = S.getNormalizedAssociatedConstraints(DP, P); 1437480093f4SDimitry Andric if (!PNormalized) 1438480093f4SDimitry Andric return true; 1439480093f4SDimitry Andric const NormalForm PDNF = makeDNF(*PNormalized); 1440480093f4SDimitry Andric 1441480093f4SDimitry Andric auto *QNormalized = S.getNormalizedAssociatedConstraints(DQ, Q); 1442480093f4SDimitry Andric if (!QNormalized) 1443480093f4SDimitry Andric return true; 1444480093f4SDimitry Andric const NormalForm QCNF = makeCNF(*QNormalized); 1445480093f4SDimitry Andric 1446480093f4SDimitry Andric Subsumes = subsumes(PDNF, QCNF, E); 1447480093f4SDimitry Andric return false; 1448480093f4SDimitry Andric } 1449480093f4SDimitry Andric 1450bdd1243dSDimitry Andric bool Sema::IsAtLeastAsConstrained(NamedDecl *D1, 1451bdd1243dSDimitry Andric MutableArrayRef<const Expr *> AC1, 1452bdd1243dSDimitry Andric NamedDecl *D2, 1453bdd1243dSDimitry Andric MutableArrayRef<const Expr *> AC2, 1454480093f4SDimitry Andric bool &Result) { 1455bdd1243dSDimitry Andric if (const auto *FD1 = dyn_cast<FunctionDecl>(D1)) { 1456bdd1243dSDimitry Andric auto IsExpectedEntity = [](const FunctionDecl *FD) { 1457bdd1243dSDimitry Andric FunctionDecl::TemplatedKind Kind = FD->getTemplatedKind(); 1458bdd1243dSDimitry Andric return Kind == FunctionDecl::TK_NonTemplate || 1459bdd1243dSDimitry Andric Kind == FunctionDecl::TK_FunctionTemplate; 1460bdd1243dSDimitry Andric }; 1461bdd1243dSDimitry Andric const auto *FD2 = dyn_cast<FunctionDecl>(D2); 1462bdd1243dSDimitry Andric (void)IsExpectedEntity; 1463bdd1243dSDimitry Andric (void)FD1; 1464bdd1243dSDimitry Andric (void)FD2; 1465bdd1243dSDimitry Andric assert(IsExpectedEntity(FD1) && FD2 && IsExpectedEntity(FD2) && 1466bdd1243dSDimitry Andric "use non-instantiated function declaration for constraints partial " 1467bdd1243dSDimitry Andric "ordering"); 1468bdd1243dSDimitry Andric } 1469bdd1243dSDimitry Andric 1470480093f4SDimitry Andric if (AC1.empty()) { 1471480093f4SDimitry Andric Result = AC2.empty(); 1472480093f4SDimitry Andric return false; 1473480093f4SDimitry Andric } 1474480093f4SDimitry Andric if (AC2.empty()) { 1475480093f4SDimitry Andric // TD1 has associated constraints and TD2 does not. 1476480093f4SDimitry Andric Result = true; 1477480093f4SDimitry Andric return false; 1478480093f4SDimitry Andric } 1479480093f4SDimitry Andric 1480480093f4SDimitry Andric std::pair<NamedDecl *, NamedDecl *> Key{D1, D2}; 1481480093f4SDimitry Andric auto CacheEntry = SubsumptionCache.find(Key); 1482480093f4SDimitry Andric if (CacheEntry != SubsumptionCache.end()) { 1483480093f4SDimitry Andric Result = CacheEntry->second; 1484480093f4SDimitry Andric return false; 1485480093f4SDimitry Andric } 1486480093f4SDimitry Andric 1487bdd1243dSDimitry Andric unsigned Depth1 = CalculateTemplateDepthForConstraints(*this, D1, true); 1488bdd1243dSDimitry Andric unsigned Depth2 = CalculateTemplateDepthForConstraints(*this, D2, true); 1489bdd1243dSDimitry Andric 1490bdd1243dSDimitry Andric for (size_t I = 0; I != AC1.size() && I != AC2.size(); ++I) { 1491bdd1243dSDimitry Andric if (Depth2 > Depth1) { 1492bdd1243dSDimitry Andric AC1[I] = AdjustConstraintDepth(*this, Depth2 - Depth1) 1493bdd1243dSDimitry Andric .TransformExpr(const_cast<Expr *>(AC1[I])) 1494bdd1243dSDimitry Andric .get(); 1495bdd1243dSDimitry Andric } else if (Depth1 > Depth2) { 1496bdd1243dSDimitry Andric AC2[I] = AdjustConstraintDepth(*this, Depth1 - Depth2) 1497bdd1243dSDimitry Andric .TransformExpr(const_cast<Expr *>(AC2[I])) 1498bdd1243dSDimitry Andric .get(); 1499bdd1243dSDimitry Andric } 1500bdd1243dSDimitry Andric } 1501bdd1243dSDimitry Andric 1502480093f4SDimitry Andric if (subsumes(*this, D1, AC1, D2, AC2, Result, 1503480093f4SDimitry Andric [this] (const AtomicConstraint &A, const AtomicConstraint &B) { 1504480093f4SDimitry Andric return A.subsumes(Context, B); 1505480093f4SDimitry Andric })) 1506480093f4SDimitry Andric return true; 1507480093f4SDimitry Andric SubsumptionCache.try_emplace(Key, Result); 1508480093f4SDimitry Andric return false; 1509480093f4SDimitry Andric } 1510480093f4SDimitry Andric 1511480093f4SDimitry Andric bool Sema::MaybeEmitAmbiguousAtomicConstraintsDiagnostic(NamedDecl *D1, 1512480093f4SDimitry Andric ArrayRef<const Expr *> AC1, NamedDecl *D2, ArrayRef<const Expr *> AC2) { 1513480093f4SDimitry Andric if (isSFINAEContext()) 1514480093f4SDimitry Andric // No need to work here because our notes would be discarded. 1515480093f4SDimitry Andric return false; 1516480093f4SDimitry Andric 1517480093f4SDimitry Andric if (AC1.empty() || AC2.empty()) 1518480093f4SDimitry Andric return false; 1519480093f4SDimitry Andric 1520480093f4SDimitry Andric auto NormalExprEvaluator = 1521480093f4SDimitry Andric [this] (const AtomicConstraint &A, const AtomicConstraint &B) { 1522480093f4SDimitry Andric return A.subsumes(Context, B); 1523480093f4SDimitry Andric }; 1524480093f4SDimitry Andric 1525480093f4SDimitry Andric const Expr *AmbiguousAtomic1 = nullptr, *AmbiguousAtomic2 = nullptr; 1526480093f4SDimitry Andric auto IdenticalExprEvaluator = 1527480093f4SDimitry Andric [&] (const AtomicConstraint &A, const AtomicConstraint &B) { 1528480093f4SDimitry Andric if (!A.hasMatchingParameterMapping(Context, B)) 1529480093f4SDimitry Andric return false; 1530480093f4SDimitry Andric const Expr *EA = A.ConstraintExpr, *EB = B.ConstraintExpr; 1531480093f4SDimitry Andric if (EA == EB) 1532480093f4SDimitry Andric return true; 1533480093f4SDimitry Andric 1534480093f4SDimitry Andric // Not the same source level expression - are the expressions 1535480093f4SDimitry Andric // identical? 1536480093f4SDimitry Andric llvm::FoldingSetNodeID IDA, IDB; 1537349cc55cSDimitry Andric EA->Profile(IDA, Context, /*Canonical=*/true); 1538349cc55cSDimitry Andric EB->Profile(IDB, Context, /*Canonical=*/true); 1539480093f4SDimitry Andric if (IDA != IDB) 1540480093f4SDimitry Andric return false; 1541480093f4SDimitry Andric 1542480093f4SDimitry Andric AmbiguousAtomic1 = EA; 1543480093f4SDimitry Andric AmbiguousAtomic2 = EB; 1544480093f4SDimitry Andric return true; 1545480093f4SDimitry Andric }; 1546480093f4SDimitry Andric 1547480093f4SDimitry Andric { 1548480093f4SDimitry Andric // The subsumption checks might cause diagnostics 1549480093f4SDimitry Andric SFINAETrap Trap(*this); 1550480093f4SDimitry Andric auto *Normalized1 = getNormalizedAssociatedConstraints(D1, AC1); 1551480093f4SDimitry Andric if (!Normalized1) 1552480093f4SDimitry Andric return false; 1553480093f4SDimitry Andric const NormalForm DNF1 = makeDNF(*Normalized1); 1554480093f4SDimitry Andric const NormalForm CNF1 = makeCNF(*Normalized1); 1555480093f4SDimitry Andric 1556480093f4SDimitry Andric auto *Normalized2 = getNormalizedAssociatedConstraints(D2, AC2); 1557480093f4SDimitry Andric if (!Normalized2) 1558480093f4SDimitry Andric return false; 1559480093f4SDimitry Andric const NormalForm DNF2 = makeDNF(*Normalized2); 1560480093f4SDimitry Andric const NormalForm CNF2 = makeCNF(*Normalized2); 1561480093f4SDimitry Andric 1562480093f4SDimitry Andric bool Is1AtLeastAs2Normally = subsumes(DNF1, CNF2, NormalExprEvaluator); 1563480093f4SDimitry Andric bool Is2AtLeastAs1Normally = subsumes(DNF2, CNF1, NormalExprEvaluator); 1564480093f4SDimitry Andric bool Is1AtLeastAs2 = subsumes(DNF1, CNF2, IdenticalExprEvaluator); 1565480093f4SDimitry Andric bool Is2AtLeastAs1 = subsumes(DNF2, CNF1, IdenticalExprEvaluator); 1566480093f4SDimitry Andric if (Is1AtLeastAs2 == Is1AtLeastAs2Normally && 1567480093f4SDimitry Andric Is2AtLeastAs1 == Is2AtLeastAs1Normally) 1568480093f4SDimitry Andric // Same result - no ambiguity was caused by identical atomic expressions. 1569480093f4SDimitry Andric return false; 1570480093f4SDimitry Andric } 1571480093f4SDimitry Andric 1572480093f4SDimitry Andric // A different result! Some ambiguous atomic constraint(s) caused a difference 1573480093f4SDimitry Andric assert(AmbiguousAtomic1 && AmbiguousAtomic2); 1574480093f4SDimitry Andric 1575480093f4SDimitry Andric Diag(AmbiguousAtomic1->getBeginLoc(), diag::note_ambiguous_atomic_constraints) 1576480093f4SDimitry Andric << AmbiguousAtomic1->getSourceRange(); 1577480093f4SDimitry Andric Diag(AmbiguousAtomic2->getBeginLoc(), 1578480093f4SDimitry Andric diag::note_ambiguous_atomic_constraints_similar_expression) 1579480093f4SDimitry Andric << AmbiguousAtomic2->getSourceRange(); 1580480093f4SDimitry Andric return true; 1581480093f4SDimitry Andric } 158255e4f9d5SDimitry Andric 158355e4f9d5SDimitry Andric concepts::ExprRequirement::ExprRequirement( 158455e4f9d5SDimitry Andric Expr *E, bool IsSimple, SourceLocation NoexceptLoc, 158555e4f9d5SDimitry Andric ReturnTypeRequirement Req, SatisfactionStatus Status, 158655e4f9d5SDimitry Andric ConceptSpecializationExpr *SubstitutedConstraintExpr) : 158755e4f9d5SDimitry Andric Requirement(IsSimple ? RK_Simple : RK_Compound, Status == SS_Dependent, 158855e4f9d5SDimitry Andric Status == SS_Dependent && 158955e4f9d5SDimitry Andric (E->containsUnexpandedParameterPack() || 159055e4f9d5SDimitry Andric Req.containsUnexpandedParameterPack()), 159155e4f9d5SDimitry Andric Status == SS_Satisfied), Value(E), NoexceptLoc(NoexceptLoc), 159255e4f9d5SDimitry Andric TypeReq(Req), SubstitutedConstraintExpr(SubstitutedConstraintExpr), 159355e4f9d5SDimitry Andric Status(Status) { 159455e4f9d5SDimitry Andric assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) && 159555e4f9d5SDimitry Andric "Simple requirement must not have a return type requirement or a " 159655e4f9d5SDimitry Andric "noexcept specification"); 159755e4f9d5SDimitry Andric assert((Status > SS_TypeRequirementSubstitutionFailure && Req.isTypeConstraint()) == 159855e4f9d5SDimitry Andric (SubstitutedConstraintExpr != nullptr)); 159955e4f9d5SDimitry Andric } 160055e4f9d5SDimitry Andric 160155e4f9d5SDimitry Andric concepts::ExprRequirement::ExprRequirement( 160255e4f9d5SDimitry Andric SubstitutionDiagnostic *ExprSubstDiag, bool IsSimple, 160355e4f9d5SDimitry Andric SourceLocation NoexceptLoc, ReturnTypeRequirement Req) : 160455e4f9d5SDimitry Andric Requirement(IsSimple ? RK_Simple : RK_Compound, Req.isDependent(), 160555e4f9d5SDimitry Andric Req.containsUnexpandedParameterPack(), /*IsSatisfied=*/false), 160655e4f9d5SDimitry Andric Value(ExprSubstDiag), NoexceptLoc(NoexceptLoc), TypeReq(Req), 160755e4f9d5SDimitry Andric Status(SS_ExprSubstitutionFailure) { 160855e4f9d5SDimitry Andric assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) && 160955e4f9d5SDimitry Andric "Simple requirement must not have a return type requirement or a " 161055e4f9d5SDimitry Andric "noexcept specification"); 161155e4f9d5SDimitry Andric } 161255e4f9d5SDimitry Andric 161355e4f9d5SDimitry Andric concepts::ExprRequirement::ReturnTypeRequirement:: 161455e4f9d5SDimitry Andric ReturnTypeRequirement(TemplateParameterList *TPL) : 161504eeddc0SDimitry Andric TypeConstraintInfo(TPL, false) { 161655e4f9d5SDimitry Andric assert(TPL->size() == 1); 161755e4f9d5SDimitry Andric const TypeConstraint *TC = 161855e4f9d5SDimitry Andric cast<TemplateTypeParmDecl>(TPL->getParam(0))->getTypeConstraint(); 161955e4f9d5SDimitry Andric assert(TC && 162055e4f9d5SDimitry Andric "TPL must have a template type parameter with a type constraint"); 162155e4f9d5SDimitry Andric auto *Constraint = 1622349cc55cSDimitry Andric cast<ConceptSpecializationExpr>(TC->getImmediatelyDeclaredConstraint()); 1623e8d8bef9SDimitry Andric bool Dependent = 1624e8d8bef9SDimitry Andric Constraint->getTemplateArgsAsWritten() && 1625e8d8bef9SDimitry Andric TemplateSpecializationType::anyInstantiationDependentTemplateArguments( 1626e8d8bef9SDimitry Andric Constraint->getTemplateArgsAsWritten()->arguments().drop_front(1)); 162704eeddc0SDimitry Andric TypeConstraintInfo.setInt(Dependent ? true : false); 162855e4f9d5SDimitry Andric } 162955e4f9d5SDimitry Andric 163055e4f9d5SDimitry Andric concepts::TypeRequirement::TypeRequirement(TypeSourceInfo *T) : 1631e8d8bef9SDimitry Andric Requirement(RK_Type, T->getType()->isInstantiationDependentType(), 163255e4f9d5SDimitry Andric T->getType()->containsUnexpandedParameterPack(), 163355e4f9d5SDimitry Andric // We reach this ctor with either dependent types (in which 163455e4f9d5SDimitry Andric // IsSatisfied doesn't matter) or with non-dependent type in 163555e4f9d5SDimitry Andric // which the existence of the type indicates satisfaction. 1636e8d8bef9SDimitry Andric /*IsSatisfied=*/true), 1637e8d8bef9SDimitry Andric Value(T), 1638e8d8bef9SDimitry Andric Status(T->getType()->isInstantiationDependentType() ? SS_Dependent 1639e8d8bef9SDimitry Andric : SS_Satisfied) {} 1640