10b57cec5SDimitry Andric //===-- SemaCoroutine.cpp - Semantic Analysis for Coroutines --------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements semantic analysis for C++ Coroutines. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric // This file contains references to sections of the Coroutines TS, which 120b57cec5SDimitry Andric // can be found at http://wg21.link/coroutines. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "CoroutineStmtBuilder.h" 170b57cec5SDimitry Andric #include "clang/AST/ASTLambda.h" 180b57cec5SDimitry Andric #include "clang/AST/Decl.h" 197a6dacacSDimitry Andric #include "clang/AST/Expr.h" 200b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 210b57cec5SDimitry Andric #include "clang/AST/StmtCXX.h" 22480093f4SDimitry Andric #include "clang/Basic/Builtins.h" 230b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 247a6dacacSDimitry Andric #include "clang/Sema/EnterExpressionEvaluationContext.h" 250b57cec5SDimitry Andric #include "clang/Sema/Initialization.h" 260b57cec5SDimitry Andric #include "clang/Sema/Overload.h" 270b57cec5SDimitry Andric #include "clang/Sema/ScopeInfo.h" 280b57cec5SDimitry Andric #include "clang/Sema/SemaInternal.h" 295ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h" 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric using namespace clang; 320b57cec5SDimitry Andric using namespace sema; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, 350b57cec5SDimitry Andric SourceLocation Loc, bool &Res) { 360b57cec5SDimitry Andric DeclarationName DN = S.PP.getIdentifierInfo(Name); 370b57cec5SDimitry Andric LookupResult LR(S, DN, Loc, Sema::LookupMemberName); 380b57cec5SDimitry Andric // Suppress diagnostics when a private member is selected. The same warnings 390b57cec5SDimitry Andric // will be produced again when building the call. 400b57cec5SDimitry Andric LR.suppressDiagnostics(); 410b57cec5SDimitry Andric Res = S.LookupQualifiedName(LR, RD); 420b57cec5SDimitry Andric return LR; 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, 460b57cec5SDimitry Andric SourceLocation Loc) { 470b57cec5SDimitry Andric bool Res; 480b57cec5SDimitry Andric lookupMember(S, Name, RD, Loc, Res); 490b57cec5SDimitry Andric return Res; 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric /// Look up the std::coroutine_traits<...>::promise_type for the given 530b57cec5SDimitry Andric /// function type. 540b57cec5SDimitry Andric static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD, 550b57cec5SDimitry Andric SourceLocation KwLoc) { 560b57cec5SDimitry Andric const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>(); 570b57cec5SDimitry Andric const SourceLocation FuncLoc = FD->getLocation(); 580b57cec5SDimitry Andric 59349cc55cSDimitry Andric ClassTemplateDecl *CoroTraits = 6006c3fb27SDimitry Andric S.lookupCoroutineTraits(KwLoc, FuncLoc); 6106c3fb27SDimitry Andric if (!CoroTraits) 620b57cec5SDimitry Andric return QualType(); 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric // Form template argument list for coroutine_traits<R, P1, P2, ...> according 650b57cec5SDimitry Andric // to [dcl.fct.def.coroutine]3 660b57cec5SDimitry Andric TemplateArgumentListInfo Args(KwLoc, KwLoc); 670b57cec5SDimitry Andric auto AddArg = [&](QualType T) { 680b57cec5SDimitry Andric Args.addArgument(TemplateArgumentLoc( 690b57cec5SDimitry Andric TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc))); 700b57cec5SDimitry Andric }; 710b57cec5SDimitry Andric AddArg(FnType->getReturnType()); 720b57cec5SDimitry Andric // If the function is a non-static member function, add the type 730b57cec5SDimitry Andric // of the implicit object parameter before the formal parameters. 740b57cec5SDimitry Andric if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { 755f757f3fSDimitry Andric if (MD->isImplicitObjectMemberFunction()) { 760b57cec5SDimitry Andric // [over.match.funcs]4 770b57cec5SDimitry Andric // For non-static member functions, the type of the implicit object 780b57cec5SDimitry Andric // parameter is 790b57cec5SDimitry Andric // -- "lvalue reference to cv X" for functions declared without a 800b57cec5SDimitry Andric // ref-qualifier or with the & ref-qualifier 810b57cec5SDimitry Andric // -- "rvalue reference to cv X" for functions declared with the && 820b57cec5SDimitry Andric // ref-qualifier 835f757f3fSDimitry Andric QualType T = MD->getFunctionObjectParameterType(); 840b57cec5SDimitry Andric T = FnType->getRefQualifier() == RQ_RValue 850b57cec5SDimitry Andric ? S.Context.getRValueReferenceType(T) 860b57cec5SDimitry Andric : S.Context.getLValueReferenceType(T, /*SpelledAsLValue*/ true); 870b57cec5SDimitry Andric AddArg(T); 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric for (QualType T : FnType->getParamTypes()) 910b57cec5SDimitry Andric AddArg(T); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric // Build the template-id. 940b57cec5SDimitry Andric QualType CoroTrait = 950b57cec5SDimitry Andric S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args); 960b57cec5SDimitry Andric if (CoroTrait.isNull()) 970b57cec5SDimitry Andric return QualType(); 980b57cec5SDimitry Andric if (S.RequireCompleteType(KwLoc, CoroTrait, 990b57cec5SDimitry Andric diag::err_coroutine_type_missing_specialization)) 1000b57cec5SDimitry Andric return QualType(); 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric auto *RD = CoroTrait->getAsCXXRecordDecl(); 1030b57cec5SDimitry Andric assert(RD && "specialization of class template is not a class?"); 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric // Look up the ::promise_type member. 1060b57cec5SDimitry Andric LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc, 1070b57cec5SDimitry Andric Sema::LookupOrdinaryName); 1080b57cec5SDimitry Andric S.LookupQualifiedName(R, RD); 1090b57cec5SDimitry Andric auto *Promise = R.getAsSingle<TypeDecl>(); 1100b57cec5SDimitry Andric if (!Promise) { 1110b57cec5SDimitry Andric S.Diag(FuncLoc, 1120b57cec5SDimitry Andric diag::err_implied_std_coroutine_traits_promise_type_not_found) 1130b57cec5SDimitry Andric << RD; 1140b57cec5SDimitry Andric return QualType(); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric // The promise type is required to be a class type. 1170b57cec5SDimitry Andric QualType PromiseType = S.Context.getTypeDeclType(Promise); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric auto buildElaboratedType = [&]() { 12006c3fb27SDimitry Andric auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, S.getStdNamespace()); 1210b57cec5SDimitry Andric NNS = NestedNameSpecifier::Create(S.Context, NNS, false, 1220b57cec5SDimitry Andric CoroTrait.getTypePtr()); 1235f757f3fSDimitry Andric return S.Context.getElaboratedType(ElaboratedTypeKeyword::None, NNS, 1245f757f3fSDimitry Andric PromiseType); 1250b57cec5SDimitry Andric }; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric if (!PromiseType->getAsCXXRecordDecl()) { 1280b57cec5SDimitry Andric S.Diag(FuncLoc, 1290b57cec5SDimitry Andric diag::err_implied_std_coroutine_traits_promise_type_not_class) 1300b57cec5SDimitry Andric << buildElaboratedType(); 1310b57cec5SDimitry Andric return QualType(); 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric if (S.RequireCompleteType(FuncLoc, buildElaboratedType(), 1340b57cec5SDimitry Andric diag::err_coroutine_promise_type_incomplete)) 1350b57cec5SDimitry Andric return QualType(); 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric return PromiseType; 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 140349cc55cSDimitry Andric /// Look up the std::coroutine_handle<PromiseType>. 1410b57cec5SDimitry Andric static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType, 1420b57cec5SDimitry Andric SourceLocation Loc) { 1430b57cec5SDimitry Andric if (PromiseType.isNull()) 1440b57cec5SDimitry Andric return QualType(); 1450b57cec5SDimitry Andric 14606c3fb27SDimitry Andric NamespaceDecl *CoroNamespace = S.getStdNamespace(); 147349cc55cSDimitry Andric assert(CoroNamespace && "Should already be diagnosed"); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"), 1500b57cec5SDimitry Andric Loc, Sema::LookupOrdinaryName); 151349cc55cSDimitry Andric if (!S.LookupQualifiedName(Result, CoroNamespace)) { 1520b57cec5SDimitry Andric S.Diag(Loc, diag::err_implied_coroutine_type_not_found) 153349cc55cSDimitry Andric << "std::coroutine_handle"; 1540b57cec5SDimitry Andric return QualType(); 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>(); 1580b57cec5SDimitry Andric if (!CoroHandle) { 1590b57cec5SDimitry Andric Result.suppressDiagnostics(); 1600b57cec5SDimitry Andric // We found something weird. Complain about the first thing we found. 1610b57cec5SDimitry Andric NamedDecl *Found = *Result.begin(); 1620b57cec5SDimitry Andric S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle); 1630b57cec5SDimitry Andric return QualType(); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric // Form template argument list for coroutine_handle<Promise>. 1670b57cec5SDimitry Andric TemplateArgumentListInfo Args(Loc, Loc); 1680b57cec5SDimitry Andric Args.addArgument(TemplateArgumentLoc( 1690b57cec5SDimitry Andric TemplateArgument(PromiseType), 1700b57cec5SDimitry Andric S.Context.getTrivialTypeSourceInfo(PromiseType, Loc))); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Build the template-id. 1730b57cec5SDimitry Andric QualType CoroHandleType = 1740b57cec5SDimitry Andric S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args); 1750b57cec5SDimitry Andric if (CoroHandleType.isNull()) 1760b57cec5SDimitry Andric return QualType(); 1770b57cec5SDimitry Andric if (S.RequireCompleteType(Loc, CoroHandleType, 1780b57cec5SDimitry Andric diag::err_coroutine_type_missing_specialization)) 1790b57cec5SDimitry Andric return QualType(); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric return CoroHandleType; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric static bool isValidCoroutineContext(Sema &S, SourceLocation Loc, 1850b57cec5SDimitry Andric StringRef Keyword) { 1860b57cec5SDimitry Andric // [expr.await]p2 dictates that 'co_await' and 'co_yield' must be used within 1870b57cec5SDimitry Andric // a function body. 1880b57cec5SDimitry Andric // FIXME: This also covers [expr.await]p2: "An await-expression shall not 1890b57cec5SDimitry Andric // appear in a default argument." But the diagnostic QoI here could be 1900b57cec5SDimitry Andric // improved to inform the user that default arguments specifically are not 1910b57cec5SDimitry Andric // allowed. 1920b57cec5SDimitry Andric auto *FD = dyn_cast<FunctionDecl>(S.CurContext); 1930b57cec5SDimitry Andric if (!FD) { 1940b57cec5SDimitry Andric S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext) 1950b57cec5SDimitry Andric ? diag::err_coroutine_objc_method 1960b57cec5SDimitry Andric : diag::err_coroutine_outside_function) << Keyword; 1970b57cec5SDimitry Andric return false; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // An enumeration for mapping the diagnostic type to the correct diagnostic 2010b57cec5SDimitry Andric // selection index. 2020b57cec5SDimitry Andric enum InvalidFuncDiag { 2030b57cec5SDimitry Andric DiagCtor = 0, 2040b57cec5SDimitry Andric DiagDtor, 2050b57cec5SDimitry Andric DiagMain, 2060b57cec5SDimitry Andric DiagConstexpr, 2070b57cec5SDimitry Andric DiagAutoRet, 2080b57cec5SDimitry Andric DiagVarargs, 2090b57cec5SDimitry Andric DiagConsteval, 2100b57cec5SDimitry Andric }; 2110b57cec5SDimitry Andric bool Diagnosed = false; 2120b57cec5SDimitry Andric auto DiagInvalid = [&](InvalidFuncDiag ID) { 2130b57cec5SDimitry Andric S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword; 2140b57cec5SDimitry Andric Diagnosed = true; 2150b57cec5SDimitry Andric return false; 2160b57cec5SDimitry Andric }; 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric // Diagnose when a constructor, destructor 2190b57cec5SDimitry Andric // or the function 'main' are declared as a coroutine. 2200b57cec5SDimitry Andric auto *MD = dyn_cast<CXXMethodDecl>(FD); 2210b57cec5SDimitry Andric // [class.ctor]p11: "A constructor shall not be a coroutine." 2220b57cec5SDimitry Andric if (MD && isa<CXXConstructorDecl>(MD)) 2230b57cec5SDimitry Andric return DiagInvalid(DiagCtor); 2240b57cec5SDimitry Andric // [class.dtor]p17: "A destructor shall not be a coroutine." 2250b57cec5SDimitry Andric else if (MD && isa<CXXDestructorDecl>(MD)) 2260b57cec5SDimitry Andric return DiagInvalid(DiagDtor); 2270b57cec5SDimitry Andric // [basic.start.main]p3: "The function main shall not be a coroutine." 2280b57cec5SDimitry Andric else if (FD->isMain()) 2290b57cec5SDimitry Andric return DiagInvalid(DiagMain); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric // Emit a diagnostics for each of the following conditions which is not met. 2320b57cec5SDimitry Andric // [expr.const]p2: "An expression e is a core constant expression unless the 2330b57cec5SDimitry Andric // evaluation of e [...] would evaluate one of the following expressions: 2340b57cec5SDimitry Andric // [...] an await-expression [...] a yield-expression." 2350b57cec5SDimitry Andric if (FD->isConstexpr()) 2360b57cec5SDimitry Andric DiagInvalid(FD->isConsteval() ? DiagConsteval : DiagConstexpr); 2370b57cec5SDimitry Andric // [dcl.spec.auto]p15: "A function declared with a return type that uses a 2380b57cec5SDimitry Andric // placeholder type shall not be a coroutine." 2390b57cec5SDimitry Andric if (FD->getReturnType()->isUndeducedType()) 2400b57cec5SDimitry Andric DiagInvalid(DiagAutoRet); 2410eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p1 2420eae32dcSDimitry Andric // The parameter-declaration-clause of the coroutine shall not terminate with 2430eae32dcSDimitry Andric // an ellipsis that is not part of a parameter-declaration. 2440b57cec5SDimitry Andric if (FD->isVariadic()) 2450b57cec5SDimitry Andric DiagInvalid(DiagVarargs); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric return !Diagnosed; 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric /// Build a call to 'operator co_await' if there is a suitable operator for 2510b57cec5SDimitry Andric /// the given expression. 25281ad6265SDimitry Andric ExprResult Sema::BuildOperatorCoawaitCall(SourceLocation Loc, Expr *E, 2530b57cec5SDimitry Andric UnresolvedLookupExpr *Lookup) { 2540b57cec5SDimitry Andric UnresolvedSet<16> Functions; 2550b57cec5SDimitry Andric Functions.append(Lookup->decls_begin(), Lookup->decls_end()); 25681ad6265SDimitry Andric return CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E); 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S, 2600b57cec5SDimitry Andric SourceLocation Loc, Expr *E) { 26181ad6265SDimitry Andric ExprResult R = SemaRef.BuildOperatorCoawaitLookupExpr(S, Loc); 2620b57cec5SDimitry Andric if (R.isInvalid()) 2630b57cec5SDimitry Andric return ExprError(); 26481ad6265SDimitry Andric return SemaRef.BuildOperatorCoawaitCall(Loc, E, 2650b57cec5SDimitry Andric cast<UnresolvedLookupExpr>(R.get())); 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType, 2690b57cec5SDimitry Andric SourceLocation Loc) { 2700b57cec5SDimitry Andric QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc); 2710b57cec5SDimitry Andric if (CoroHandleType.isNull()) 2720b57cec5SDimitry Andric return ExprError(); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType); 2750b57cec5SDimitry Andric LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc, 2760b57cec5SDimitry Andric Sema::LookupOrdinaryName); 2770b57cec5SDimitry Andric if (!S.LookupQualifiedName(Found, LookupCtx)) { 2780b57cec5SDimitry Andric S.Diag(Loc, diag::err_coroutine_handle_missing_member) 2790b57cec5SDimitry Andric << "from_address"; 2800b57cec5SDimitry Andric return ExprError(); 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric Expr *FramePtr = 284fe6060f1SDimitry Andric S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_frame, {}); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric CXXScopeSpec SS; 2870b57cec5SDimitry Andric ExprResult FromAddr = 2880b57cec5SDimitry Andric S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); 2890b57cec5SDimitry Andric if (FromAddr.isInvalid()) 2900b57cec5SDimitry Andric return ExprError(); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric return S.BuildCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc); 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric struct ReadySuspendResumeResult { 2960b57cec5SDimitry Andric enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume }; 2970b57cec5SDimitry Andric Expr *Results[3]; 2980b57cec5SDimitry Andric OpaqueValueExpr *OpaqueValue; 2990b57cec5SDimitry Andric bool IsInvalid; 3000b57cec5SDimitry Andric }; 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc, 3030b57cec5SDimitry Andric StringRef Name, MultiExprArg Args) { 3040b57cec5SDimitry Andric DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc); 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&. 3070b57cec5SDimitry Andric CXXScopeSpec SS; 3080b57cec5SDimitry Andric ExprResult Result = S.BuildMemberReferenceExpr( 3090b57cec5SDimitry Andric Base, Base->getType(), Loc, /*IsPtr=*/false, SS, 3100b57cec5SDimitry Andric SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr, 3110b57cec5SDimitry Andric /*Scope=*/nullptr); 3120b57cec5SDimitry Andric if (Result.isInvalid()) 3130b57cec5SDimitry Andric return ExprError(); 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric // We meant exactly what we asked for. No need for typo correction. 3160b57cec5SDimitry Andric if (auto *TE = dyn_cast<TypoExpr>(Result.get())) { 3170b57cec5SDimitry Andric S.clearDelayedTypo(TE); 3180b57cec5SDimitry Andric S.Diag(Loc, diag::err_no_member) 3190b57cec5SDimitry Andric << NameInfo.getName() << Base->getType()->getAsCXXRecordDecl() 3200b57cec5SDimitry Andric << Base->getSourceRange(); 3210b57cec5SDimitry Andric return ExprError(); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3245f757f3fSDimitry Andric auto EndLoc = Args.empty() ? Loc : Args.back()->getEndLoc(); 3255f757f3fSDimitry Andric return S.BuildCallExpr(nullptr, Result.get(), Loc, Args, EndLoc, nullptr); 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric // See if return type is coroutine-handle and if so, invoke builtin coro-resume 32906c3fb27SDimitry Andric // on its address. This is to enable the support for coroutine-handle 3300b57cec5SDimitry Andric // returning await_suspend that results in a guaranteed tail call to the target 3310b57cec5SDimitry Andric // coroutine. 3320b57cec5SDimitry Andric static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E, 3330b57cec5SDimitry Andric SourceLocation Loc) { 3340b57cec5SDimitry Andric if (RetType->isReferenceType()) 3350b57cec5SDimitry Andric return nullptr; 3360b57cec5SDimitry Andric Type const *T = RetType.getTypePtr(); 3370b57cec5SDimitry Andric if (!T->isClassType() && !T->isStructureType()) 3380b57cec5SDimitry Andric return nullptr; 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric // FIXME: Add convertability check to coroutine_handle<>. Possibly via 3410b57cec5SDimitry Andric // EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment 3420b57cec5SDimitry Andric // a private function in SemaExprCXX.cpp 3430b57cec5SDimitry Andric 344bdd1243dSDimitry Andric ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", std::nullopt); 3450b57cec5SDimitry Andric if (AddressExpr.isInvalid()) 3460b57cec5SDimitry Andric return nullptr; 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric Expr *JustAddress = AddressExpr.get(); 3495ffd83dbSDimitry Andric 3505ffd83dbSDimitry Andric // Check that the type of AddressExpr is void* 3515ffd83dbSDimitry Andric if (!JustAddress->getType().getTypePtr()->isVoidPointerType()) 3525ffd83dbSDimitry Andric S.Diag(cast<CallExpr>(JustAddress)->getCalleeDecl()->getLocation(), 3535ffd83dbSDimitry Andric diag::warn_coroutine_handle_address_invalid_return_type) 3545ffd83dbSDimitry Andric << JustAddress->getType(); 3555ffd83dbSDimitry Andric 3560fca6ea1SDimitry Andric // Clean up temporary objects, because the resulting expression 3570fca6ea1SDimitry Andric // will become the body of await_suspend wrapper. 3580fca6ea1SDimitry Andric return S.MaybeCreateExprWithCleanups(JustAddress); 3595f757f3fSDimitry Andric } 3605f757f3fSDimitry Andric 3610b57cec5SDimitry Andric /// Build calls to await_ready, await_suspend, and await_resume for a co_await 3620b57cec5SDimitry Andric /// expression. 363e8d8bef9SDimitry Andric /// The generated AST tries to clean up temporary objects as early as 364e8d8bef9SDimitry Andric /// possible so that they don't live across suspension points if possible. 365e8d8bef9SDimitry Andric /// Having temporary objects living across suspension points unnecessarily can 366e8d8bef9SDimitry Andric /// lead to large frame size, and also lead to memory corruptions if the 367e8d8bef9SDimitry Andric /// coroutine frame is destroyed after coming back from suspension. This is done 368e8d8bef9SDimitry Andric /// by wrapping both the await_ready call and the await_suspend call with 369e8d8bef9SDimitry Andric /// ExprWithCleanups. In the end of this function, we also need to explicitly 370e8d8bef9SDimitry Andric /// set cleanup state so that the CoawaitExpr is also wrapped with an 371e8d8bef9SDimitry Andric /// ExprWithCleanups to clean up the awaiter associated with the co_await 372e8d8bef9SDimitry Andric /// expression. 3730b57cec5SDimitry Andric static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise, 3740b57cec5SDimitry Andric SourceLocation Loc, Expr *E) { 3750b57cec5SDimitry Andric OpaqueValueExpr *Operand = new (S.Context) 3760b57cec5SDimitry Andric OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E); 3770b57cec5SDimitry Andric 378e8d8bef9SDimitry Andric // Assume valid until we see otherwise. 379e8d8bef9SDimitry Andric // Further operations are responsible for setting IsInalid to true. 380e8d8bef9SDimitry Andric ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/false}; 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric using ACT = ReadySuspendResumeResult::AwaitCallType; 383e8d8bef9SDimitry Andric 384e8d8bef9SDimitry Andric auto BuildSubExpr = [&](ACT CallType, StringRef Func, 385e8d8bef9SDimitry Andric MultiExprArg Arg) -> Expr * { 386e8d8bef9SDimitry Andric ExprResult Result = buildMemberCall(S, Operand, Loc, Func, Arg); 387e8d8bef9SDimitry Andric if (Result.isInvalid()) { 388e8d8bef9SDimitry Andric Calls.IsInvalid = true; 389e8d8bef9SDimitry Andric return nullptr; 390e8d8bef9SDimitry Andric } 391e8d8bef9SDimitry Andric Calls.Results[CallType] = Result.get(); 392e8d8bef9SDimitry Andric return Result.get(); 393e8d8bef9SDimitry Andric }; 394e8d8bef9SDimitry Andric 395bdd1243dSDimitry Andric CallExpr *AwaitReady = cast_or_null<CallExpr>( 396bdd1243dSDimitry Andric BuildSubExpr(ACT::ACT_Ready, "await_ready", std::nullopt)); 397e8d8bef9SDimitry Andric if (!AwaitReady) 398e8d8bef9SDimitry Andric return Calls; 3990b57cec5SDimitry Andric if (!AwaitReady->getType()->isDependentType()) { 4000b57cec5SDimitry Andric // [expr.await]p3 [...] 4010b57cec5SDimitry Andric // — await-ready is the expression e.await_ready(), contextually converted 4020b57cec5SDimitry Andric // to bool. 4030b57cec5SDimitry Andric ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady); 4040b57cec5SDimitry Andric if (Conv.isInvalid()) { 4050b57cec5SDimitry Andric S.Diag(AwaitReady->getDirectCallee()->getBeginLoc(), 4060b57cec5SDimitry Andric diag::note_await_ready_no_bool_conversion); 4070b57cec5SDimitry Andric S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) 4080b57cec5SDimitry Andric << AwaitReady->getDirectCallee() << E->getSourceRange(); 4090b57cec5SDimitry Andric Calls.IsInvalid = true; 410e8d8bef9SDimitry Andric } else 411e8d8bef9SDimitry Andric Calls.Results[ACT::ACT_Ready] = S.MaybeCreateExprWithCleanups(Conv.get()); 4120b57cec5SDimitry Andric } 413e8d8bef9SDimitry Andric 414e8d8bef9SDimitry Andric ExprResult CoroHandleRes = 415e8d8bef9SDimitry Andric buildCoroutineHandle(S, CoroPromise->getType(), Loc); 416e8d8bef9SDimitry Andric if (CoroHandleRes.isInvalid()) { 417e8d8bef9SDimitry Andric Calls.IsInvalid = true; 418e8d8bef9SDimitry Andric return Calls; 4190b57cec5SDimitry Andric } 420e8d8bef9SDimitry Andric Expr *CoroHandle = CoroHandleRes.get(); 421e8d8bef9SDimitry Andric CallExpr *AwaitSuspend = cast_or_null<CallExpr>( 422e8d8bef9SDimitry Andric BuildSubExpr(ACT::ACT_Suspend, "await_suspend", CoroHandle)); 423e8d8bef9SDimitry Andric if (!AwaitSuspend) 424e8d8bef9SDimitry Andric return Calls; 4250b57cec5SDimitry Andric if (!AwaitSuspend->getType()->isDependentType()) { 4260b57cec5SDimitry Andric // [expr.await]p3 [...] 4270b57cec5SDimitry Andric // - await-suspend is the expression e.await_suspend(h), which shall be 428e8d8bef9SDimitry Andric // a prvalue of type void, bool, or std::coroutine_handle<Z> for some 429e8d8bef9SDimitry Andric // type Z. 4300b57cec5SDimitry Andric QualType RetType = AwaitSuspend->getCallReturnType(S.Context); 4310b57cec5SDimitry Andric 43206c3fb27SDimitry Andric // Support for coroutine_handle returning await_suspend. 433e8d8bef9SDimitry Andric if (Expr *TailCallSuspend = 434e8d8bef9SDimitry Andric maybeTailCall(S, RetType, AwaitSuspend, Loc)) 435e8d8bef9SDimitry Andric // Note that we don't wrap the expression with ExprWithCleanups here 436e8d8bef9SDimitry Andric // because that might interfere with tailcall contract (e.g. inserting 437e8d8bef9SDimitry Andric // clean up instructions in-between tailcall and return). Instead 438e8d8bef9SDimitry Andric // ExprWithCleanups is wrapped within maybeTailCall() prior to the resume 439e8d8bef9SDimitry Andric // call. 4400b57cec5SDimitry Andric Calls.Results[ACT::ACT_Suspend] = TailCallSuspend; 4410b57cec5SDimitry Andric else { 4420b57cec5SDimitry Andric // non-class prvalues always have cv-unqualified types 4430b57cec5SDimitry Andric if (RetType->isReferenceType() || 4440b57cec5SDimitry Andric (!RetType->isBooleanType() && !RetType->isVoidType())) { 4450b57cec5SDimitry Andric S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(), 4460b57cec5SDimitry Andric diag::err_await_suspend_invalid_return_type) 4470b57cec5SDimitry Andric << RetType; 4480b57cec5SDimitry Andric S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) 4490b57cec5SDimitry Andric << AwaitSuspend->getDirectCallee(); 4500b57cec5SDimitry Andric Calls.IsInvalid = true; 451e8d8bef9SDimitry Andric } else 452e8d8bef9SDimitry Andric Calls.Results[ACT::ACT_Suspend] = 453e8d8bef9SDimitry Andric S.MaybeCreateExprWithCleanups(AwaitSuspend); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric } 456e8d8bef9SDimitry Andric 457bdd1243dSDimitry Andric BuildSubExpr(ACT::ACT_Resume, "await_resume", std::nullopt); 458e8d8bef9SDimitry Andric 459e8d8bef9SDimitry Andric // Make sure the awaiter object gets a chance to be cleaned up. 460e8d8bef9SDimitry Andric S.Cleanup.setExprNeedsCleanups(true); 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric return Calls; 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise, 4660b57cec5SDimitry Andric SourceLocation Loc, StringRef Name, 4670b57cec5SDimitry Andric MultiExprArg Args) { 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric // Form a reference to the promise. 4700b57cec5SDimitry Andric ExprResult PromiseRef = S.BuildDeclRefExpr( 4710b57cec5SDimitry Andric Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc); 4720b57cec5SDimitry Andric if (PromiseRef.isInvalid()) 4730b57cec5SDimitry Andric return ExprError(); 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args); 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) { 4790b57cec5SDimitry Andric assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); 4800b57cec5SDimitry Andric auto *FD = cast<FunctionDecl>(CurContext); 4810b57cec5SDimitry Andric bool IsThisDependentType = [&] { 4825f757f3fSDimitry Andric if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FD)) 4835f757f3fSDimitry Andric return MD->isImplicitObjectMemberFunction() && 4845f757f3fSDimitry Andric MD->getThisType()->isDependentType(); 4850b57cec5SDimitry Andric return false; 4860b57cec5SDimitry Andric }(); 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric QualType T = FD->getType()->isDependentType() || IsThisDependentType 4890b57cec5SDimitry Andric ? Context.DependentTy 4900b57cec5SDimitry Andric : lookupPromiseType(*this, FD, Loc); 4910b57cec5SDimitry Andric if (T.isNull()) 4920b57cec5SDimitry Andric return nullptr; 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(), 4950b57cec5SDimitry Andric &PP.getIdentifierTable().get("__promise"), T, 4960b57cec5SDimitry Andric Context.getTrivialTypeSourceInfo(T, Loc), SC_None); 497e8d8bef9SDimitry Andric VD->setImplicit(); 4980b57cec5SDimitry Andric CheckVariableDeclarationType(VD); 4990b57cec5SDimitry Andric if (VD->isInvalidDecl()) 5000b57cec5SDimitry Andric return nullptr; 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric auto *ScopeInfo = getCurFunction(); 5035ffd83dbSDimitry Andric 5045ffd83dbSDimitry Andric // Build a list of arguments, based on the coroutine function's arguments, 5055ffd83dbSDimitry Andric // that if present will be passed to the promise type's constructor. 5060b57cec5SDimitry Andric llvm::SmallVector<Expr *, 4> CtorArgExprs; 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric // Add implicit object parameter. 5090b57cec5SDimitry Andric if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { 5105f757f3fSDimitry Andric if (MD->isImplicitObjectMemberFunction() && !isLambdaCallOperator(MD)) { 5110b57cec5SDimitry Andric ExprResult ThisExpr = ActOnCXXThis(Loc); 5120b57cec5SDimitry Andric if (ThisExpr.isInvalid()) 5130b57cec5SDimitry Andric return nullptr; 5140b57cec5SDimitry Andric ThisExpr = CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get()); 5150b57cec5SDimitry Andric if (ThisExpr.isInvalid()) 5160b57cec5SDimitry Andric return nullptr; 5170b57cec5SDimitry Andric CtorArgExprs.push_back(ThisExpr.get()); 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5215ffd83dbSDimitry Andric // Add the coroutine function's parameters. 5220b57cec5SDimitry Andric auto &Moves = ScopeInfo->CoroutineParameterMoves; 5230b57cec5SDimitry Andric for (auto *PD : FD->parameters()) { 5240b57cec5SDimitry Andric if (PD->getType()->isDependentType()) 5250b57cec5SDimitry Andric continue; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric auto RefExpr = ExprEmpty(); 5280b57cec5SDimitry Andric auto Move = Moves.find(PD); 5290b57cec5SDimitry Andric assert(Move != Moves.end() && 5300b57cec5SDimitry Andric "Coroutine function parameter not inserted into move map"); 5310b57cec5SDimitry Andric // If a reference to the function parameter exists in the coroutine 5320b57cec5SDimitry Andric // frame, use that reference. 5330b57cec5SDimitry Andric auto *MoveDecl = 5340b57cec5SDimitry Andric cast<VarDecl>(cast<DeclStmt>(Move->second)->getSingleDecl()); 5350b57cec5SDimitry Andric RefExpr = 5360b57cec5SDimitry Andric BuildDeclRefExpr(MoveDecl, MoveDecl->getType().getNonReferenceType(), 5370b57cec5SDimitry Andric ExprValueKind::VK_LValue, FD->getLocation()); 5380b57cec5SDimitry Andric if (RefExpr.isInvalid()) 5390b57cec5SDimitry Andric return nullptr; 5400b57cec5SDimitry Andric CtorArgExprs.push_back(RefExpr.get()); 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5435ffd83dbSDimitry Andric // If we have a non-zero number of constructor arguments, try to use them. 5445ffd83dbSDimitry Andric // Otherwise, fall back to the promise type's default constructor. 5455ffd83dbSDimitry Andric if (!CtorArgExprs.empty()) { 5460b57cec5SDimitry Andric // Create an initialization sequence for the promise type using the 5470b57cec5SDimitry Andric // constructor arguments, wrapped in a parenthesized list expression. 5480b57cec5SDimitry Andric Expr *PLE = ParenListExpr::Create(Context, FD->getLocation(), 5490b57cec5SDimitry Andric CtorArgExprs, FD->getLocation()); 5500b57cec5SDimitry Andric InitializedEntity Entity = InitializedEntity::InitializeVariable(VD); 5510b57cec5SDimitry Andric InitializationKind Kind = InitializationKind::CreateForInit( 5520b57cec5SDimitry Andric VD->getLocation(), /*DirectInit=*/true, PLE); 5530b57cec5SDimitry Andric InitializationSequence InitSeq(*this, Entity, Kind, CtorArgExprs, 5540b57cec5SDimitry Andric /*TopLevelOfInitList=*/false, 5550b57cec5SDimitry Andric /*TreatUnavailableAsInvalid=*/false); 5560b57cec5SDimitry Andric 5570eae32dcSDimitry Andric // [dcl.fct.def.coroutine]5.7 5580eae32dcSDimitry Andric // promise-constructor-arguments is determined as follows: overload 5590eae32dcSDimitry Andric // resolution is performed on a promise constructor call created by 5600eae32dcSDimitry Andric // assembling an argument list q_1 ... q_n . If a viable constructor is 5610eae32dcSDimitry Andric // found ([over.match.viable]), then promise-constructor-arguments is ( q_1 5620eae32dcSDimitry Andric // , ..., q_n ), otherwise promise-constructor-arguments is empty. 5630b57cec5SDimitry Andric if (InitSeq) { 5640b57cec5SDimitry Andric ExprResult Result = InitSeq.Perform(*this, Entity, Kind, CtorArgExprs); 5650b57cec5SDimitry Andric if (Result.isInvalid()) { 5660b57cec5SDimitry Andric VD->setInvalidDecl(); 5670b57cec5SDimitry Andric } else if (Result.get()) { 5680b57cec5SDimitry Andric VD->setInit(MaybeCreateExprWithCleanups(Result.get())); 5690b57cec5SDimitry Andric VD->setInitStyle(VarDecl::CallInit); 5700b57cec5SDimitry Andric CheckCompleteVariableDeclaration(VD); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric } else 5730b57cec5SDimitry Andric ActOnUninitializedDecl(VD); 5745ffd83dbSDimitry Andric } else 5755ffd83dbSDimitry Andric ActOnUninitializedDecl(VD); 5760b57cec5SDimitry Andric 5770b57cec5SDimitry Andric FD->addDecl(VD); 5780b57cec5SDimitry Andric return VD; 5790b57cec5SDimitry Andric } 5800b57cec5SDimitry Andric 5810b57cec5SDimitry Andric /// Check that this is a context in which a coroutine suspension can appear. 5820b57cec5SDimitry Andric static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc, 5830b57cec5SDimitry Andric StringRef Keyword, 5840b57cec5SDimitry Andric bool IsImplicit = false) { 5850b57cec5SDimitry Andric if (!isValidCoroutineContext(S, Loc, Keyword)) 5860b57cec5SDimitry Andric return nullptr; 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope"); 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric auto *ScopeInfo = S.getCurFunction(); 5910b57cec5SDimitry Andric assert(ScopeInfo && "missing function scope for function"); 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit) 5940b57cec5SDimitry Andric ScopeInfo->setFirstCoroutineStmt(Loc, Keyword); 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric if (ScopeInfo->CoroutinePromise) 5970b57cec5SDimitry Andric return ScopeInfo; 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric if (!S.buildCoroutineParameterMoves(Loc)) 6000b57cec5SDimitry Andric return nullptr; 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc); 6030b57cec5SDimitry Andric if (!ScopeInfo->CoroutinePromise) 6040b57cec5SDimitry Andric return nullptr; 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric return ScopeInfo; 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 6095ffd83dbSDimitry Andric /// Recursively check \p E and all its children to see if any call target 6105ffd83dbSDimitry Andric /// (including constructor call) is declared noexcept. Also any value returned 6115ffd83dbSDimitry Andric /// from the call has a noexcept destructor. 6125ffd83dbSDimitry Andric static void checkNoThrow(Sema &S, const Stmt *E, 6135ffd83dbSDimitry Andric llvm::SmallPtrSetImpl<const Decl *> &ThrowingDecls) { 6145ffd83dbSDimitry Andric auto checkDeclNoexcept = [&](const Decl *D, bool IsDtor = false) { 6155ffd83dbSDimitry Andric // In the case of dtor, the call to dtor is implicit and hence we should 6165ffd83dbSDimitry Andric // pass nullptr to canCalleeThrow. 6175ffd83dbSDimitry Andric if (Sema::canCalleeThrow(S, IsDtor ? nullptr : cast<Expr>(E), D)) { 6185ffd83dbSDimitry Andric if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 6195ffd83dbSDimitry Andric // co_await promise.final_suspend() could end up calling 6205ffd83dbSDimitry Andric // __builtin_coro_resume for symmetric transfer if await_suspend() 6215ffd83dbSDimitry Andric // returns a handle. In that case, even __builtin_coro_resume is not 6225ffd83dbSDimitry Andric // declared as noexcept and may throw, it does not throw _into_ the 6235ffd83dbSDimitry Andric // coroutine that just suspended, but rather throws back out from 6245ffd83dbSDimitry Andric // whoever called coroutine_handle::resume(), hence we claim that 6255ffd83dbSDimitry Andric // logically it does not throw. 6265ffd83dbSDimitry Andric if (FD->getBuiltinID() == Builtin::BI__builtin_coro_resume) 6275ffd83dbSDimitry Andric return; 6285ffd83dbSDimitry Andric } 6295ffd83dbSDimitry Andric if (ThrowingDecls.empty()) { 6300eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p15 63104eeddc0SDimitry Andric // The expression co_await promise.final_suspend() shall not be 6320eae32dcSDimitry Andric // potentially-throwing ([except.spec]). 6330eae32dcSDimitry Andric // 6345ffd83dbSDimitry Andric // First time seeing an error, emit the error message. 6355ffd83dbSDimitry Andric S.Diag(cast<FunctionDecl>(S.CurContext)->getLocation(), 6365ffd83dbSDimitry Andric diag::err_coroutine_promise_final_suspend_requires_nothrow); 6375ffd83dbSDimitry Andric } 6385ffd83dbSDimitry Andric ThrowingDecls.insert(D); 6395ffd83dbSDimitry Andric } 6405ffd83dbSDimitry Andric }; 64104eeddc0SDimitry Andric 64204eeddc0SDimitry Andric if (auto *CE = dyn_cast<CXXConstructExpr>(E)) { 64304eeddc0SDimitry Andric CXXConstructorDecl *Ctor = CE->getConstructor(); 6445ffd83dbSDimitry Andric checkDeclNoexcept(Ctor); 6455ffd83dbSDimitry Andric // Check the corresponding destructor of the constructor. 64604eeddc0SDimitry Andric checkDeclNoexcept(Ctor->getParent()->getDestructor(), /*IsDtor=*/true); 64704eeddc0SDimitry Andric } else if (auto *CE = dyn_cast<CallExpr>(E)) { 64804eeddc0SDimitry Andric if (CE->isTypeDependent()) 64904eeddc0SDimitry Andric return; 65004eeddc0SDimitry Andric 65104eeddc0SDimitry Andric checkDeclNoexcept(CE->getCalleeDecl()); 65204eeddc0SDimitry Andric QualType ReturnType = CE->getCallReturnType(S.getASTContext()); 6535ffd83dbSDimitry Andric // Check the destructor of the call return type, if any. 6545ffd83dbSDimitry Andric if (ReturnType.isDestructedType() == 6555ffd83dbSDimitry Andric QualType::DestructionKind::DK_cxx_destructor) { 6565ffd83dbSDimitry Andric const auto *T = 6575ffd83dbSDimitry Andric cast<RecordType>(ReturnType.getCanonicalType().getTypePtr()); 65881ad6265SDimitry Andric checkDeclNoexcept(cast<CXXRecordDecl>(T->getDecl())->getDestructor(), 65904eeddc0SDimitry Andric /*IsDtor=*/true); 6605ffd83dbSDimitry Andric } 66104eeddc0SDimitry Andric } else 6625ffd83dbSDimitry Andric for (const auto *Child : E->children()) { 6635ffd83dbSDimitry Andric if (!Child) 6645ffd83dbSDimitry Andric continue; 6655ffd83dbSDimitry Andric checkNoThrow(S, Child, ThrowingDecls); 6665ffd83dbSDimitry Andric } 6675ffd83dbSDimitry Andric } 6685ffd83dbSDimitry Andric 6695ffd83dbSDimitry Andric bool Sema::checkFinalSuspendNoThrow(const Stmt *FinalSuspend) { 6705ffd83dbSDimitry Andric llvm::SmallPtrSet<const Decl *, 4> ThrowingDecls; 6715ffd83dbSDimitry Andric // We first collect all declarations that should not throw but not declared 6725ffd83dbSDimitry Andric // with noexcept. We then sort them based on the location before printing. 6735ffd83dbSDimitry Andric // This is to avoid emitting the same note multiple times on the same 6745ffd83dbSDimitry Andric // declaration, and also provide a deterministic order for the messages. 6755ffd83dbSDimitry Andric checkNoThrow(*this, FinalSuspend, ThrowingDecls); 6765ffd83dbSDimitry Andric auto SortedDecls = llvm::SmallVector<const Decl *, 4>{ThrowingDecls.begin(), 6775ffd83dbSDimitry Andric ThrowingDecls.end()}; 6785ffd83dbSDimitry Andric sort(SortedDecls, [](const Decl *A, const Decl *B) { 6795ffd83dbSDimitry Andric return A->getEndLoc() < B->getEndLoc(); 6805ffd83dbSDimitry Andric }); 6815ffd83dbSDimitry Andric for (const auto *D : SortedDecls) { 6825ffd83dbSDimitry Andric Diag(D->getEndLoc(), diag::note_coroutine_function_declare_noexcept); 6835ffd83dbSDimitry Andric } 6845ffd83dbSDimitry Andric return ThrowingDecls.empty(); 6855ffd83dbSDimitry Andric } 6865ffd83dbSDimitry Andric 6870b57cec5SDimitry Andric bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc, 6880b57cec5SDimitry Andric StringRef Keyword) { 6897a6dacacSDimitry Andric // Ignore previous expr evaluation contexts. 6907a6dacacSDimitry Andric EnterExpressionEvaluationContext PotentiallyEvaluated( 6917a6dacacSDimitry Andric *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); 6920b57cec5SDimitry Andric if (!checkCoroutineContext(*this, KWLoc, Keyword)) 6930b57cec5SDimitry Andric return false; 6940b57cec5SDimitry Andric auto *ScopeInfo = getCurFunction(); 6950b57cec5SDimitry Andric assert(ScopeInfo->CoroutinePromise); 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric // If we have existing coroutine statements then we have already built 6980b57cec5SDimitry Andric // the initial and final suspend points. 6990b57cec5SDimitry Andric if (!ScopeInfo->NeedsCoroutineSuspends) 7000b57cec5SDimitry Andric return true; 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric ScopeInfo->setNeedsCoroutineSuspends(false); 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric auto *Fn = cast<FunctionDecl>(CurContext); 7050b57cec5SDimitry Andric SourceLocation Loc = Fn->getLocation(); 7060b57cec5SDimitry Andric // Build the initial suspend point 7070b57cec5SDimitry Andric auto buildSuspends = [&](StringRef Name) mutable -> StmtResult { 708bdd1243dSDimitry Andric ExprResult Operand = buildPromiseCall(*this, ScopeInfo->CoroutinePromise, 709bdd1243dSDimitry Andric Loc, Name, std::nullopt); 71081ad6265SDimitry Andric if (Operand.isInvalid()) 71181ad6265SDimitry Andric return StmtError(); 71281ad6265SDimitry Andric ExprResult Suspend = 71381ad6265SDimitry Andric buildOperatorCoawaitCall(*this, SC, Loc, Operand.get()); 7140b57cec5SDimitry Andric if (Suspend.isInvalid()) 7150b57cec5SDimitry Andric return StmtError(); 71681ad6265SDimitry Andric Suspend = BuildResolvedCoawaitExpr(Loc, Operand.get(), Suspend.get(), 7170b57cec5SDimitry Andric /*IsImplicit*/ true); 7180b57cec5SDimitry Andric Suspend = ActOnFinishFullExpr(Suspend.get(), /*DiscardedValue*/ false); 7190b57cec5SDimitry Andric if (Suspend.isInvalid()) { 7200b57cec5SDimitry Andric Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required) 7210b57cec5SDimitry Andric << ((Name == "initial_suspend") ? 0 : 1); 7220b57cec5SDimitry Andric Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword; 7230b57cec5SDimitry Andric return StmtError(); 7240b57cec5SDimitry Andric } 7250b57cec5SDimitry Andric return cast<Stmt>(Suspend.get()); 7260b57cec5SDimitry Andric }; 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric StmtResult InitSuspend = buildSuspends("initial_suspend"); 7290b57cec5SDimitry Andric if (InitSuspend.isInvalid()) 7300b57cec5SDimitry Andric return true; 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric StmtResult FinalSuspend = buildSuspends("final_suspend"); 7335ffd83dbSDimitry Andric if (FinalSuspend.isInvalid() || !checkFinalSuspendNoThrow(FinalSuspend.get())) 7340b57cec5SDimitry Andric return true; 7350b57cec5SDimitry Andric 7360b57cec5SDimitry Andric ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get()); 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric return true; 7390b57cec5SDimitry Andric } 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // Recursively walks up the scope hierarchy until either a 'catch' or a function 7420b57cec5SDimitry Andric // scope is found, whichever comes first. 7430b57cec5SDimitry Andric static bool isWithinCatchScope(Scope *S) { 7440b57cec5SDimitry Andric // 'co_await' and 'co_yield' keywords are disallowed within catch blocks, but 7450b57cec5SDimitry Andric // lambdas that use 'co_await' are allowed. The loop below ends when a 7460b57cec5SDimitry Andric // function scope is found in order to ensure the following behavior: 7470b57cec5SDimitry Andric // 7480b57cec5SDimitry Andric // void foo() { // <- function scope 7490b57cec5SDimitry Andric // try { // 7500b57cec5SDimitry Andric // co_await x; // <- 'co_await' is OK within a function scope 7510b57cec5SDimitry Andric // } catch { // <- catch scope 7520b57cec5SDimitry Andric // co_await x; // <- 'co_await' is not OK within a catch scope 7530b57cec5SDimitry Andric // []() { // <- function scope 7540b57cec5SDimitry Andric // co_await x; // <- 'co_await' is OK within a function scope 7550b57cec5SDimitry Andric // }(); 7560b57cec5SDimitry Andric // } 7570b57cec5SDimitry Andric // } 75881ad6265SDimitry Andric while (S && !S->isFunctionScope()) { 75981ad6265SDimitry Andric if (S->isCatchScope()) 7600b57cec5SDimitry Andric return true; 7610b57cec5SDimitry Andric S = S->getParent(); 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric return false; 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric // [expr.await]p2, emphasis added: "An await-expression shall appear only in 7670b57cec5SDimitry Andric // a *potentially evaluated* expression within the compound-statement of a 7680b57cec5SDimitry Andric // function-body *outside of a handler* [...] A context within a function 7690b57cec5SDimitry Andric // where an await-expression can appear is called a suspension context of the 7700b57cec5SDimitry Andric // function." 771bdd1243dSDimitry Andric static bool checkSuspensionContext(Sema &S, SourceLocation Loc, 7720b57cec5SDimitry Andric StringRef Keyword) { 7730b57cec5SDimitry Andric // First emphasis of [expr.await]p2: must be a potentially evaluated context. 7740b57cec5SDimitry Andric // That is, 'co_await' and 'co_yield' cannot appear in subexpressions of 7750b57cec5SDimitry Andric // \c sizeof. 776bdd1243dSDimitry Andric if (S.isUnevaluatedContext()) { 7770b57cec5SDimitry Andric S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword; 778bdd1243dSDimitry Andric return false; 779bdd1243dSDimitry Andric } 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric // Second emphasis of [expr.await]p2: must be outside of an exception handler. 782bdd1243dSDimitry Andric if (isWithinCatchScope(S.getCurScope())) { 7830b57cec5SDimitry Andric S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword; 784bdd1243dSDimitry Andric return false; 785bdd1243dSDimitry Andric } 786bdd1243dSDimitry Andric 787bdd1243dSDimitry Andric return true; 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) { 791bdd1243dSDimitry Andric if (!checkSuspensionContext(*this, Loc, "co_await")) 792bdd1243dSDimitry Andric return ExprError(); 793bdd1243dSDimitry Andric 7940b57cec5SDimitry Andric if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) { 7950b57cec5SDimitry Andric CorrectDelayedTyposInExpr(E); 7960b57cec5SDimitry Andric return ExprError(); 7970b57cec5SDimitry Andric } 7980b57cec5SDimitry Andric 7991fd87a68SDimitry Andric if (E->hasPlaceholderType()) { 8000b57cec5SDimitry Andric ExprResult R = CheckPlaceholderExpr(E); 8010b57cec5SDimitry Andric if (R.isInvalid()) return ExprError(); 8020b57cec5SDimitry Andric E = R.get(); 8030b57cec5SDimitry Andric } 80481ad6265SDimitry Andric ExprResult Lookup = BuildOperatorCoawaitLookupExpr(S, Loc); 8050b57cec5SDimitry Andric if (Lookup.isInvalid()) 8060b57cec5SDimitry Andric return ExprError(); 8070b57cec5SDimitry Andric return BuildUnresolvedCoawaitExpr(Loc, E, 8080b57cec5SDimitry Andric cast<UnresolvedLookupExpr>(Lookup.get())); 8090b57cec5SDimitry Andric } 8100b57cec5SDimitry Andric 81181ad6265SDimitry Andric ExprResult Sema::BuildOperatorCoawaitLookupExpr(Scope *S, SourceLocation Loc) { 81281ad6265SDimitry Andric DeclarationName OpName = 81381ad6265SDimitry Andric Context.DeclarationNames.getCXXOperatorName(OO_Coawait); 81481ad6265SDimitry Andric LookupResult Operators(*this, OpName, SourceLocation(), 81581ad6265SDimitry Andric Sema::LookupOperatorName); 81681ad6265SDimitry Andric LookupName(Operators, S); 81781ad6265SDimitry Andric 81881ad6265SDimitry Andric assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); 81981ad6265SDimitry Andric const auto &Functions = Operators.asUnresolvedSet(); 82081ad6265SDimitry Andric Expr *CoawaitOp = UnresolvedLookupExpr::Create( 82181ad6265SDimitry Andric Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(), 8220fca6ea1SDimitry Andric DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, Functions.begin(), 823*62987288SDimitry Andric Functions.end(), /*KnownDependent=*/false, 824*62987288SDimitry Andric /*KnownInstantiationDependent=*/false); 82581ad6265SDimitry Andric assert(CoawaitOp); 82681ad6265SDimitry Andric return CoawaitOp; 82781ad6265SDimitry Andric } 82881ad6265SDimitry Andric 82981ad6265SDimitry Andric // Attempts to resolve and build a CoawaitExpr from "raw" inputs, bailing out to 83081ad6265SDimitry Andric // DependentCoawaitExpr if needed. 83181ad6265SDimitry Andric ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *Operand, 8320b57cec5SDimitry Andric UnresolvedLookupExpr *Lookup) { 8330b57cec5SDimitry Andric auto *FSI = checkCoroutineContext(*this, Loc, "co_await"); 8340b57cec5SDimitry Andric if (!FSI) 8350b57cec5SDimitry Andric return ExprError(); 8360b57cec5SDimitry Andric 83781ad6265SDimitry Andric if (Operand->hasPlaceholderType()) { 83881ad6265SDimitry Andric ExprResult R = CheckPlaceholderExpr(Operand); 8390b57cec5SDimitry Andric if (R.isInvalid()) 8400b57cec5SDimitry Andric return ExprError(); 84181ad6265SDimitry Andric Operand = R.get(); 8420b57cec5SDimitry Andric } 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric auto *Promise = FSI->CoroutinePromise; 8450b57cec5SDimitry Andric if (Promise->getType()->isDependentType()) { 84681ad6265SDimitry Andric Expr *Res = new (Context) 84781ad6265SDimitry Andric DependentCoawaitExpr(Loc, Context.DependentTy, Operand, Lookup); 8480b57cec5SDimitry Andric return Res; 8490b57cec5SDimitry Andric } 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric auto *RD = Promise->getType()->getAsCXXRecordDecl(); 85281ad6265SDimitry Andric auto *Transformed = Operand; 8530b57cec5SDimitry Andric if (lookupMember(*this, "await_transform", RD, Loc)) { 85481ad6265SDimitry Andric ExprResult R = 85581ad6265SDimitry Andric buildPromiseCall(*this, Promise, Loc, "await_transform", Operand); 8560b57cec5SDimitry Andric if (R.isInvalid()) { 8570b57cec5SDimitry Andric Diag(Loc, 8580b57cec5SDimitry Andric diag::note_coroutine_promise_implicit_await_transform_required_here) 85981ad6265SDimitry Andric << Operand->getSourceRange(); 8600b57cec5SDimitry Andric return ExprError(); 8610b57cec5SDimitry Andric } 86281ad6265SDimitry Andric Transformed = R.get(); 8630b57cec5SDimitry Andric } 86481ad6265SDimitry Andric ExprResult Awaiter = BuildOperatorCoawaitCall(Loc, Transformed, Lookup); 86581ad6265SDimitry Andric if (Awaiter.isInvalid()) 8660b57cec5SDimitry Andric return ExprError(); 8670b57cec5SDimitry Andric 86881ad6265SDimitry Andric return BuildResolvedCoawaitExpr(Loc, Operand, Awaiter.get()); 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric 87181ad6265SDimitry Andric ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *Operand, 87281ad6265SDimitry Andric Expr *Awaiter, bool IsImplicit) { 8730b57cec5SDimitry Andric auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit); 8740b57cec5SDimitry Andric if (!Coroutine) 8750b57cec5SDimitry Andric return ExprError(); 8760b57cec5SDimitry Andric 87781ad6265SDimitry Andric if (Awaiter->hasPlaceholderType()) { 87881ad6265SDimitry Andric ExprResult R = CheckPlaceholderExpr(Awaiter); 8790b57cec5SDimitry Andric if (R.isInvalid()) return ExprError(); 88081ad6265SDimitry Andric Awaiter = R.get(); 8810b57cec5SDimitry Andric } 8820b57cec5SDimitry Andric 88381ad6265SDimitry Andric if (Awaiter->getType()->isDependentType()) { 8840b57cec5SDimitry Andric Expr *Res = new (Context) 88581ad6265SDimitry Andric CoawaitExpr(Loc, Context.DependentTy, Operand, Awaiter, IsImplicit); 8860b57cec5SDimitry Andric return Res; 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric // If the expression is a temporary, materialize it as an lvalue so that we 8900b57cec5SDimitry Andric // can use it multiple times. 89181ad6265SDimitry Andric if (Awaiter->isPRValue()) 89281ad6265SDimitry Andric Awaiter = CreateMaterializeTemporaryExpr(Awaiter->getType(), Awaiter, true); 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric // The location of the `co_await` token cannot be used when constructing 8950b57cec5SDimitry Andric // the member call expressions since it's before the location of `Expr`, which 8960b57cec5SDimitry Andric // is used as the start of the member call expression. 89781ad6265SDimitry Andric SourceLocation CallLoc = Awaiter->getExprLoc(); 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric // Build the await_ready, await_suspend, await_resume calls. 90081ad6265SDimitry Andric ReadySuspendResumeResult RSS = 90181ad6265SDimitry Andric buildCoawaitCalls(*this, Coroutine->CoroutinePromise, CallLoc, Awaiter); 9020b57cec5SDimitry Andric if (RSS.IsInvalid) 9030b57cec5SDimitry Andric return ExprError(); 9040b57cec5SDimitry Andric 90581ad6265SDimitry Andric Expr *Res = new (Context) 90681ad6265SDimitry Andric CoawaitExpr(Loc, Operand, Awaiter, RSS.Results[0], RSS.Results[1], 9070b57cec5SDimitry Andric RSS.Results[2], RSS.OpaqueValue, IsImplicit); 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric return Res; 9100b57cec5SDimitry Andric } 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) { 913bdd1243dSDimitry Andric if (!checkSuspensionContext(*this, Loc, "co_yield")) 914bdd1243dSDimitry Andric return ExprError(); 915bdd1243dSDimitry Andric 9160b57cec5SDimitry Andric if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) { 9170b57cec5SDimitry Andric CorrectDelayedTyposInExpr(E); 9180b57cec5SDimitry Andric return ExprError(); 9190b57cec5SDimitry Andric } 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric // Build yield_value call. 9220b57cec5SDimitry Andric ExprResult Awaitable = buildPromiseCall( 9230b57cec5SDimitry Andric *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E); 9240b57cec5SDimitry Andric if (Awaitable.isInvalid()) 9250b57cec5SDimitry Andric return ExprError(); 9260b57cec5SDimitry Andric 9270b57cec5SDimitry Andric // Build 'operator co_await' call. 9280b57cec5SDimitry Andric Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get()); 9290b57cec5SDimitry Andric if (Awaitable.isInvalid()) 9300b57cec5SDimitry Andric return ExprError(); 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric return BuildCoyieldExpr(Loc, Awaitable.get()); 9330b57cec5SDimitry Andric } 9340b57cec5SDimitry Andric ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) { 9350b57cec5SDimitry Andric auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield"); 9360b57cec5SDimitry Andric if (!Coroutine) 9370b57cec5SDimitry Andric return ExprError(); 9380b57cec5SDimitry Andric 9391fd87a68SDimitry Andric if (E->hasPlaceholderType()) { 9400b57cec5SDimitry Andric ExprResult R = CheckPlaceholderExpr(E); 9410b57cec5SDimitry Andric if (R.isInvalid()) return ExprError(); 9420b57cec5SDimitry Andric E = R.get(); 9430b57cec5SDimitry Andric } 9440b57cec5SDimitry Andric 94581ad6265SDimitry Andric Expr *Operand = E; 94681ad6265SDimitry Andric 9470b57cec5SDimitry Andric if (E->getType()->isDependentType()) { 94881ad6265SDimitry Andric Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, Operand, E); 9490b57cec5SDimitry Andric return Res; 9500b57cec5SDimitry Andric } 9510b57cec5SDimitry Andric 9520b57cec5SDimitry Andric // If the expression is a temporary, materialize it as an lvalue so that we 9530b57cec5SDimitry Andric // can use it multiple times. 954fe6060f1SDimitry Andric if (E->isPRValue()) 9550b57cec5SDimitry Andric E = CreateMaterializeTemporaryExpr(E->getType(), E, true); 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric // Build the await_ready, await_suspend, await_resume calls. 958e8d8bef9SDimitry Andric ReadySuspendResumeResult RSS = buildCoawaitCalls( 959e8d8bef9SDimitry Andric *this, Coroutine->CoroutinePromise, Loc, E); 9600b57cec5SDimitry Andric if (RSS.IsInvalid) 9610b57cec5SDimitry Andric return ExprError(); 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric Expr *Res = 96481ad6265SDimitry Andric new (Context) CoyieldExpr(Loc, Operand, E, RSS.Results[0], RSS.Results[1], 9650b57cec5SDimitry Andric RSS.Results[2], RSS.OpaqueValue); 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric return Res; 9680b57cec5SDimitry Andric } 9690b57cec5SDimitry Andric 9700b57cec5SDimitry Andric StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) { 9710b57cec5SDimitry Andric if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) { 9720b57cec5SDimitry Andric CorrectDelayedTyposInExpr(E); 9730b57cec5SDimitry Andric return StmtError(); 9740b57cec5SDimitry Andric } 9750b57cec5SDimitry Andric return BuildCoreturnStmt(Loc, E); 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E, 9790b57cec5SDimitry Andric bool IsImplicit) { 9800b57cec5SDimitry Andric auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit); 9810b57cec5SDimitry Andric if (!FSI) 9820b57cec5SDimitry Andric return StmtError(); 9830b57cec5SDimitry Andric 9841fd87a68SDimitry Andric if (E && E->hasPlaceholderType() && 9851fd87a68SDimitry Andric !E->hasPlaceholderType(BuiltinType::Overload)) { 9860b57cec5SDimitry Andric ExprResult R = CheckPlaceholderExpr(E); 9870b57cec5SDimitry Andric if (R.isInvalid()) return StmtError(); 9880b57cec5SDimitry Andric E = R.get(); 9890b57cec5SDimitry Andric } 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric VarDecl *Promise = FSI->CoroutinePromise; 9920b57cec5SDimitry Andric ExprResult PC; 9930b57cec5SDimitry Andric if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) { 994fe6060f1SDimitry Andric getNamedReturnInfo(E, SimplerImplicitMoveMode::ForceOn); 9950b57cec5SDimitry Andric PC = buildPromiseCall(*this, Promise, Loc, "return_value", E); 9960b57cec5SDimitry Andric } else { 9970b57cec5SDimitry Andric E = MakeFullDiscardedValueExpr(E).get(); 998bdd1243dSDimitry Andric PC = buildPromiseCall(*this, Promise, Loc, "return_void", std::nullopt); 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric if (PC.isInvalid()) 10010b57cec5SDimitry Andric return StmtError(); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric Expr *PCE = ActOnFinishFullExpr(PC.get(), /*DiscardedValue*/ false).get(); 10040b57cec5SDimitry Andric 10050b57cec5SDimitry Andric Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit); 10060b57cec5SDimitry Andric return Res; 10070b57cec5SDimitry Andric } 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric /// Look up the std::nothrow object. 10100b57cec5SDimitry Andric static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) { 10110b57cec5SDimitry Andric NamespaceDecl *Std = S.getStdNamespace(); 10120b57cec5SDimitry Andric assert(Std && "Should already be diagnosed"); 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc, 10150b57cec5SDimitry Andric Sema::LookupOrdinaryName); 10160b57cec5SDimitry Andric if (!S.LookupQualifiedName(Result, Std)) { 10170eae32dcSDimitry Andric // <coroutine> is not requred to include <new>, so we couldn't omit 10180eae32dcSDimitry Andric // the check here. 10190b57cec5SDimitry Andric S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found); 10200b57cec5SDimitry Andric return nullptr; 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric auto *VD = Result.getAsSingle<VarDecl>(); 10240b57cec5SDimitry Andric if (!VD) { 10250b57cec5SDimitry Andric Result.suppressDiagnostics(); 10260b57cec5SDimitry Andric // We found something weird. Complain about the first thing we found. 10270b57cec5SDimitry Andric NamedDecl *Found = *Result.begin(); 10280b57cec5SDimitry Andric S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow); 10290b57cec5SDimitry Andric return nullptr; 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc); 10330b57cec5SDimitry Andric if (DR.isInvalid()) 10340b57cec5SDimitry Andric return nullptr; 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric return DR.get(); 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric 1039bdd1243dSDimitry Andric static TypeSourceInfo *getTypeSourceInfoForStdAlignValT(Sema &S, 1040bdd1243dSDimitry Andric SourceLocation Loc) { 1041bdd1243dSDimitry Andric EnumDecl *StdAlignValT = S.getStdAlignValT(); 1042bdd1243dSDimitry Andric QualType StdAlignValDecl = S.Context.getTypeDeclType(StdAlignValT); 1043bdd1243dSDimitry Andric return S.Context.getTrivialTypeSourceInfo(StdAlignValDecl); 1044bdd1243dSDimitry Andric } 10450b57cec5SDimitry Andric 1046bdd1243dSDimitry Andric // Find an appropriate delete for the promise. 1047bdd1243dSDimitry Andric static bool findDeleteForPromise(Sema &S, SourceLocation Loc, QualType PromiseType, 1048bdd1243dSDimitry Andric FunctionDecl *&OperatorDelete) { 10490b57cec5SDimitry Andric DeclarationName DeleteName = 10500b57cec5SDimitry Andric S.Context.DeclarationNames.getCXXOperatorName(OO_Delete); 10510b57cec5SDimitry Andric 10520b57cec5SDimitry Andric auto *PointeeRD = PromiseType->getAsCXXRecordDecl(); 10530b57cec5SDimitry Andric assert(PointeeRD && "PromiseType must be a CxxRecordDecl type"); 10540b57cec5SDimitry Andric 1055bdd1243dSDimitry Andric const bool Overaligned = S.getLangOpts().CoroAlignedAllocation; 1056bdd1243dSDimitry Andric 10570eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p12 10580eae32dcSDimitry Andric // The deallocation function's name is looked up by searching for it in the 10590eae32dcSDimitry Andric // scope of the promise type. If nothing is found, a search is performed in 10600eae32dcSDimitry Andric // the global scope. 1061bdd1243dSDimitry Andric if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete, 1062bdd1243dSDimitry Andric /*Diagnose*/ true, /*WantSize*/ true, 1063bdd1243dSDimitry Andric /*WantAligned*/ Overaligned)) 1064bdd1243dSDimitry Andric return false; 10650b57cec5SDimitry Andric 10660eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p12 10670eae32dcSDimitry Andric // If both a usual deallocation function with only a pointer parameter and a 10680eae32dcSDimitry Andric // usual deallocation function with both a pointer parameter and a size 10690eae32dcSDimitry Andric // parameter are found, then the selected deallocation function shall be the 10700eae32dcSDimitry Andric // one with two parameters. Otherwise, the selected deallocation function 10710eae32dcSDimitry Andric // shall be the function with one parameter. 10720b57cec5SDimitry Andric if (!OperatorDelete) { 10730b57cec5SDimitry Andric // Look for a global declaration. 1074bdd1243dSDimitry Andric // Coroutines can always provide their required size. 1075bdd1243dSDimitry Andric const bool CanProvideSize = true; 1076bdd1243dSDimitry Andric // Sema::FindUsualDeallocationFunction will try to find the one with two 1077bdd1243dSDimitry Andric // parameters first. It will return the deallocation function with one 1078bdd1243dSDimitry Andric // parameter if failed. 10790b57cec5SDimitry Andric OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize, 10800b57cec5SDimitry Andric Overaligned, DeleteName); 1081bdd1243dSDimitry Andric 1082bdd1243dSDimitry Andric if (!OperatorDelete) 1083bdd1243dSDimitry Andric return false; 10840b57cec5SDimitry Andric } 1085bdd1243dSDimitry Andric 10860b57cec5SDimitry Andric S.MarkFunctionReferenced(Loc, OperatorDelete); 1087bdd1243dSDimitry Andric return true; 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) { 10920b57cec5SDimitry Andric FunctionScopeInfo *Fn = getCurFunction(); 10930b57cec5SDimitry Andric assert(Fn && Fn->isCoroutine() && "not a coroutine"); 10940b57cec5SDimitry Andric if (!Body) { 10950b57cec5SDimitry Andric assert(FD->isInvalidDecl() && 10960b57cec5SDimitry Andric "a null body is only allowed for invalid declarations"); 10970b57cec5SDimitry Andric return; 10980b57cec5SDimitry Andric } 10990b57cec5SDimitry Andric // We have a function that uses coroutine keywords, but we failed to build 11000b57cec5SDimitry Andric // the promise type. 11010b57cec5SDimitry Andric if (!Fn->CoroutinePromise) 11020b57cec5SDimitry Andric return FD->setInvalidDecl(); 11030b57cec5SDimitry Andric 11040b57cec5SDimitry Andric if (isa<CoroutineBodyStmt>(Body)) { 11050b57cec5SDimitry Andric // Nothing todo. the body is already a transformed coroutine body statement. 11060b57cec5SDimitry Andric return; 11070b57cec5SDimitry Andric } 11080b57cec5SDimitry Andric 110981ad6265SDimitry Andric // The always_inline attribute doesn't reliably apply to a coroutine, 111081ad6265SDimitry Andric // because the coroutine will be split into pieces and some pieces 111181ad6265SDimitry Andric // might be called indirectly, as in a virtual call. Even the ramp 111281ad6265SDimitry Andric // function cannot be inlined at -O0, due to pipeline ordering 111381ad6265SDimitry Andric // problems (see https://llvm.org/PR53413). Tell the user about it. 111481ad6265SDimitry Andric if (FD->hasAttr<AlwaysInlineAttr>()) 111581ad6265SDimitry Andric Diag(FD->getLocation(), diag::warn_always_inline_coroutine); 111681ad6265SDimitry Andric 11175f757f3fSDimitry Andric // The design of coroutines means we cannot allow use of VLAs within one, so 11185f757f3fSDimitry Andric // diagnose if we've seen a VLA in the body of this function. 11195f757f3fSDimitry Andric if (Fn->FirstVLALoc.isValid()) 11205f757f3fSDimitry Andric Diag(Fn->FirstVLALoc, diag::err_vla_in_coroutine_unsupported); 11215f757f3fSDimitry Andric 11220eae32dcSDimitry Andric // [stmt.return.coroutine]p1: 11230eae32dcSDimitry Andric // A coroutine shall not enclose a return statement ([stmt.return]). 11240b57cec5SDimitry Andric if (Fn->FirstReturnLoc.isValid()) { 11250b57cec5SDimitry Andric assert(Fn->FirstCoroutineStmtLoc.isValid() && 11260b57cec5SDimitry Andric "first coroutine location not set"); 11270b57cec5SDimitry Andric Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine); 11280b57cec5SDimitry Andric Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) 11290b57cec5SDimitry Andric << Fn->getFirstCoroutineStmtKeyword(); 11300b57cec5SDimitry Andric } 1131bdd1243dSDimitry Andric 1132bdd1243dSDimitry Andric // Coroutines will get splitted into pieces. The GNU address of label 1133bdd1243dSDimitry Andric // extension wouldn't be meaningful in coroutines. 1134bdd1243dSDimitry Andric for (AddrLabelExpr *ALE : Fn->AddrLabels) 1135bdd1243dSDimitry Andric Diag(ALE->getBeginLoc(), diag::err_coro_invalid_addr_of_label); 1136bdd1243dSDimitry Andric 11370b57cec5SDimitry Andric CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body); 11380b57cec5SDimitry Andric if (Builder.isInvalid() || !Builder.buildStatements()) 11390b57cec5SDimitry Andric return FD->setInvalidDecl(); 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric // Build body for the coroutine wrapper statement. 11420b57cec5SDimitry Andric Body = CoroutineBodyStmt::Create(Context, Builder); 11430b57cec5SDimitry Andric } 11440b57cec5SDimitry Andric 114506c3fb27SDimitry Andric static CompoundStmt *buildCoroutineBody(Stmt *Body, ASTContext &Context) { 114606c3fb27SDimitry Andric if (auto *CS = dyn_cast<CompoundStmt>(Body)) 114706c3fb27SDimitry Andric return CS; 114806c3fb27SDimitry Andric 114906c3fb27SDimitry Andric // The body of the coroutine may be a try statement if it is in 115006c3fb27SDimitry Andric // 'function-try-block' syntax. Here we wrap it into a compound 115106c3fb27SDimitry Andric // statement for consistency. 115206c3fb27SDimitry Andric assert(isa<CXXTryStmt>(Body) && "Unimaged coroutine body type"); 115306c3fb27SDimitry Andric return CompoundStmt::Create(Context, {Body}, FPOptionsOverride(), 115406c3fb27SDimitry Andric SourceLocation(), SourceLocation()); 115506c3fb27SDimitry Andric } 115606c3fb27SDimitry Andric 11570b57cec5SDimitry Andric CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD, 11580b57cec5SDimitry Andric sema::FunctionScopeInfo &Fn, 11590b57cec5SDimitry Andric Stmt *Body) 11600b57cec5SDimitry Andric : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()), 11610b57cec5SDimitry Andric IsPromiseDependentType( 11620b57cec5SDimitry Andric !Fn.CoroutinePromise || 11630b57cec5SDimitry Andric Fn.CoroutinePromise->getType()->isDependentType()) { 116406c3fb27SDimitry Andric this->Body = buildCoroutineBody(Body, S.getASTContext()); 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric for (auto KV : Fn.CoroutineParameterMoves) 11670b57cec5SDimitry Andric this->ParamMovesVector.push_back(KV.second); 11680b57cec5SDimitry Andric this->ParamMoves = this->ParamMovesVector; 11690b57cec5SDimitry Andric 11700b57cec5SDimitry Andric if (!IsPromiseDependentType) { 11710b57cec5SDimitry Andric PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl(); 11720b57cec5SDimitry Andric assert(PromiseRecordDecl && "Type should have already been checked"); 11730b57cec5SDimitry Andric } 11740b57cec5SDimitry Andric this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend(); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric bool CoroutineStmtBuilder::buildStatements() { 11780b57cec5SDimitry Andric assert(this->IsValid && "coroutine already invalid"); 11790b57cec5SDimitry Andric this->IsValid = makeReturnObject(); 11800b57cec5SDimitry Andric if (this->IsValid && !IsPromiseDependentType) 11810b57cec5SDimitry Andric buildDependentStatements(); 11820b57cec5SDimitry Andric return this->IsValid; 11830b57cec5SDimitry Andric } 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric bool CoroutineStmtBuilder::buildDependentStatements() { 11860b57cec5SDimitry Andric assert(this->IsValid && "coroutine already invalid"); 11870b57cec5SDimitry Andric assert(!this->IsPromiseDependentType && 11880b57cec5SDimitry Andric "coroutine cannot have a dependent promise type"); 11890b57cec5SDimitry Andric this->IsValid = makeOnException() && makeOnFallthrough() && 11900b57cec5SDimitry Andric makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() && 11910b57cec5SDimitry Andric makeNewAndDeleteExpr(); 11920b57cec5SDimitry Andric return this->IsValid; 11930b57cec5SDimitry Andric } 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric bool CoroutineStmtBuilder::makePromiseStmt() { 11960b57cec5SDimitry Andric // Form a declaration statement for the promise declaration, so that AST 11970b57cec5SDimitry Andric // visitors can more easily find it. 11980b57cec5SDimitry Andric StmtResult PromiseStmt = 11990b57cec5SDimitry Andric S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc); 12000b57cec5SDimitry Andric if (PromiseStmt.isInvalid()) 12010b57cec5SDimitry Andric return false; 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric this->Promise = PromiseStmt.get(); 12040b57cec5SDimitry Andric return true; 12050b57cec5SDimitry Andric } 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() { 12080b57cec5SDimitry Andric if (Fn.hasInvalidCoroutineSuspends()) 12090b57cec5SDimitry Andric return false; 12100b57cec5SDimitry Andric this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first); 12110b57cec5SDimitry Andric this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second); 12120b57cec5SDimitry Andric return true; 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric static bool diagReturnOnAllocFailure(Sema &S, Expr *E, 12160b57cec5SDimitry Andric CXXRecordDecl *PromiseRecordDecl, 12170b57cec5SDimitry Andric FunctionScopeInfo &Fn) { 12180b57cec5SDimitry Andric auto Loc = E->getExprLoc(); 12190b57cec5SDimitry Andric if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) { 12200b57cec5SDimitry Andric auto *Decl = DeclRef->getDecl(); 12210b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) { 12220b57cec5SDimitry Andric if (Method->isStatic()) 12230b57cec5SDimitry Andric return true; 12240b57cec5SDimitry Andric else 12250b57cec5SDimitry Andric Loc = Decl->getLocation(); 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric } 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric S.Diag( 12300b57cec5SDimitry Andric Loc, 12310b57cec5SDimitry Andric diag::err_coroutine_promise_get_return_object_on_allocation_failure) 12320b57cec5SDimitry Andric << PromiseRecordDecl; 12330b57cec5SDimitry Andric S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) 12340b57cec5SDimitry Andric << Fn.getFirstCoroutineStmtKeyword(); 12350b57cec5SDimitry Andric return false; 12360b57cec5SDimitry Andric } 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeReturnOnAllocFailure() { 12390b57cec5SDimitry Andric assert(!IsPromiseDependentType && 12400b57cec5SDimitry Andric "cannot make statement while the promise type is dependent"); 12410b57cec5SDimitry Andric 12420eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p10 124304eeddc0SDimitry Andric // If a search for the name get_return_object_on_allocation_failure in 12440eae32dcSDimitry Andric // the scope of the promise type ([class.member.lookup]) finds any 12450eae32dcSDimitry Andric // declarations, then the result of a call to an allocation function used to 12460eae32dcSDimitry Andric // obtain storage for the coroutine state is assumed to return nullptr if it 12470eae32dcSDimitry Andric // fails to obtain storage, ... If the allocation function returns nullptr, 12480eae32dcSDimitry Andric // ... and the return value is obtained by a call to 124904eeddc0SDimitry Andric // T::get_return_object_on_allocation_failure(), where T is the 12500eae32dcSDimitry Andric // promise type. 12510b57cec5SDimitry Andric DeclarationName DN = 12520b57cec5SDimitry Andric S.PP.getIdentifierInfo("get_return_object_on_allocation_failure"); 12530b57cec5SDimitry Andric LookupResult Found(S, DN, Loc, Sema::LookupMemberName); 12540b57cec5SDimitry Andric if (!S.LookupQualifiedName(Found, PromiseRecordDecl)) 12550b57cec5SDimitry Andric return true; 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric CXXScopeSpec SS; 12580b57cec5SDimitry Andric ExprResult DeclNameExpr = 12590b57cec5SDimitry Andric S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); 12600b57cec5SDimitry Andric if (DeclNameExpr.isInvalid()) 12610b57cec5SDimitry Andric return false; 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn)) 12640b57cec5SDimitry Andric return false; 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric ExprResult ReturnObjectOnAllocationFailure = 12670b57cec5SDimitry Andric S.BuildCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc); 12680b57cec5SDimitry Andric if (ReturnObjectOnAllocationFailure.isInvalid()) 12690b57cec5SDimitry Andric return false; 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric StmtResult ReturnStmt = 12720b57cec5SDimitry Andric S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get()); 12730b57cec5SDimitry Andric if (ReturnStmt.isInvalid()) { 12740b57cec5SDimitry Andric S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here) 12750b57cec5SDimitry Andric << DN; 12760b57cec5SDimitry Andric S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) 12770b57cec5SDimitry Andric << Fn.getFirstCoroutineStmtKeyword(); 12780b57cec5SDimitry Andric return false; 12790b57cec5SDimitry Andric } 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric this->ReturnStmtOnAllocFailure = ReturnStmt.get(); 12820b57cec5SDimitry Andric return true; 12830b57cec5SDimitry Andric } 12840b57cec5SDimitry Andric 128581ad6265SDimitry Andric // Collect placement arguments for allocation function of coroutine FD. 128681ad6265SDimitry Andric // Return true if we collect placement arguments succesfully. Return false, 128781ad6265SDimitry Andric // otherwise. 128881ad6265SDimitry Andric static bool collectPlacementArgs(Sema &S, FunctionDecl &FD, SourceLocation Loc, 128981ad6265SDimitry Andric SmallVectorImpl<Expr *> &PlacementArgs) { 129081ad6265SDimitry Andric if (auto *MD = dyn_cast<CXXMethodDecl>(&FD)) { 12915f757f3fSDimitry Andric if (MD->isImplicitObjectMemberFunction() && !isLambdaCallOperator(MD)) { 129281ad6265SDimitry Andric ExprResult ThisExpr = S.ActOnCXXThis(Loc); 129381ad6265SDimitry Andric if (ThisExpr.isInvalid()) 129481ad6265SDimitry Andric return false; 129581ad6265SDimitry Andric ThisExpr = S.CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get()); 129681ad6265SDimitry Andric if (ThisExpr.isInvalid()) 129781ad6265SDimitry Andric return false; 129881ad6265SDimitry Andric PlacementArgs.push_back(ThisExpr.get()); 129981ad6265SDimitry Andric } 130081ad6265SDimitry Andric } 130181ad6265SDimitry Andric 130281ad6265SDimitry Andric for (auto *PD : FD.parameters()) { 130381ad6265SDimitry Andric if (PD->getType()->isDependentType()) 130481ad6265SDimitry Andric continue; 130581ad6265SDimitry Andric 130681ad6265SDimitry Andric // Build a reference to the parameter. 130781ad6265SDimitry Andric auto PDLoc = PD->getLocation(); 130881ad6265SDimitry Andric ExprResult PDRefExpr = 130981ad6265SDimitry Andric S.BuildDeclRefExpr(PD, PD->getOriginalType().getNonReferenceType(), 131081ad6265SDimitry Andric ExprValueKind::VK_LValue, PDLoc); 131181ad6265SDimitry Andric if (PDRefExpr.isInvalid()) 131281ad6265SDimitry Andric return false; 131381ad6265SDimitry Andric 131481ad6265SDimitry Andric PlacementArgs.push_back(PDRefExpr.get()); 131581ad6265SDimitry Andric } 131681ad6265SDimitry Andric 131781ad6265SDimitry Andric return true; 131881ad6265SDimitry Andric } 131981ad6265SDimitry Andric 13200b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeNewAndDeleteExpr() { 13210b57cec5SDimitry Andric // Form and check allocation and deallocation calls. 13220b57cec5SDimitry Andric assert(!IsPromiseDependentType && 13230b57cec5SDimitry Andric "cannot make statement while the promise type is dependent"); 13240b57cec5SDimitry Andric QualType PromiseType = Fn.CoroutinePromise->getType(); 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type)) 13270b57cec5SDimitry Andric return false; 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr; 13300b57cec5SDimitry Andric 13310eae32dcSDimitry Andric // According to [dcl.fct.def.coroutine]p9, Lookup allocation functions using a 13320eae32dcSDimitry Andric // parameter list composed of the requested size of the coroutine state being 13330eae32dcSDimitry Andric // allocated, followed by the coroutine function's arguments. If a matching 13340eae32dcSDimitry Andric // allocation function exists, use it. Otherwise, use an allocation function 13350eae32dcSDimitry Andric // that just takes the requested size. 133681ad6265SDimitry Andric // 13370eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p9 13380eae32dcSDimitry Andric // An implementation may need to allocate additional storage for a 13390eae32dcSDimitry Andric // coroutine. 13400eae32dcSDimitry Andric // This storage is known as the coroutine state and is obtained by calling a 13410eae32dcSDimitry Andric // non-array allocation function ([basic.stc.dynamic.allocation]). The 13420eae32dcSDimitry Andric // allocation function's name is looked up by searching for it in the scope of 13430eae32dcSDimitry Andric // the promise type. 13440eae32dcSDimitry Andric // - If any declarations are found, overload resolution is performed on a 13450eae32dcSDimitry Andric // function call created by assembling an argument list. The first argument is 13460eae32dcSDimitry Andric // the amount of space requested, and has type std::size_t. The 13470eae32dcSDimitry Andric // lvalues p1 ... pn are the succeeding arguments. 13480b57cec5SDimitry Andric // 13490b57cec5SDimitry Andric // ...where "p1 ... pn" are defined earlier as: 13500b57cec5SDimitry Andric // 13510eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p3 13520eae32dcSDimitry Andric // The promise type of a coroutine is `std::coroutine_traits<R, P1, ..., 13530eae32dcSDimitry Andric // Pn>` 13540eae32dcSDimitry Andric // , where R is the return type of the function, and `P1, ..., Pn` are the 13550eae32dcSDimitry Andric // sequence of types of the non-object function parameters, preceded by the 13560eae32dcSDimitry Andric // type of the object parameter ([dcl.fct]) if the coroutine is a non-static 13570eae32dcSDimitry Andric // member function. [dcl.fct.def.coroutine]p4 In the following, p_i is an 13580eae32dcSDimitry Andric // lvalue of type P_i, where p1 denotes the object parameter and p_i+1 denotes 13590eae32dcSDimitry Andric // the i-th non-object function parameter for a non-static member function, 13600eae32dcSDimitry Andric // and p_i denotes the i-th function parameter otherwise. For a non-static 13610eae32dcSDimitry Andric // member function, q_1 is an lvalue that denotes *this; any other q_i is an 13620eae32dcSDimitry Andric // lvalue that denotes the parameter copy corresponding to p_i. 13630b57cec5SDimitry Andric 136481ad6265SDimitry Andric FunctionDecl *OperatorNew = nullptr; 136581ad6265SDimitry Andric SmallVector<Expr *, 1> PlacementArgs; 13660b57cec5SDimitry Andric 1367bdd1243dSDimitry Andric const bool PromiseContainsNew = [this, &PromiseType]() -> bool { 136881ad6265SDimitry Andric DeclarationName NewName = 136981ad6265SDimitry Andric S.getASTContext().DeclarationNames.getCXXOperatorName(OO_New); 137081ad6265SDimitry Andric LookupResult R(S, NewName, Loc, Sema::LookupOrdinaryName); 137181ad6265SDimitry Andric 137281ad6265SDimitry Andric if (PromiseType->isRecordType()) 137381ad6265SDimitry Andric S.LookupQualifiedName(R, PromiseType->getAsCXXRecordDecl()); 137481ad6265SDimitry Andric 137581ad6265SDimitry Andric return !R.empty() && !R.isAmbiguous(); 137681ad6265SDimitry Andric }(); 137781ad6265SDimitry Andric 1378bdd1243dSDimitry Andric // Helper function to indicate whether the last lookup found the aligned 1379bdd1243dSDimitry Andric // allocation function. 1380bdd1243dSDimitry Andric bool PassAlignment = S.getLangOpts().CoroAlignedAllocation; 1381bdd1243dSDimitry Andric auto LookupAllocationFunction = [&](Sema::AllocationFunctionScope NewScope = 1382bdd1243dSDimitry Andric Sema::AFS_Both, 1383bdd1243dSDimitry Andric bool WithoutPlacementArgs = false, 1384bdd1243dSDimitry Andric bool ForceNonAligned = false) { 138581ad6265SDimitry Andric // [dcl.fct.def.coroutine]p9 138681ad6265SDimitry Andric // The allocation function's name is looked up by searching for it in the 138781ad6265SDimitry Andric // scope of the promise type. 138881ad6265SDimitry Andric // - If any declarations are found, ... 138981ad6265SDimitry Andric // - If no declarations are found in the scope of the promise type, a search 139081ad6265SDimitry Andric // is performed in the global scope. 1391bdd1243dSDimitry Andric if (NewScope == Sema::AFS_Both) 1392bdd1243dSDimitry Andric NewScope = PromiseContainsNew ? Sema::AFS_Class : Sema::AFS_Global; 1393bdd1243dSDimitry Andric 1394bdd1243dSDimitry Andric PassAlignment = !ForceNonAligned && S.getLangOpts().CoroAlignedAllocation; 1395bdd1243dSDimitry Andric FunctionDecl *UnusedResult = nullptr; 1396bdd1243dSDimitry Andric S.FindAllocationFunctions(Loc, SourceRange(), NewScope, 13970b57cec5SDimitry Andric /*DeleteScope*/ Sema::AFS_Both, PromiseType, 1398bdd1243dSDimitry Andric /*isArray*/ false, PassAlignment, 1399bdd1243dSDimitry Andric WithoutPlacementArgs ? MultiExprArg{} 1400bdd1243dSDimitry Andric : PlacementArgs, 14010b57cec5SDimitry Andric OperatorNew, UnusedResult, /*Diagnose*/ false); 140281ad6265SDimitry Andric }; 140381ad6265SDimitry Andric 140481ad6265SDimitry Andric // We don't expect to call to global operator new with (size, p0, …, pn). 140581ad6265SDimitry Andric // So if we choose to lookup the allocation function in global scope, we 140681ad6265SDimitry Andric // shouldn't lookup placement arguments. 140781ad6265SDimitry Andric if (PromiseContainsNew && !collectPlacementArgs(S, FD, Loc, PlacementArgs)) 140881ad6265SDimitry Andric return false; 140981ad6265SDimitry Andric 141081ad6265SDimitry Andric LookupAllocationFunction(); 14110b57cec5SDimitry Andric 1412bdd1243dSDimitry Andric if (PromiseContainsNew && !PlacementArgs.empty()) { 14130eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p9 1414bdd1243dSDimitry Andric // If no viable function is found ([over.match.viable]), overload 1415bdd1243dSDimitry Andric // resolution 1416bdd1243dSDimitry Andric // is performed again on a function call created by passing just the amount 1417bdd1243dSDimitry Andric // of space required as an argument of type std::size_t. 1418bdd1243dSDimitry Andric // 1419bdd1243dSDimitry Andric // Proposed Change of [dcl.fct.def.coroutine]p9 in P2014R0: 1420bdd1243dSDimitry Andric // Otherwise, overload resolution is performed again on a function call 1421bdd1243dSDimitry Andric // created 1422bdd1243dSDimitry Andric // by passing the amount of space requested as an argument of type 1423bdd1243dSDimitry Andric // std::size_t as the first argument, and the requested alignment as 1424bdd1243dSDimitry Andric // an argument of type std:align_val_t as the second argument. 1425bdd1243dSDimitry Andric if (!OperatorNew || 1426bdd1243dSDimitry Andric (S.getLangOpts().CoroAlignedAllocation && !PassAlignment)) 1427bdd1243dSDimitry Andric LookupAllocationFunction(/*NewScope*/ Sema::AFS_Class, 1428bdd1243dSDimitry Andric /*WithoutPlacementArgs*/ true); 1429bdd1243dSDimitry Andric } 1430bdd1243dSDimitry Andric 1431bdd1243dSDimitry Andric // Proposed Change of [dcl.fct.def.coroutine]p12 in P2014R0: 1432bdd1243dSDimitry Andric // Otherwise, overload resolution is performed again on a function call 1433bdd1243dSDimitry Andric // created 1434bdd1243dSDimitry Andric // by passing the amount of space requested as an argument of type 1435bdd1243dSDimitry Andric // std::size_t as the first argument, and the lvalues p1 ... pn as the 1436bdd1243dSDimitry Andric // succeeding arguments. Otherwise, overload resolution is performed again 1437bdd1243dSDimitry Andric // on a function call created by passing just the amount of space required as 1438bdd1243dSDimitry Andric // an argument of type std::size_t. 1439bdd1243dSDimitry Andric // 1440bdd1243dSDimitry Andric // So within the proposed change in P2014RO, the priority order of aligned 1441bdd1243dSDimitry Andric // allocation functions wiht promise_type is: 1442bdd1243dSDimitry Andric // 1443bdd1243dSDimitry Andric // void* operator new( std::size_t, std::align_val_t, placement_args... ); 1444bdd1243dSDimitry Andric // void* operator new( std::size_t, std::align_val_t); 1445bdd1243dSDimitry Andric // void* operator new( std::size_t, placement_args... ); 1446bdd1243dSDimitry Andric // void* operator new( std::size_t); 1447bdd1243dSDimitry Andric 1448bdd1243dSDimitry Andric // Helper variable to emit warnings. 1449bdd1243dSDimitry Andric bool FoundNonAlignedInPromise = false; 1450bdd1243dSDimitry Andric if (PromiseContainsNew && S.getLangOpts().CoroAlignedAllocation) 1451bdd1243dSDimitry Andric if (!OperatorNew || !PassAlignment) { 1452bdd1243dSDimitry Andric FoundNonAlignedInPromise = OperatorNew; 1453bdd1243dSDimitry Andric 1454bdd1243dSDimitry Andric LookupAllocationFunction(/*NewScope*/ Sema::AFS_Class, 1455bdd1243dSDimitry Andric /*WithoutPlacementArgs*/ false, 1456bdd1243dSDimitry Andric /*ForceNonAligned*/ true); 1457bdd1243dSDimitry Andric 1458bdd1243dSDimitry Andric if (!OperatorNew && !PlacementArgs.empty()) 1459bdd1243dSDimitry Andric LookupAllocationFunction(/*NewScope*/ Sema::AFS_Class, 1460bdd1243dSDimitry Andric /*WithoutPlacementArgs*/ true, 1461bdd1243dSDimitry Andric /*ForceNonAligned*/ true); 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric bool IsGlobalOverload = 14650b57cec5SDimitry Andric OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext()); 14660b57cec5SDimitry Andric // If we didn't find a class-local new declaration and non-throwing new 14670b57cec5SDimitry Andric // was is required then we need to lookup the non-throwing global operator 14680b57cec5SDimitry Andric // instead. 14690b57cec5SDimitry Andric if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) { 14700b57cec5SDimitry Andric auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc); 14710b57cec5SDimitry Andric if (!StdNoThrow) 14720b57cec5SDimitry Andric return false; 14730b57cec5SDimitry Andric PlacementArgs = {StdNoThrow}; 14740b57cec5SDimitry Andric OperatorNew = nullptr; 1475bdd1243dSDimitry Andric LookupAllocationFunction(Sema::AFS_Global); 1476bdd1243dSDimitry Andric } 1477bdd1243dSDimitry Andric 1478bdd1243dSDimitry Andric // If we found a non-aligned allocation function in the promise_type, 1479bdd1243dSDimitry Andric // it indicates the user forgot to update the allocation function. Let's emit 1480bdd1243dSDimitry Andric // a warning here. 1481bdd1243dSDimitry Andric if (FoundNonAlignedInPromise) { 1482bdd1243dSDimitry Andric S.Diag(OperatorNew->getLocation(), 1483bdd1243dSDimitry Andric diag::warn_non_aligned_allocation_function) 1484bdd1243dSDimitry Andric << &FD; 14850b57cec5SDimitry Andric } 14860b57cec5SDimitry Andric 148781ad6265SDimitry Andric if (!OperatorNew) { 148881ad6265SDimitry Andric if (PromiseContainsNew) 148981ad6265SDimitry Andric S.Diag(Loc, diag::err_coroutine_unusable_new) << PromiseType << &FD; 1490bdd1243dSDimitry Andric else if (RequiresNoThrowAlloc) 1491bdd1243dSDimitry Andric S.Diag(Loc, diag::err_coroutine_unfound_nothrow_new) 1492bdd1243dSDimitry Andric << &FD << S.getLangOpts().CoroAlignedAllocation; 149381ad6265SDimitry Andric 14940b57cec5SDimitry Andric return false; 149581ad6265SDimitry Andric } 14960b57cec5SDimitry Andric 14970b57cec5SDimitry Andric if (RequiresNoThrowAlloc) { 1498480093f4SDimitry Andric const auto *FT = OperatorNew->getType()->castAs<FunctionProtoType>(); 14990b57cec5SDimitry Andric if (!FT->isNothrow(/*ResultIfDependent*/ false)) { 15000b57cec5SDimitry Andric S.Diag(OperatorNew->getLocation(), 15010b57cec5SDimitry Andric diag::err_coroutine_promise_new_requires_nothrow) 15020b57cec5SDimitry Andric << OperatorNew; 15030b57cec5SDimitry Andric S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) 15040b57cec5SDimitry Andric << OperatorNew; 15050b57cec5SDimitry Andric return false; 15060b57cec5SDimitry Andric } 15070b57cec5SDimitry Andric } 15080b57cec5SDimitry Andric 1509bdd1243dSDimitry Andric FunctionDecl *OperatorDelete = nullptr; 1510bdd1243dSDimitry Andric if (!findDeleteForPromise(S, Loc, PromiseType, OperatorDelete)) { 15110eae32dcSDimitry Andric // FIXME: We should add an error here. According to: 15120eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p12 15130eae32dcSDimitry Andric // If no usual deallocation function is found, the program is ill-formed. 15140b57cec5SDimitry Andric return false; 15150eae32dcSDimitry Andric } 15160b57cec5SDimitry Andric 15170b57cec5SDimitry Andric Expr *FramePtr = 1518fe6060f1SDimitry Andric S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_frame, {}); 15190b57cec5SDimitry Andric 15200b57cec5SDimitry Andric Expr *FrameSize = 1521fe6060f1SDimitry Andric S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_size, {}); 15220b57cec5SDimitry Andric 1523bdd1243dSDimitry Andric Expr *FrameAlignment = nullptr; 15240b57cec5SDimitry Andric 1525bdd1243dSDimitry Andric if (S.getLangOpts().CoroAlignedAllocation) { 1526bdd1243dSDimitry Andric FrameAlignment = 1527bdd1243dSDimitry Andric S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_align, {}); 1528bdd1243dSDimitry Andric 1529bdd1243dSDimitry Andric TypeSourceInfo *AlignValTy = getTypeSourceInfoForStdAlignValT(S, Loc); 1530bdd1243dSDimitry Andric if (!AlignValTy) 1531bdd1243dSDimitry Andric return false; 1532bdd1243dSDimitry Andric 1533bdd1243dSDimitry Andric FrameAlignment = S.BuildCXXNamedCast(Loc, tok::kw_static_cast, AlignValTy, 1534bdd1243dSDimitry Andric FrameAlignment, SourceRange(Loc, Loc), 1535bdd1243dSDimitry Andric SourceRange(Loc, Loc)) 1536bdd1243dSDimitry Andric .get(); 1537bdd1243dSDimitry Andric } 1538bdd1243dSDimitry Andric 1539bdd1243dSDimitry Andric // Make new call. 15400b57cec5SDimitry Andric ExprResult NewRef = 15410b57cec5SDimitry Andric S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc); 15420b57cec5SDimitry Andric if (NewRef.isInvalid()) 15430b57cec5SDimitry Andric return false; 15440b57cec5SDimitry Andric 15450b57cec5SDimitry Andric SmallVector<Expr *, 2> NewArgs(1, FrameSize); 1546bdd1243dSDimitry Andric if (S.getLangOpts().CoroAlignedAllocation && PassAlignment) 1547bdd1243dSDimitry Andric NewArgs.push_back(FrameAlignment); 1548bdd1243dSDimitry Andric 1549bdd1243dSDimitry Andric if (OperatorNew->getNumParams() > NewArgs.size()) 155081ad6265SDimitry Andric llvm::append_range(NewArgs, PlacementArgs); 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric ExprResult NewExpr = 15530b57cec5SDimitry Andric S.BuildCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc); 15540b57cec5SDimitry Andric NewExpr = S.ActOnFinishFullExpr(NewExpr.get(), /*DiscardedValue*/ false); 15550b57cec5SDimitry Andric if (NewExpr.isInvalid()) 15560b57cec5SDimitry Andric return false; 15570b57cec5SDimitry Andric 15580b57cec5SDimitry Andric // Make delete call. 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric QualType OpDeleteQualType = OperatorDelete->getType(); 15610b57cec5SDimitry Andric 15620b57cec5SDimitry Andric ExprResult DeleteRef = 15630b57cec5SDimitry Andric S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc); 15640b57cec5SDimitry Andric if (DeleteRef.isInvalid()) 15650b57cec5SDimitry Andric return false; 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric Expr *CoroFree = 1568fe6060f1SDimitry Andric S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_free, {FramePtr}); 15690b57cec5SDimitry Andric 15700b57cec5SDimitry Andric SmallVector<Expr *, 2> DeleteArgs{CoroFree}; 15710b57cec5SDimitry Andric 15720eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p12 15730eae32dcSDimitry Andric // The selected deallocation function shall be called with the address of 15740eae32dcSDimitry Andric // the block of storage to be reclaimed as its first argument. If a 15750eae32dcSDimitry Andric // deallocation function with a parameter of type std::size_t is 15760eae32dcSDimitry Andric // used, the size of the block is passed as the corresponding argument. 15770b57cec5SDimitry Andric const auto *OpDeleteType = 1578480093f4SDimitry Andric OpDeleteQualType.getTypePtr()->castAs<FunctionProtoType>(); 1579bdd1243dSDimitry Andric if (OpDeleteType->getNumParams() > DeleteArgs.size() && 15801ac55f4cSDimitry Andric S.getASTContext().hasSameUnqualifiedType( 1581bdd1243dSDimitry Andric OpDeleteType->getParamType(DeleteArgs.size()), FrameSize->getType())) 15820b57cec5SDimitry Andric DeleteArgs.push_back(FrameSize); 15830b57cec5SDimitry Andric 1584bdd1243dSDimitry Andric // Proposed Change of [dcl.fct.def.coroutine]p12 in P2014R0: 1585bdd1243dSDimitry Andric // If deallocation function lookup finds a usual deallocation function with 1586bdd1243dSDimitry Andric // a pointer parameter, size parameter and alignment parameter then this 1587bdd1243dSDimitry Andric // will be the selected deallocation function, otherwise if lookup finds a 1588bdd1243dSDimitry Andric // usual deallocation function with both a pointer parameter and a size 1589bdd1243dSDimitry Andric // parameter, then this will be the selected deallocation function. 1590bdd1243dSDimitry Andric // Otherwise, if lookup finds a usual deallocation function with only a 1591bdd1243dSDimitry Andric // pointer parameter, then this will be the selected deallocation 1592bdd1243dSDimitry Andric // function. 1593bdd1243dSDimitry Andric // 1594bdd1243dSDimitry Andric // So we are not forced to pass alignment to the deallocation function. 1595bdd1243dSDimitry Andric if (S.getLangOpts().CoroAlignedAllocation && 1596bdd1243dSDimitry Andric OpDeleteType->getNumParams() > DeleteArgs.size() && 15971ac55f4cSDimitry Andric S.getASTContext().hasSameUnqualifiedType( 1598bdd1243dSDimitry Andric OpDeleteType->getParamType(DeleteArgs.size()), 1599bdd1243dSDimitry Andric FrameAlignment->getType())) 1600bdd1243dSDimitry Andric DeleteArgs.push_back(FrameAlignment); 1601bdd1243dSDimitry Andric 16020b57cec5SDimitry Andric ExprResult DeleteExpr = 16030b57cec5SDimitry Andric S.BuildCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc); 16040b57cec5SDimitry Andric DeleteExpr = 16050b57cec5SDimitry Andric S.ActOnFinishFullExpr(DeleteExpr.get(), /*DiscardedValue*/ false); 16060b57cec5SDimitry Andric if (DeleteExpr.isInvalid()) 16070b57cec5SDimitry Andric return false; 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric this->Allocate = NewExpr.get(); 16100b57cec5SDimitry Andric this->Deallocate = DeleteExpr.get(); 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric return true; 16130b57cec5SDimitry Andric } 16140b57cec5SDimitry Andric 16150b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeOnFallthrough() { 16160b57cec5SDimitry Andric assert(!IsPromiseDependentType && 16170b57cec5SDimitry Andric "cannot make statement while the promise type is dependent"); 16180b57cec5SDimitry Andric 16190eae32dcSDimitry Andric // [dcl.fct.def.coroutine]/p6 162004eeddc0SDimitry Andric // If searches for the names return_void and return_value in the scope of 16210eae32dcSDimitry Andric // the promise type each find any declarations, the program is ill-formed. 162204eeddc0SDimitry Andric // [Note 1: If return_void is found, flowing off the end of a coroutine is 162304eeddc0SDimitry Andric // equivalent to a co_return with no operand. Otherwise, flowing off the end 16240eae32dcSDimitry Andric // of a coroutine results in undefined behavior ([stmt.return.coroutine]). — 16250eae32dcSDimitry Andric // end note] 16260b57cec5SDimitry Andric bool HasRVoid, HasRValue; 16270b57cec5SDimitry Andric LookupResult LRVoid = 16280b57cec5SDimitry Andric lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid); 16290b57cec5SDimitry Andric LookupResult LRValue = 16300b57cec5SDimitry Andric lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue); 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andric StmtResult Fallthrough; 16330b57cec5SDimitry Andric if (HasRVoid && HasRValue) { 16340b57cec5SDimitry Andric // FIXME Improve this diagnostic 16350b57cec5SDimitry Andric S.Diag(FD.getLocation(), 16360b57cec5SDimitry Andric diag::err_coroutine_promise_incompatible_return_functions) 16370b57cec5SDimitry Andric << PromiseRecordDecl; 16380b57cec5SDimitry Andric S.Diag(LRVoid.getRepresentativeDecl()->getLocation(), 16390b57cec5SDimitry Andric diag::note_member_first_declared_here) 16400b57cec5SDimitry Andric << LRVoid.getLookupName(); 16410b57cec5SDimitry Andric S.Diag(LRValue.getRepresentativeDecl()->getLocation(), 16420b57cec5SDimitry Andric diag::note_member_first_declared_here) 16430b57cec5SDimitry Andric << LRValue.getLookupName(); 16440b57cec5SDimitry Andric return false; 16450b57cec5SDimitry Andric } else if (!HasRVoid && !HasRValue) { 16460eae32dcSDimitry Andric // We need to set 'Fallthrough'. Otherwise the other analysis part might 16470eae32dcSDimitry Andric // think the coroutine has defined a return_value method. So it might emit 16480eae32dcSDimitry Andric // **false** positive warning. e.g., 16490eae32dcSDimitry Andric // 16500eae32dcSDimitry Andric // promise_without_return_func foo() { 16510eae32dcSDimitry Andric // co_await something(); 16520eae32dcSDimitry Andric // } 16530eae32dcSDimitry Andric // 16540eae32dcSDimitry Andric // Then AnalysisBasedWarning would emit a warning about `foo()` lacking a 16550eae32dcSDimitry Andric // co_return statements, which isn't correct. 16560eae32dcSDimitry Andric Fallthrough = S.ActOnNullStmt(PromiseRecordDecl->getLocation()); 16570eae32dcSDimitry Andric if (Fallthrough.isInvalid()) 16580b57cec5SDimitry Andric return false; 16590b57cec5SDimitry Andric } else if (HasRVoid) { 16600b57cec5SDimitry Andric Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr, 16610fca6ea1SDimitry Andric /*IsImplicit=*/true); 16620b57cec5SDimitry Andric Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get()); 16630b57cec5SDimitry Andric if (Fallthrough.isInvalid()) 16640b57cec5SDimitry Andric return false; 16650b57cec5SDimitry Andric } 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric this->OnFallthrough = Fallthrough.get(); 16680b57cec5SDimitry Andric return true; 16690b57cec5SDimitry Andric } 16700b57cec5SDimitry Andric 16710b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeOnException() { 16720b57cec5SDimitry Andric // Try to form 'p.unhandled_exception();' 16730b57cec5SDimitry Andric assert(!IsPromiseDependentType && 16740b57cec5SDimitry Andric "cannot make statement while the promise type is dependent"); 16750b57cec5SDimitry Andric 16760b57cec5SDimitry Andric const bool RequireUnhandledException = S.getLangOpts().CXXExceptions; 16770b57cec5SDimitry Andric 16780b57cec5SDimitry Andric if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) { 16790b57cec5SDimitry Andric auto DiagID = 16800b57cec5SDimitry Andric RequireUnhandledException 16810b57cec5SDimitry Andric ? diag::err_coroutine_promise_unhandled_exception_required 16820b57cec5SDimitry Andric : diag:: 16830b57cec5SDimitry Andric warn_coroutine_promise_unhandled_exception_required_with_exceptions; 16840b57cec5SDimitry Andric S.Diag(Loc, DiagID) << PromiseRecordDecl; 16850b57cec5SDimitry Andric S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here) 16860b57cec5SDimitry Andric << PromiseRecordDecl; 16870b57cec5SDimitry Andric return !RequireUnhandledException; 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric // If exceptions are disabled, don't try to build OnException. 16910b57cec5SDimitry Andric if (!S.getLangOpts().CXXExceptions) 16920b57cec5SDimitry Andric return true; 16930b57cec5SDimitry Andric 1694bdd1243dSDimitry Andric ExprResult UnhandledException = buildPromiseCall( 1695bdd1243dSDimitry Andric S, Fn.CoroutinePromise, Loc, "unhandled_exception", std::nullopt); 16960b57cec5SDimitry Andric UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc, 16970b57cec5SDimitry Andric /*DiscardedValue*/ false); 16980b57cec5SDimitry Andric if (UnhandledException.isInvalid()) 16990b57cec5SDimitry Andric return false; 17000b57cec5SDimitry Andric 17010b57cec5SDimitry Andric // Since the body of the coroutine will be wrapped in try-catch, it will 17020b57cec5SDimitry Andric // be incompatible with SEH __try if present in a function. 17030b57cec5SDimitry Andric if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) { 17040b57cec5SDimitry Andric S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions); 17050b57cec5SDimitry Andric S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) 17060b57cec5SDimitry Andric << Fn.getFirstCoroutineStmtKeyword(); 17070b57cec5SDimitry Andric return false; 17080b57cec5SDimitry Andric } 17090b57cec5SDimitry Andric 17100b57cec5SDimitry Andric this->OnException = UnhandledException.get(); 17110b57cec5SDimitry Andric return true; 17120b57cec5SDimitry Andric } 17130b57cec5SDimitry Andric 17140b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeReturnObject() { 17150eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p7 171604eeddc0SDimitry Andric // The expression promise.get_return_object() is used to initialize the 17170eae32dcSDimitry Andric // returned reference or prvalue result object of a call to a coroutine. 1718bdd1243dSDimitry Andric ExprResult ReturnObject = buildPromiseCall(S, Fn.CoroutinePromise, Loc, 1719bdd1243dSDimitry Andric "get_return_object", std::nullopt); 17200b57cec5SDimitry Andric if (ReturnObject.isInvalid()) 17210b57cec5SDimitry Andric return false; 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric this->ReturnValue = ReturnObject.get(); 17240b57cec5SDimitry Andric return true; 17250b57cec5SDimitry Andric } 17260b57cec5SDimitry Andric 17270b57cec5SDimitry Andric static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) { 17280b57cec5SDimitry Andric if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) { 17290b57cec5SDimitry Andric auto *MethodDecl = MbrRef->getMethodDecl(); 17300b57cec5SDimitry Andric S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here) 17310b57cec5SDimitry Andric << MethodDecl; 17320b57cec5SDimitry Andric } 17330b57cec5SDimitry Andric S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) 17340b57cec5SDimitry Andric << Fn.getFirstCoroutineStmtKeyword(); 17350b57cec5SDimitry Andric } 17360b57cec5SDimitry Andric 17370b57cec5SDimitry Andric bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() { 17380b57cec5SDimitry Andric assert(!IsPromiseDependentType && 17390b57cec5SDimitry Andric "cannot make statement while the promise type is dependent"); 17400b57cec5SDimitry Andric assert(this->ReturnValue && "ReturnValue must be already formed"); 17410b57cec5SDimitry Andric 17420b57cec5SDimitry Andric QualType const GroType = this->ReturnValue->getType(); 17430b57cec5SDimitry Andric assert(!GroType->isDependentType() && 17440b57cec5SDimitry Andric "get_return_object type must no longer be dependent"); 17450b57cec5SDimitry Andric 17460b57cec5SDimitry Andric QualType const FnRetType = FD.getReturnType(); 17470b57cec5SDimitry Andric assert(!FnRetType->isDependentType() && 17480b57cec5SDimitry Andric "get_return_object type must no longer be dependent"); 17490b57cec5SDimitry Andric 175006c3fb27SDimitry Andric // The call to get_return_object is sequenced before the call to 175106c3fb27SDimitry Andric // initial_suspend and is invoked at most once, but there are caveats 175206c3fb27SDimitry Andric // regarding on whether the prvalue result object may be initialized 175306c3fb27SDimitry Andric // directly/eager or delayed, depending on the types involved. 175406c3fb27SDimitry Andric // 175506c3fb27SDimitry Andric // More info at https://github.com/cplusplus/papers/issues/1414 175606c3fb27SDimitry Andric bool GroMatchesRetType = S.getASTContext().hasSameType(GroType, FnRetType); 175706c3fb27SDimitry Andric 17580b57cec5SDimitry Andric if (FnRetType->isVoidType()) { 17590b57cec5SDimitry Andric ExprResult Res = 17600b57cec5SDimitry Andric S.ActOnFinishFullExpr(this->ReturnValue, Loc, /*DiscardedValue*/ false); 17610b57cec5SDimitry Andric if (Res.isInvalid()) 17620b57cec5SDimitry Andric return false; 17630b57cec5SDimitry Andric 176406c3fb27SDimitry Andric if (!GroMatchesRetType) 176506c3fb27SDimitry Andric this->ResultDecl = Res.get(); 17660b57cec5SDimitry Andric return true; 17670b57cec5SDimitry Andric } 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric if (GroType->isVoidType()) { 17700b57cec5SDimitry Andric // Trigger a nice error message. 17710b57cec5SDimitry Andric InitializedEntity Entity = 177228a41182SDimitry Andric InitializedEntity::InitializeResult(Loc, FnRetType); 1773fe6060f1SDimitry Andric S.PerformCopyInitialization(Entity, SourceLocation(), ReturnValue); 17740b57cec5SDimitry Andric noteMemberDeclaredHere(S, ReturnValue, Fn); 17750b57cec5SDimitry Andric return false; 17760b57cec5SDimitry Andric } 17770b57cec5SDimitry Andric 177806c3fb27SDimitry Andric StmtResult ReturnStmt; 177906c3fb27SDimitry Andric clang::VarDecl *GroDecl = nullptr; 178006c3fb27SDimitry Andric if (GroMatchesRetType) { 178106c3fb27SDimitry Andric ReturnStmt = S.BuildReturnStmt(Loc, ReturnValue); 178206c3fb27SDimitry Andric } else { 178306c3fb27SDimitry Andric GroDecl = VarDecl::Create( 178406c3fb27SDimitry Andric S.Context, &FD, FD.getLocation(), FD.getLocation(), 178506c3fb27SDimitry Andric &S.PP.getIdentifierTable().get("__coro_gro"), GroType, 178606c3fb27SDimitry Andric S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None); 178706c3fb27SDimitry Andric GroDecl->setImplicit(); 178806c3fb27SDimitry Andric 178906c3fb27SDimitry Andric S.CheckVariableDeclarationType(GroDecl); 179006c3fb27SDimitry Andric if (GroDecl->isInvalidDecl()) 179106c3fb27SDimitry Andric return false; 179206c3fb27SDimitry Andric 179306c3fb27SDimitry Andric InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl); 179406c3fb27SDimitry Andric ExprResult Res = 179506c3fb27SDimitry Andric S.PerformCopyInitialization(Entity, SourceLocation(), ReturnValue); 179606c3fb27SDimitry Andric if (Res.isInvalid()) 179706c3fb27SDimitry Andric return false; 179806c3fb27SDimitry Andric 179906c3fb27SDimitry Andric Res = S.ActOnFinishFullExpr(Res.get(), /*DiscardedValue*/ false); 180006c3fb27SDimitry Andric if (Res.isInvalid()) 180106c3fb27SDimitry Andric return false; 180206c3fb27SDimitry Andric 180306c3fb27SDimitry Andric S.AddInitializerToDecl(GroDecl, Res.get(), 180406c3fb27SDimitry Andric /*DirectInit=*/false); 180506c3fb27SDimitry Andric 180606c3fb27SDimitry Andric S.FinalizeDeclaration(GroDecl); 180706c3fb27SDimitry Andric 180806c3fb27SDimitry Andric // Form a declaration statement for the return declaration, so that AST 180906c3fb27SDimitry Andric // visitors can more easily find it. 181006c3fb27SDimitry Andric StmtResult GroDeclStmt = 181106c3fb27SDimitry Andric S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(GroDecl), Loc, Loc); 181206c3fb27SDimitry Andric if (GroDeclStmt.isInvalid()) 181306c3fb27SDimitry Andric return false; 181406c3fb27SDimitry Andric 181506c3fb27SDimitry Andric this->ResultDecl = GroDeclStmt.get(); 181606c3fb27SDimitry Andric 181706c3fb27SDimitry Andric ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc); 181806c3fb27SDimitry Andric if (declRef.isInvalid()) 181906c3fb27SDimitry Andric return false; 182006c3fb27SDimitry Andric 182106c3fb27SDimitry Andric ReturnStmt = S.BuildReturnStmt(Loc, declRef.get()); 182206c3fb27SDimitry Andric } 182306c3fb27SDimitry Andric 18240b57cec5SDimitry Andric if (ReturnStmt.isInvalid()) { 18250b57cec5SDimitry Andric noteMemberDeclaredHere(S, ReturnValue, Fn); 18260b57cec5SDimitry Andric return false; 18270b57cec5SDimitry Andric } 18280b57cec5SDimitry Andric 182906c3fb27SDimitry Andric if (!GroMatchesRetType && 183006c3fb27SDimitry Andric cast<clang::ReturnStmt>(ReturnStmt.get())->getNRVOCandidate() == GroDecl) 183106c3fb27SDimitry Andric GroDecl->setNRVOVariable(true); 183206c3fb27SDimitry Andric 18330b57cec5SDimitry Andric this->ReturnStmt = ReturnStmt.get(); 18340b57cec5SDimitry Andric return true; 18350b57cec5SDimitry Andric } 18360b57cec5SDimitry Andric 18370b57cec5SDimitry Andric // Create a static_cast\<T&&>(expr). 18380b57cec5SDimitry Andric static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) { 18390b57cec5SDimitry Andric if (T.isNull()) 18400b57cec5SDimitry Andric T = E->getType(); 18410b57cec5SDimitry Andric QualType TargetType = S.BuildReferenceType( 18420b57cec5SDimitry Andric T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName()); 18430b57cec5SDimitry Andric SourceLocation ExprLoc = E->getBeginLoc(); 18440b57cec5SDimitry Andric TypeSourceInfo *TargetLoc = 18450b57cec5SDimitry Andric S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc); 18460b57cec5SDimitry Andric 18470b57cec5SDimitry Andric return S 18480b57cec5SDimitry Andric .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, 18490b57cec5SDimitry Andric SourceRange(ExprLoc, ExprLoc), E->getSourceRange()) 18500b57cec5SDimitry Andric .get(); 18510b57cec5SDimitry Andric } 18520b57cec5SDimitry Andric 18530b57cec5SDimitry Andric /// Build a variable declaration for move parameter. 18540b57cec5SDimitry Andric static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type, 18550b57cec5SDimitry Andric IdentifierInfo *II) { 18560b57cec5SDimitry Andric TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc); 18570b57cec5SDimitry Andric VarDecl *Decl = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type, 18580b57cec5SDimitry Andric TInfo, SC_None); 18590b57cec5SDimitry Andric Decl->setImplicit(); 18600b57cec5SDimitry Andric return Decl; 18610b57cec5SDimitry Andric } 18620b57cec5SDimitry Andric 18630b57cec5SDimitry Andric // Build statements that move coroutine function parameters to the coroutine 18640b57cec5SDimitry Andric // frame, and store them on the function scope info. 18650b57cec5SDimitry Andric bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) { 18660b57cec5SDimitry Andric assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); 18670b57cec5SDimitry Andric auto *FD = cast<FunctionDecl>(CurContext); 18680b57cec5SDimitry Andric 18690b57cec5SDimitry Andric auto *ScopeInfo = getCurFunction(); 1870480093f4SDimitry Andric if (!ScopeInfo->CoroutineParameterMoves.empty()) 1871480093f4SDimitry Andric return false; 18720b57cec5SDimitry Andric 18730eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p13 18740eae32dcSDimitry Andric // When a coroutine is invoked, after initializing its parameters 18750eae32dcSDimitry Andric // ([expr.call]), a copy is created for each coroutine parameter. For a 18760eae32dcSDimitry Andric // parameter of type cv T, the copy is a variable of type cv T with 18770eae32dcSDimitry Andric // automatic storage duration that is direct-initialized from an xvalue of 18780eae32dcSDimitry Andric // type T referring to the parameter. 18790b57cec5SDimitry Andric for (auto *PD : FD->parameters()) { 18800b57cec5SDimitry Andric if (PD->getType()->isDependentType()) 18810b57cec5SDimitry Andric continue; 18820b57cec5SDimitry Andric 18835f757f3fSDimitry Andric // Preserve the referenced state for unused parameter diagnostics. 18845f757f3fSDimitry Andric bool DeclReferenced = PD->isReferenced(); 18855f757f3fSDimitry Andric 18860b57cec5SDimitry Andric ExprResult PDRefExpr = 18870b57cec5SDimitry Andric BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), 18880b57cec5SDimitry Andric ExprValueKind::VK_LValue, Loc); // FIXME: scope? 18895f757f3fSDimitry Andric 18905f757f3fSDimitry Andric PD->setReferenced(DeclReferenced); 18915f757f3fSDimitry Andric 18920b57cec5SDimitry Andric if (PDRefExpr.isInvalid()) 18930b57cec5SDimitry Andric return false; 18940b57cec5SDimitry Andric 18950b57cec5SDimitry Andric Expr *CExpr = nullptr; 18960b57cec5SDimitry Andric if (PD->getType()->getAsCXXRecordDecl() || 18970b57cec5SDimitry Andric PD->getType()->isRValueReferenceType()) 18980b57cec5SDimitry Andric CExpr = castForMoving(*this, PDRefExpr.get()); 18990b57cec5SDimitry Andric else 19000b57cec5SDimitry Andric CExpr = PDRefExpr.get(); 19010eae32dcSDimitry Andric // [dcl.fct.def.coroutine]p13 19020eae32dcSDimitry Andric // The initialization and destruction of each parameter copy occurs in the 19030eae32dcSDimitry Andric // context of the called coroutine. 1904bdd1243dSDimitry Andric auto *D = buildVarDecl(*this, Loc, PD->getType(), PD->getIdentifier()); 19050b57cec5SDimitry Andric AddInitializerToDecl(D, CExpr, /*DirectInit=*/true); 19060b57cec5SDimitry Andric 19070b57cec5SDimitry Andric // Convert decl to a statement. 19080b57cec5SDimitry Andric StmtResult Stmt = ActOnDeclStmt(ConvertDeclToDeclGroup(D), Loc, Loc); 19090b57cec5SDimitry Andric if (Stmt.isInvalid()) 19100b57cec5SDimitry Andric return false; 19110b57cec5SDimitry Andric 19120b57cec5SDimitry Andric ScopeInfo->CoroutineParameterMoves.insert(std::make_pair(PD, Stmt.get())); 19130b57cec5SDimitry Andric } 19140b57cec5SDimitry Andric return true; 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) { 19180b57cec5SDimitry Andric CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args); 19190b57cec5SDimitry Andric if (!Res) 19200b57cec5SDimitry Andric return StmtError(); 19210b57cec5SDimitry Andric return Res; 19220b57cec5SDimitry Andric } 19230b57cec5SDimitry Andric 19240b57cec5SDimitry Andric ClassTemplateDecl *Sema::lookupCoroutineTraits(SourceLocation KwLoc, 192506c3fb27SDimitry Andric SourceLocation FuncLoc) { 192606c3fb27SDimitry Andric if (StdCoroutineTraitsCache) 192706c3fb27SDimitry Andric return StdCoroutineTraitsCache; 1928349cc55cSDimitry Andric 1929bdd1243dSDimitry Andric IdentifierInfo const &TraitIdent = 1930bdd1243dSDimitry Andric PP.getIdentifierTable().get("coroutine_traits"); 19310eae32dcSDimitry Andric 19320eae32dcSDimitry Andric NamespaceDecl *StdSpace = getStdNamespace(); 193306c3fb27SDimitry Andric LookupResult Result(*this, &TraitIdent, FuncLoc, LookupOrdinaryName); 193406c3fb27SDimitry Andric bool Found = StdSpace && LookupQualifiedName(Result, StdSpace); 19350eae32dcSDimitry Andric 193606c3fb27SDimitry Andric if (!Found) { 193706c3fb27SDimitry Andric // The goggles, we found nothing! 19380b57cec5SDimitry Andric Diag(KwLoc, diag::err_implied_coroutine_type_not_found) 1939349cc55cSDimitry Andric << "std::coroutine_traits"; 19400b57cec5SDimitry Andric return nullptr; 19410b57cec5SDimitry Andric } 19420eae32dcSDimitry Andric 19430eae32dcSDimitry Andric // coroutine_traits is required to be a class template. 194404eeddc0SDimitry Andric StdCoroutineTraitsCache = Result.getAsSingle<ClassTemplateDecl>(); 194504eeddc0SDimitry Andric if (!StdCoroutineTraitsCache) { 19460b57cec5SDimitry Andric Result.suppressDiagnostics(); 19470b57cec5SDimitry Andric NamedDecl *Found = *Result.begin(); 19480b57cec5SDimitry Andric Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits); 19490b57cec5SDimitry Andric return nullptr; 19500b57cec5SDimitry Andric } 195104eeddc0SDimitry Andric 19520b57cec5SDimitry Andric return StdCoroutineTraitsCache; 19530b57cec5SDimitry Andric } 1954