xref: /openbsd-src/gnu/llvm/clang/lib/Sema/SemaCoroutine.cpp (revision 12c855180aad702bbcca06e0398d774beeafb155)
1e5dd7070Spatrick //===-- SemaCoroutine.cpp - Semantic Analysis for Coroutines --------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick //  This file implements semantic analysis for C++ Coroutines.
10e5dd7070Spatrick //
11e5dd7070Spatrick //  This file contains references to sections of the Coroutines TS, which
12e5dd7070Spatrick //  can be found at http://wg21.link/coroutines.
13e5dd7070Spatrick //
14e5dd7070Spatrick //===----------------------------------------------------------------------===//
15e5dd7070Spatrick 
16e5dd7070Spatrick #include "CoroutineStmtBuilder.h"
17e5dd7070Spatrick #include "clang/AST/ASTLambda.h"
18e5dd7070Spatrick #include "clang/AST/Decl.h"
19e5dd7070Spatrick #include "clang/AST/ExprCXX.h"
20e5dd7070Spatrick #include "clang/AST/StmtCXX.h"
21e5dd7070Spatrick #include "clang/Basic/Builtins.h"
22e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
23e5dd7070Spatrick #include "clang/Sema/Initialization.h"
24e5dd7070Spatrick #include "clang/Sema/Overload.h"
25e5dd7070Spatrick #include "clang/Sema/ScopeInfo.h"
26e5dd7070Spatrick #include "clang/Sema/SemaInternal.h"
27ec727ea7Spatrick #include "llvm/ADT/SmallSet.h"
28e5dd7070Spatrick 
29e5dd7070Spatrick using namespace clang;
30e5dd7070Spatrick using namespace sema;
31e5dd7070Spatrick 
lookupMember(Sema & S,const char * Name,CXXRecordDecl * RD,SourceLocation Loc,bool & Res)32e5dd7070Spatrick static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
33e5dd7070Spatrick                                  SourceLocation Loc, bool &Res) {
34e5dd7070Spatrick   DeclarationName DN = S.PP.getIdentifierInfo(Name);
35e5dd7070Spatrick   LookupResult LR(S, DN, Loc, Sema::LookupMemberName);
36e5dd7070Spatrick   // Suppress diagnostics when a private member is selected. The same warnings
37e5dd7070Spatrick   // will be produced again when building the call.
38e5dd7070Spatrick   LR.suppressDiagnostics();
39e5dd7070Spatrick   Res = S.LookupQualifiedName(LR, RD);
40e5dd7070Spatrick   return LR;
41e5dd7070Spatrick }
42e5dd7070Spatrick 
lookupMember(Sema & S,const char * Name,CXXRecordDecl * RD,SourceLocation Loc)43e5dd7070Spatrick static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
44e5dd7070Spatrick                          SourceLocation Loc) {
45e5dd7070Spatrick   bool Res;
46e5dd7070Spatrick   lookupMember(S, Name, RD, Loc, Res);
47e5dd7070Spatrick   return Res;
48e5dd7070Spatrick }
49e5dd7070Spatrick 
50e5dd7070Spatrick /// Look up the std::coroutine_traits<...>::promise_type for the given
51e5dd7070Spatrick /// function type.
lookupPromiseType(Sema & S,const FunctionDecl * FD,SourceLocation KwLoc)52e5dd7070Spatrick static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD,
53e5dd7070Spatrick                                   SourceLocation KwLoc) {
54e5dd7070Spatrick   const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>();
55e5dd7070Spatrick   const SourceLocation FuncLoc = FD->getLocation();
56e5dd7070Spatrick 
57*12c85518Srobert   NamespaceDecl *CoroNamespace = nullptr;
58*12c85518Srobert   ClassTemplateDecl *CoroTraits =
59*12c85518Srobert       S.lookupCoroutineTraits(KwLoc, FuncLoc, CoroNamespace);
60e5dd7070Spatrick   if (!CoroTraits) {
61e5dd7070Spatrick     return QualType();
62e5dd7070Spatrick   }
63e5dd7070Spatrick 
64e5dd7070Spatrick   // Form template argument list for coroutine_traits<R, P1, P2, ...> according
65e5dd7070Spatrick   // to [dcl.fct.def.coroutine]3
66e5dd7070Spatrick   TemplateArgumentListInfo Args(KwLoc, KwLoc);
67e5dd7070Spatrick   auto AddArg = [&](QualType T) {
68e5dd7070Spatrick     Args.addArgument(TemplateArgumentLoc(
69e5dd7070Spatrick         TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc)));
70e5dd7070Spatrick   };
71e5dd7070Spatrick   AddArg(FnType->getReturnType());
72e5dd7070Spatrick   // If the function is a non-static member function, add the type
73e5dd7070Spatrick   // of the implicit object parameter before the formal parameters.
74e5dd7070Spatrick   if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
75e5dd7070Spatrick     if (MD->isInstance()) {
76e5dd7070Spatrick       // [over.match.funcs]4
77e5dd7070Spatrick       // For non-static member functions, the type of the implicit object
78e5dd7070Spatrick       // parameter is
79e5dd7070Spatrick       //  -- "lvalue reference to cv X" for functions declared without a
80e5dd7070Spatrick       //      ref-qualifier or with the & ref-qualifier
81e5dd7070Spatrick       //  -- "rvalue reference to cv X" for functions declared with the &&
82e5dd7070Spatrick       //      ref-qualifier
83e5dd7070Spatrick       QualType T = MD->getThisType()->castAs<PointerType>()->getPointeeType();
84e5dd7070Spatrick       T = FnType->getRefQualifier() == RQ_RValue
85e5dd7070Spatrick               ? S.Context.getRValueReferenceType(T)
86e5dd7070Spatrick               : S.Context.getLValueReferenceType(T, /*SpelledAsLValue*/ true);
87e5dd7070Spatrick       AddArg(T);
88e5dd7070Spatrick     }
89e5dd7070Spatrick   }
90e5dd7070Spatrick   for (QualType T : FnType->getParamTypes())
91e5dd7070Spatrick     AddArg(T);
92e5dd7070Spatrick 
93e5dd7070Spatrick   // Build the template-id.
94e5dd7070Spatrick   QualType CoroTrait =
95e5dd7070Spatrick       S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args);
96e5dd7070Spatrick   if (CoroTrait.isNull())
97e5dd7070Spatrick     return QualType();
98e5dd7070Spatrick   if (S.RequireCompleteType(KwLoc, CoroTrait,
99e5dd7070Spatrick                             diag::err_coroutine_type_missing_specialization))
100e5dd7070Spatrick     return QualType();
101e5dd7070Spatrick 
102e5dd7070Spatrick   auto *RD = CoroTrait->getAsCXXRecordDecl();
103e5dd7070Spatrick   assert(RD && "specialization of class template is not a class?");
104e5dd7070Spatrick 
105e5dd7070Spatrick   // Look up the ::promise_type member.
106e5dd7070Spatrick   LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc,
107e5dd7070Spatrick                  Sema::LookupOrdinaryName);
108e5dd7070Spatrick   S.LookupQualifiedName(R, RD);
109e5dd7070Spatrick   auto *Promise = R.getAsSingle<TypeDecl>();
110e5dd7070Spatrick   if (!Promise) {
111e5dd7070Spatrick     S.Diag(FuncLoc,
112e5dd7070Spatrick            diag::err_implied_std_coroutine_traits_promise_type_not_found)
113e5dd7070Spatrick         << RD;
114e5dd7070Spatrick     return QualType();
115e5dd7070Spatrick   }
116e5dd7070Spatrick   // The promise type is required to be a class type.
117e5dd7070Spatrick   QualType PromiseType = S.Context.getTypeDeclType(Promise);
118e5dd7070Spatrick 
119e5dd7070Spatrick   auto buildElaboratedType = [&]() {
120*12c85518Srobert     auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, CoroNamespace);
121e5dd7070Spatrick     NNS = NestedNameSpecifier::Create(S.Context, NNS, false,
122e5dd7070Spatrick                                       CoroTrait.getTypePtr());
123e5dd7070Spatrick     return S.Context.getElaboratedType(ETK_None, NNS, PromiseType);
124e5dd7070Spatrick   };
125e5dd7070Spatrick 
126e5dd7070Spatrick   if (!PromiseType->getAsCXXRecordDecl()) {
127e5dd7070Spatrick     S.Diag(FuncLoc,
128e5dd7070Spatrick            diag::err_implied_std_coroutine_traits_promise_type_not_class)
129e5dd7070Spatrick         << buildElaboratedType();
130e5dd7070Spatrick     return QualType();
131e5dd7070Spatrick   }
132e5dd7070Spatrick   if (S.RequireCompleteType(FuncLoc, buildElaboratedType(),
133e5dd7070Spatrick                             diag::err_coroutine_promise_type_incomplete))
134e5dd7070Spatrick     return QualType();
135e5dd7070Spatrick 
136e5dd7070Spatrick   return PromiseType;
137e5dd7070Spatrick }
138e5dd7070Spatrick 
139*12c85518Srobert /// Look up the std::coroutine_handle<PromiseType>.
lookupCoroutineHandleType(Sema & S,QualType PromiseType,SourceLocation Loc)140e5dd7070Spatrick static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType,
141e5dd7070Spatrick                                           SourceLocation Loc) {
142e5dd7070Spatrick   if (PromiseType.isNull())
143e5dd7070Spatrick     return QualType();
144e5dd7070Spatrick 
145*12c85518Srobert   NamespaceDecl *CoroNamespace = S.getCachedCoroNamespace();
146*12c85518Srobert   assert(CoroNamespace && "Should already be diagnosed");
147e5dd7070Spatrick 
148e5dd7070Spatrick   LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"),
149e5dd7070Spatrick                       Loc, Sema::LookupOrdinaryName);
150*12c85518Srobert   if (!S.LookupQualifiedName(Result, CoroNamespace)) {
151e5dd7070Spatrick     S.Diag(Loc, diag::err_implied_coroutine_type_not_found)
152*12c85518Srobert         << "std::coroutine_handle";
153e5dd7070Spatrick     return QualType();
154e5dd7070Spatrick   }
155e5dd7070Spatrick 
156e5dd7070Spatrick   ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>();
157e5dd7070Spatrick   if (!CoroHandle) {
158e5dd7070Spatrick     Result.suppressDiagnostics();
159e5dd7070Spatrick     // We found something weird. Complain about the first thing we found.
160e5dd7070Spatrick     NamedDecl *Found = *Result.begin();
161e5dd7070Spatrick     S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle);
162e5dd7070Spatrick     return QualType();
163e5dd7070Spatrick   }
164e5dd7070Spatrick 
165e5dd7070Spatrick   // Form template argument list for coroutine_handle<Promise>.
166e5dd7070Spatrick   TemplateArgumentListInfo Args(Loc, Loc);
167e5dd7070Spatrick   Args.addArgument(TemplateArgumentLoc(
168e5dd7070Spatrick       TemplateArgument(PromiseType),
169e5dd7070Spatrick       S.Context.getTrivialTypeSourceInfo(PromiseType, Loc)));
170e5dd7070Spatrick 
171e5dd7070Spatrick   // Build the template-id.
172e5dd7070Spatrick   QualType CoroHandleType =
173e5dd7070Spatrick       S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args);
174e5dd7070Spatrick   if (CoroHandleType.isNull())
175e5dd7070Spatrick     return QualType();
176e5dd7070Spatrick   if (S.RequireCompleteType(Loc, CoroHandleType,
177e5dd7070Spatrick                             diag::err_coroutine_type_missing_specialization))
178e5dd7070Spatrick     return QualType();
179e5dd7070Spatrick 
180e5dd7070Spatrick   return CoroHandleType;
181e5dd7070Spatrick }
182e5dd7070Spatrick 
isValidCoroutineContext(Sema & S,SourceLocation Loc,StringRef Keyword)183e5dd7070Spatrick static bool isValidCoroutineContext(Sema &S, SourceLocation Loc,
184e5dd7070Spatrick                                     StringRef Keyword) {
185e5dd7070Spatrick   // [expr.await]p2 dictates that 'co_await' and 'co_yield' must be used within
186e5dd7070Spatrick   // a function body.
187e5dd7070Spatrick   // FIXME: This also covers [expr.await]p2: "An await-expression shall not
188e5dd7070Spatrick   // appear in a default argument." But the diagnostic QoI here could be
189e5dd7070Spatrick   // improved to inform the user that default arguments specifically are not
190e5dd7070Spatrick   // allowed.
191e5dd7070Spatrick   auto *FD = dyn_cast<FunctionDecl>(S.CurContext);
192e5dd7070Spatrick   if (!FD) {
193e5dd7070Spatrick     S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext)
194e5dd7070Spatrick                     ? diag::err_coroutine_objc_method
195e5dd7070Spatrick                     : diag::err_coroutine_outside_function) << Keyword;
196e5dd7070Spatrick     return false;
197e5dd7070Spatrick   }
198e5dd7070Spatrick 
199e5dd7070Spatrick   // An enumeration for mapping the diagnostic type to the correct diagnostic
200e5dd7070Spatrick   // selection index.
201e5dd7070Spatrick   enum InvalidFuncDiag {
202e5dd7070Spatrick     DiagCtor = 0,
203e5dd7070Spatrick     DiagDtor,
204e5dd7070Spatrick     DiagMain,
205e5dd7070Spatrick     DiagConstexpr,
206e5dd7070Spatrick     DiagAutoRet,
207e5dd7070Spatrick     DiagVarargs,
208e5dd7070Spatrick     DiagConsteval,
209e5dd7070Spatrick   };
210e5dd7070Spatrick   bool Diagnosed = false;
211e5dd7070Spatrick   auto DiagInvalid = [&](InvalidFuncDiag ID) {
212e5dd7070Spatrick     S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword;
213e5dd7070Spatrick     Diagnosed = true;
214e5dd7070Spatrick     return false;
215e5dd7070Spatrick   };
216e5dd7070Spatrick 
217e5dd7070Spatrick   // Diagnose when a constructor, destructor
218e5dd7070Spatrick   // or the function 'main' are declared as a coroutine.
219e5dd7070Spatrick   auto *MD = dyn_cast<CXXMethodDecl>(FD);
220e5dd7070Spatrick   // [class.ctor]p11: "A constructor shall not be a coroutine."
221e5dd7070Spatrick   if (MD && isa<CXXConstructorDecl>(MD))
222e5dd7070Spatrick     return DiagInvalid(DiagCtor);
223e5dd7070Spatrick   // [class.dtor]p17: "A destructor shall not be a coroutine."
224e5dd7070Spatrick   else if (MD && isa<CXXDestructorDecl>(MD))
225e5dd7070Spatrick     return DiagInvalid(DiagDtor);
226e5dd7070Spatrick   // [basic.start.main]p3: "The function main shall not be a coroutine."
227e5dd7070Spatrick   else if (FD->isMain())
228e5dd7070Spatrick     return DiagInvalid(DiagMain);
229e5dd7070Spatrick 
230e5dd7070Spatrick   // Emit a diagnostics for each of the following conditions which is not met.
231e5dd7070Spatrick   // [expr.const]p2: "An expression e is a core constant expression unless the
232e5dd7070Spatrick   // evaluation of e [...] would evaluate one of the following expressions:
233e5dd7070Spatrick   // [...] an await-expression [...] a yield-expression."
234e5dd7070Spatrick   if (FD->isConstexpr())
235e5dd7070Spatrick     DiagInvalid(FD->isConsteval() ? DiagConsteval : DiagConstexpr);
236e5dd7070Spatrick   // [dcl.spec.auto]p15: "A function declared with a return type that uses a
237e5dd7070Spatrick   // placeholder type shall not be a coroutine."
238e5dd7070Spatrick   if (FD->getReturnType()->isUndeducedType())
239e5dd7070Spatrick     DiagInvalid(DiagAutoRet);
240*12c85518Srobert   // [dcl.fct.def.coroutine]p1
241*12c85518Srobert   // The parameter-declaration-clause of the coroutine shall not terminate with
242*12c85518Srobert   // an ellipsis that is not part of a parameter-declaration.
243e5dd7070Spatrick   if (FD->isVariadic())
244e5dd7070Spatrick     DiagInvalid(DiagVarargs);
245e5dd7070Spatrick 
246e5dd7070Spatrick   return !Diagnosed;
247e5dd7070Spatrick }
248e5dd7070Spatrick 
249e5dd7070Spatrick /// Build a call to 'operator co_await' if there is a suitable operator for
250e5dd7070Spatrick /// the given expression.
BuildOperatorCoawaitCall(SourceLocation Loc,Expr * E,UnresolvedLookupExpr * Lookup)251*12c85518Srobert ExprResult Sema::BuildOperatorCoawaitCall(SourceLocation Loc, Expr *E,
252e5dd7070Spatrick                                           UnresolvedLookupExpr *Lookup) {
253e5dd7070Spatrick   UnresolvedSet<16> Functions;
254e5dd7070Spatrick   Functions.append(Lookup->decls_begin(), Lookup->decls_end());
255*12c85518Srobert   return CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E);
256e5dd7070Spatrick }
257e5dd7070Spatrick 
buildOperatorCoawaitCall(Sema & SemaRef,Scope * S,SourceLocation Loc,Expr * E)258e5dd7070Spatrick static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S,
259e5dd7070Spatrick                                            SourceLocation Loc, Expr *E) {
260*12c85518Srobert   ExprResult R = SemaRef.BuildOperatorCoawaitLookupExpr(S, Loc);
261e5dd7070Spatrick   if (R.isInvalid())
262e5dd7070Spatrick     return ExprError();
263*12c85518Srobert   return SemaRef.BuildOperatorCoawaitCall(Loc, E,
264e5dd7070Spatrick                                           cast<UnresolvedLookupExpr>(R.get()));
265e5dd7070Spatrick }
266e5dd7070Spatrick 
buildCoroutineHandle(Sema & S,QualType PromiseType,SourceLocation Loc)267e5dd7070Spatrick static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType,
268e5dd7070Spatrick                                        SourceLocation Loc) {
269e5dd7070Spatrick   QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc);
270e5dd7070Spatrick   if (CoroHandleType.isNull())
271e5dd7070Spatrick     return ExprError();
272e5dd7070Spatrick 
273e5dd7070Spatrick   DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType);
274e5dd7070Spatrick   LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc,
275e5dd7070Spatrick                      Sema::LookupOrdinaryName);
276e5dd7070Spatrick   if (!S.LookupQualifiedName(Found, LookupCtx)) {
277e5dd7070Spatrick     S.Diag(Loc, diag::err_coroutine_handle_missing_member)
278e5dd7070Spatrick         << "from_address";
279e5dd7070Spatrick     return ExprError();
280e5dd7070Spatrick   }
281e5dd7070Spatrick 
282e5dd7070Spatrick   Expr *FramePtr =
283a9ac8606Spatrick       S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_frame, {});
284e5dd7070Spatrick 
285e5dd7070Spatrick   CXXScopeSpec SS;
286e5dd7070Spatrick   ExprResult FromAddr =
287e5dd7070Spatrick       S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
288e5dd7070Spatrick   if (FromAddr.isInvalid())
289e5dd7070Spatrick     return ExprError();
290e5dd7070Spatrick 
291e5dd7070Spatrick   return S.BuildCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc);
292e5dd7070Spatrick }
293e5dd7070Spatrick 
294e5dd7070Spatrick struct ReadySuspendResumeResult {
295e5dd7070Spatrick   enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume };
296e5dd7070Spatrick   Expr *Results[3];
297e5dd7070Spatrick   OpaqueValueExpr *OpaqueValue;
298e5dd7070Spatrick   bool IsInvalid;
299e5dd7070Spatrick };
300e5dd7070Spatrick 
buildMemberCall(Sema & S,Expr * Base,SourceLocation Loc,StringRef Name,MultiExprArg Args)301e5dd7070Spatrick static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc,
302e5dd7070Spatrick                                   StringRef Name, MultiExprArg Args) {
303e5dd7070Spatrick   DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc);
304e5dd7070Spatrick 
305e5dd7070Spatrick   // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&.
306e5dd7070Spatrick   CXXScopeSpec SS;
307e5dd7070Spatrick   ExprResult Result = S.BuildMemberReferenceExpr(
308e5dd7070Spatrick       Base, Base->getType(), Loc, /*IsPtr=*/false, SS,
309e5dd7070Spatrick       SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr,
310e5dd7070Spatrick       /*Scope=*/nullptr);
311e5dd7070Spatrick   if (Result.isInvalid())
312e5dd7070Spatrick     return ExprError();
313e5dd7070Spatrick 
314e5dd7070Spatrick   // We meant exactly what we asked for. No need for typo correction.
315e5dd7070Spatrick   if (auto *TE = dyn_cast<TypoExpr>(Result.get())) {
316e5dd7070Spatrick     S.clearDelayedTypo(TE);
317e5dd7070Spatrick     S.Diag(Loc, diag::err_no_member)
318e5dd7070Spatrick         << NameInfo.getName() << Base->getType()->getAsCXXRecordDecl()
319e5dd7070Spatrick         << Base->getSourceRange();
320e5dd7070Spatrick     return ExprError();
321e5dd7070Spatrick   }
322e5dd7070Spatrick 
323e5dd7070Spatrick   return S.BuildCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr);
324e5dd7070Spatrick }
325e5dd7070Spatrick 
326e5dd7070Spatrick // See if return type is coroutine-handle and if so, invoke builtin coro-resume
327e5dd7070Spatrick // on its address. This is to enable experimental support for coroutine-handle
328e5dd7070Spatrick // returning await_suspend that results in a guaranteed tail call to the target
329e5dd7070Spatrick // coroutine.
maybeTailCall(Sema & S,QualType RetType,Expr * E,SourceLocation Loc)330e5dd7070Spatrick static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E,
331e5dd7070Spatrick                            SourceLocation Loc) {
332e5dd7070Spatrick   if (RetType->isReferenceType())
333e5dd7070Spatrick     return nullptr;
334e5dd7070Spatrick   Type const *T = RetType.getTypePtr();
335e5dd7070Spatrick   if (!T->isClassType() && !T->isStructureType())
336e5dd7070Spatrick     return nullptr;
337e5dd7070Spatrick 
338e5dd7070Spatrick   // FIXME: Add convertability check to coroutine_handle<>. Possibly via
339e5dd7070Spatrick   // EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment
340e5dd7070Spatrick   // a private function in SemaExprCXX.cpp
341e5dd7070Spatrick 
342*12c85518Srobert   ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", std::nullopt);
343e5dd7070Spatrick   if (AddressExpr.isInvalid())
344e5dd7070Spatrick     return nullptr;
345e5dd7070Spatrick 
346e5dd7070Spatrick   Expr *JustAddress = AddressExpr.get();
347ec727ea7Spatrick 
348ec727ea7Spatrick   // Check that the type of AddressExpr is void*
349ec727ea7Spatrick   if (!JustAddress->getType().getTypePtr()->isVoidPointerType())
350ec727ea7Spatrick     S.Diag(cast<CallExpr>(JustAddress)->getCalleeDecl()->getLocation(),
351ec727ea7Spatrick            diag::warn_coroutine_handle_address_invalid_return_type)
352ec727ea7Spatrick         << JustAddress->getType();
353ec727ea7Spatrick 
354a9ac8606Spatrick   // Clean up temporary objects so that they don't live across suspension points
355a9ac8606Spatrick   // unnecessarily. We choose to clean up before the call to
356a9ac8606Spatrick   // __builtin_coro_resume so that the cleanup code are not inserted in-between
357a9ac8606Spatrick   // the resume call and return instruction, which would interfere with the
358a9ac8606Spatrick   // musttail call contract.
359a9ac8606Spatrick   JustAddress = S.MaybeCreateExprWithCleanups(JustAddress);
360a9ac8606Spatrick   return S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_resume,
361e5dd7070Spatrick                                 JustAddress);
362e5dd7070Spatrick }
363e5dd7070Spatrick 
364e5dd7070Spatrick /// Build calls to await_ready, await_suspend, and await_resume for a co_await
365e5dd7070Spatrick /// expression.
366a9ac8606Spatrick /// The generated AST tries to clean up temporary objects as early as
367a9ac8606Spatrick /// possible so that they don't live across suspension points if possible.
368a9ac8606Spatrick /// Having temporary objects living across suspension points unnecessarily can
369a9ac8606Spatrick /// lead to large frame size, and also lead to memory corruptions if the
370a9ac8606Spatrick /// coroutine frame is destroyed after coming back from suspension. This is done
371a9ac8606Spatrick /// by wrapping both the await_ready call and the await_suspend call with
372a9ac8606Spatrick /// ExprWithCleanups. In the end of this function, we also need to explicitly
373a9ac8606Spatrick /// set cleanup state so that the CoawaitExpr is also wrapped with an
374a9ac8606Spatrick /// ExprWithCleanups to clean up the awaiter associated with the co_await
375a9ac8606Spatrick /// expression.
buildCoawaitCalls(Sema & S,VarDecl * CoroPromise,SourceLocation Loc,Expr * E)376e5dd7070Spatrick static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise,
377e5dd7070Spatrick                                                   SourceLocation Loc, Expr *E) {
378e5dd7070Spatrick   OpaqueValueExpr *Operand = new (S.Context)
379e5dd7070Spatrick       OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E);
380e5dd7070Spatrick 
381a9ac8606Spatrick   // Assume valid until we see otherwise.
382a9ac8606Spatrick   // Further operations are responsible for setting IsInalid to true.
383a9ac8606Spatrick   ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/false};
384e5dd7070Spatrick 
385e5dd7070Spatrick   using ACT = ReadySuspendResumeResult::AwaitCallType;
386a9ac8606Spatrick 
387a9ac8606Spatrick   auto BuildSubExpr = [&](ACT CallType, StringRef Func,
388a9ac8606Spatrick                           MultiExprArg Arg) -> Expr * {
389a9ac8606Spatrick     ExprResult Result = buildMemberCall(S, Operand, Loc, Func, Arg);
390a9ac8606Spatrick     if (Result.isInvalid()) {
391a9ac8606Spatrick       Calls.IsInvalid = true;
392a9ac8606Spatrick       return nullptr;
393a9ac8606Spatrick     }
394a9ac8606Spatrick     Calls.Results[CallType] = Result.get();
395a9ac8606Spatrick     return Result.get();
396a9ac8606Spatrick   };
397a9ac8606Spatrick 
398*12c85518Srobert   CallExpr *AwaitReady = cast_or_null<CallExpr>(
399*12c85518Srobert       BuildSubExpr(ACT::ACT_Ready, "await_ready", std::nullopt));
400a9ac8606Spatrick   if (!AwaitReady)
401a9ac8606Spatrick     return Calls;
402e5dd7070Spatrick   if (!AwaitReady->getType()->isDependentType()) {
403e5dd7070Spatrick     // [expr.await]p3 [...]
404e5dd7070Spatrick     // — await-ready is the expression e.await_ready(), contextually converted
405e5dd7070Spatrick     // to bool.
406e5dd7070Spatrick     ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady);
407e5dd7070Spatrick     if (Conv.isInvalid()) {
408e5dd7070Spatrick       S.Diag(AwaitReady->getDirectCallee()->getBeginLoc(),
409e5dd7070Spatrick              diag::note_await_ready_no_bool_conversion);
410e5dd7070Spatrick       S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
411e5dd7070Spatrick           << AwaitReady->getDirectCallee() << E->getSourceRange();
412e5dd7070Spatrick       Calls.IsInvalid = true;
413a9ac8606Spatrick     } else
414a9ac8606Spatrick       Calls.Results[ACT::ACT_Ready] = S.MaybeCreateExprWithCleanups(Conv.get());
415e5dd7070Spatrick   }
416a9ac8606Spatrick 
417a9ac8606Spatrick   ExprResult CoroHandleRes =
418a9ac8606Spatrick       buildCoroutineHandle(S, CoroPromise->getType(), Loc);
419a9ac8606Spatrick   if (CoroHandleRes.isInvalid()) {
420a9ac8606Spatrick     Calls.IsInvalid = true;
421a9ac8606Spatrick     return Calls;
422e5dd7070Spatrick   }
423a9ac8606Spatrick   Expr *CoroHandle = CoroHandleRes.get();
424a9ac8606Spatrick   CallExpr *AwaitSuspend = cast_or_null<CallExpr>(
425a9ac8606Spatrick       BuildSubExpr(ACT::ACT_Suspend, "await_suspend", CoroHandle));
426a9ac8606Spatrick   if (!AwaitSuspend)
427a9ac8606Spatrick     return Calls;
428e5dd7070Spatrick   if (!AwaitSuspend->getType()->isDependentType()) {
429e5dd7070Spatrick     // [expr.await]p3 [...]
430e5dd7070Spatrick     //   - await-suspend is the expression e.await_suspend(h), which shall be
431a9ac8606Spatrick     //     a prvalue of type void, bool, or std::coroutine_handle<Z> for some
432a9ac8606Spatrick     //     type Z.
433e5dd7070Spatrick     QualType RetType = AwaitSuspend->getCallReturnType(S.Context);
434e5dd7070Spatrick 
435e5dd7070Spatrick     // Experimental support for coroutine_handle returning await_suspend.
436a9ac8606Spatrick     if (Expr *TailCallSuspend =
437a9ac8606Spatrick             maybeTailCall(S, RetType, AwaitSuspend, Loc))
438a9ac8606Spatrick       // Note that we don't wrap the expression with ExprWithCleanups here
439a9ac8606Spatrick       // because that might interfere with tailcall contract (e.g. inserting
440a9ac8606Spatrick       // clean up instructions in-between tailcall and return). Instead
441a9ac8606Spatrick       // ExprWithCleanups is wrapped within maybeTailCall() prior to the resume
442a9ac8606Spatrick       // call.
443e5dd7070Spatrick       Calls.Results[ACT::ACT_Suspend] = TailCallSuspend;
444e5dd7070Spatrick     else {
445e5dd7070Spatrick       // non-class prvalues always have cv-unqualified types
446e5dd7070Spatrick       if (RetType->isReferenceType() ||
447e5dd7070Spatrick           (!RetType->isBooleanType() && !RetType->isVoidType())) {
448e5dd7070Spatrick         S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(),
449e5dd7070Spatrick                diag::err_await_suspend_invalid_return_type)
450e5dd7070Spatrick             << RetType;
451e5dd7070Spatrick         S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
452e5dd7070Spatrick             << AwaitSuspend->getDirectCallee();
453e5dd7070Spatrick         Calls.IsInvalid = true;
454a9ac8606Spatrick       } else
455a9ac8606Spatrick         Calls.Results[ACT::ACT_Suspend] =
456a9ac8606Spatrick             S.MaybeCreateExprWithCleanups(AwaitSuspend);
457e5dd7070Spatrick     }
458e5dd7070Spatrick   }
459a9ac8606Spatrick 
460*12c85518Srobert   BuildSubExpr(ACT::ACT_Resume, "await_resume", std::nullopt);
461a9ac8606Spatrick 
462a9ac8606Spatrick   // Make sure the awaiter object gets a chance to be cleaned up.
463a9ac8606Spatrick   S.Cleanup.setExprNeedsCleanups(true);
464e5dd7070Spatrick 
465e5dd7070Spatrick   return Calls;
466e5dd7070Spatrick }
467e5dd7070Spatrick 
buildPromiseCall(Sema & S,VarDecl * Promise,SourceLocation Loc,StringRef Name,MultiExprArg Args)468e5dd7070Spatrick static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise,
469e5dd7070Spatrick                                    SourceLocation Loc, StringRef Name,
470e5dd7070Spatrick                                    MultiExprArg Args) {
471e5dd7070Spatrick 
472e5dd7070Spatrick   // Form a reference to the promise.
473e5dd7070Spatrick   ExprResult PromiseRef = S.BuildDeclRefExpr(
474e5dd7070Spatrick       Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc);
475e5dd7070Spatrick   if (PromiseRef.isInvalid())
476e5dd7070Spatrick     return ExprError();
477e5dd7070Spatrick 
478e5dd7070Spatrick   return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args);
479e5dd7070Spatrick }
480e5dd7070Spatrick 
buildCoroutinePromise(SourceLocation Loc)481e5dd7070Spatrick VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) {
482e5dd7070Spatrick   assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
483e5dd7070Spatrick   auto *FD = cast<FunctionDecl>(CurContext);
484e5dd7070Spatrick   bool IsThisDependentType = [&] {
485e5dd7070Spatrick     if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(FD))
486e5dd7070Spatrick       return MD->isInstance() && MD->getThisType()->isDependentType();
487e5dd7070Spatrick     else
488e5dd7070Spatrick       return false;
489e5dd7070Spatrick   }();
490e5dd7070Spatrick 
491e5dd7070Spatrick   QualType T = FD->getType()->isDependentType() || IsThisDependentType
492e5dd7070Spatrick                    ? Context.DependentTy
493e5dd7070Spatrick                    : lookupPromiseType(*this, FD, Loc);
494e5dd7070Spatrick   if (T.isNull())
495e5dd7070Spatrick     return nullptr;
496e5dd7070Spatrick 
497e5dd7070Spatrick   auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(),
498e5dd7070Spatrick                              &PP.getIdentifierTable().get("__promise"), T,
499e5dd7070Spatrick                              Context.getTrivialTypeSourceInfo(T, Loc), SC_None);
500a9ac8606Spatrick   VD->setImplicit();
501e5dd7070Spatrick   CheckVariableDeclarationType(VD);
502e5dd7070Spatrick   if (VD->isInvalidDecl())
503e5dd7070Spatrick     return nullptr;
504e5dd7070Spatrick 
505e5dd7070Spatrick   auto *ScopeInfo = getCurFunction();
506ec727ea7Spatrick 
507ec727ea7Spatrick   // Build a list of arguments, based on the coroutine function's arguments,
508ec727ea7Spatrick   // that if present will be passed to the promise type's constructor.
509e5dd7070Spatrick   llvm::SmallVector<Expr *, 4> CtorArgExprs;
510e5dd7070Spatrick 
511e5dd7070Spatrick   // Add implicit object parameter.
512e5dd7070Spatrick   if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
513e5dd7070Spatrick     if (MD->isInstance() && !isLambdaCallOperator(MD)) {
514e5dd7070Spatrick       ExprResult ThisExpr = ActOnCXXThis(Loc);
515e5dd7070Spatrick       if (ThisExpr.isInvalid())
516e5dd7070Spatrick         return nullptr;
517e5dd7070Spatrick       ThisExpr = CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get());
518e5dd7070Spatrick       if (ThisExpr.isInvalid())
519e5dd7070Spatrick         return nullptr;
520e5dd7070Spatrick       CtorArgExprs.push_back(ThisExpr.get());
521e5dd7070Spatrick     }
522e5dd7070Spatrick   }
523e5dd7070Spatrick 
524ec727ea7Spatrick   // Add the coroutine function's parameters.
525e5dd7070Spatrick   auto &Moves = ScopeInfo->CoroutineParameterMoves;
526e5dd7070Spatrick   for (auto *PD : FD->parameters()) {
527e5dd7070Spatrick     if (PD->getType()->isDependentType())
528e5dd7070Spatrick       continue;
529e5dd7070Spatrick 
530e5dd7070Spatrick     auto RefExpr = ExprEmpty();
531e5dd7070Spatrick     auto Move = Moves.find(PD);
532e5dd7070Spatrick     assert(Move != Moves.end() &&
533e5dd7070Spatrick            "Coroutine function parameter not inserted into move map");
534e5dd7070Spatrick     // If a reference to the function parameter exists in the coroutine
535e5dd7070Spatrick     // frame, use that reference.
536e5dd7070Spatrick     auto *MoveDecl =
537e5dd7070Spatrick         cast<VarDecl>(cast<DeclStmt>(Move->second)->getSingleDecl());
538e5dd7070Spatrick     RefExpr =
539e5dd7070Spatrick         BuildDeclRefExpr(MoveDecl, MoveDecl->getType().getNonReferenceType(),
540e5dd7070Spatrick                          ExprValueKind::VK_LValue, FD->getLocation());
541e5dd7070Spatrick     if (RefExpr.isInvalid())
542e5dd7070Spatrick       return nullptr;
543e5dd7070Spatrick     CtorArgExprs.push_back(RefExpr.get());
544e5dd7070Spatrick   }
545e5dd7070Spatrick 
546ec727ea7Spatrick   // If we have a non-zero number of constructor arguments, try to use them.
547ec727ea7Spatrick   // Otherwise, fall back to the promise type's default constructor.
548ec727ea7Spatrick   if (!CtorArgExprs.empty()) {
549e5dd7070Spatrick     // Create an initialization sequence for the promise type using the
550e5dd7070Spatrick     // constructor arguments, wrapped in a parenthesized list expression.
551e5dd7070Spatrick     Expr *PLE = ParenListExpr::Create(Context, FD->getLocation(),
552e5dd7070Spatrick                                       CtorArgExprs, FD->getLocation());
553e5dd7070Spatrick     InitializedEntity Entity = InitializedEntity::InitializeVariable(VD);
554e5dd7070Spatrick     InitializationKind Kind = InitializationKind::CreateForInit(
555e5dd7070Spatrick         VD->getLocation(), /*DirectInit=*/true, PLE);
556e5dd7070Spatrick     InitializationSequence InitSeq(*this, Entity, Kind, CtorArgExprs,
557e5dd7070Spatrick                                    /*TopLevelOfInitList=*/false,
558e5dd7070Spatrick                                    /*TreatUnavailableAsInvalid=*/false);
559e5dd7070Spatrick 
560*12c85518Srobert     // [dcl.fct.def.coroutine]5.7
561*12c85518Srobert     // promise-constructor-arguments is determined as follows: overload
562*12c85518Srobert     // resolution is performed on a promise constructor call created by
563*12c85518Srobert     // assembling an argument list  q_1 ... q_n . If a viable constructor is
564*12c85518Srobert     // found ([over.match.viable]), then promise-constructor-arguments is ( q_1
565*12c85518Srobert     // , ...,  q_n ), otherwise promise-constructor-arguments is empty.
566e5dd7070Spatrick     if (InitSeq) {
567e5dd7070Spatrick       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, CtorArgExprs);
568e5dd7070Spatrick       if (Result.isInvalid()) {
569e5dd7070Spatrick         VD->setInvalidDecl();
570e5dd7070Spatrick       } else if (Result.get()) {
571e5dd7070Spatrick         VD->setInit(MaybeCreateExprWithCleanups(Result.get()));
572e5dd7070Spatrick         VD->setInitStyle(VarDecl::CallInit);
573e5dd7070Spatrick         CheckCompleteVariableDeclaration(VD);
574e5dd7070Spatrick       }
575e5dd7070Spatrick     } else
576e5dd7070Spatrick       ActOnUninitializedDecl(VD);
577ec727ea7Spatrick   } else
578ec727ea7Spatrick     ActOnUninitializedDecl(VD);
579e5dd7070Spatrick 
580e5dd7070Spatrick   FD->addDecl(VD);
581e5dd7070Spatrick   return VD;
582e5dd7070Spatrick }
583e5dd7070Spatrick 
584e5dd7070Spatrick /// Check that this is a context in which a coroutine suspension can appear.
checkCoroutineContext(Sema & S,SourceLocation Loc,StringRef Keyword,bool IsImplicit=false)585e5dd7070Spatrick static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc,
586e5dd7070Spatrick                                                 StringRef Keyword,
587e5dd7070Spatrick                                                 bool IsImplicit = false) {
588e5dd7070Spatrick   if (!isValidCoroutineContext(S, Loc, Keyword))
589e5dd7070Spatrick     return nullptr;
590e5dd7070Spatrick 
591e5dd7070Spatrick   assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope");
592e5dd7070Spatrick 
593e5dd7070Spatrick   auto *ScopeInfo = S.getCurFunction();
594e5dd7070Spatrick   assert(ScopeInfo && "missing function scope for function");
595e5dd7070Spatrick 
596e5dd7070Spatrick   if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit)
597e5dd7070Spatrick     ScopeInfo->setFirstCoroutineStmt(Loc, Keyword);
598e5dd7070Spatrick 
599e5dd7070Spatrick   if (ScopeInfo->CoroutinePromise)
600e5dd7070Spatrick     return ScopeInfo;
601e5dd7070Spatrick 
602e5dd7070Spatrick   if (!S.buildCoroutineParameterMoves(Loc))
603e5dd7070Spatrick     return nullptr;
604e5dd7070Spatrick 
605e5dd7070Spatrick   ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc);
606e5dd7070Spatrick   if (!ScopeInfo->CoroutinePromise)
607e5dd7070Spatrick     return nullptr;
608e5dd7070Spatrick 
609e5dd7070Spatrick   return ScopeInfo;
610e5dd7070Spatrick }
611e5dd7070Spatrick 
612ec727ea7Spatrick /// Recursively check \p E and all its children to see if any call target
613ec727ea7Spatrick /// (including constructor call) is declared noexcept. Also any value returned
614ec727ea7Spatrick /// from the call has a noexcept destructor.
checkNoThrow(Sema & S,const Stmt * E,llvm::SmallPtrSetImpl<const Decl * > & ThrowingDecls)615ec727ea7Spatrick static void checkNoThrow(Sema &S, const Stmt *E,
616ec727ea7Spatrick                          llvm::SmallPtrSetImpl<const Decl *> &ThrowingDecls) {
617ec727ea7Spatrick   auto checkDeclNoexcept = [&](const Decl *D, bool IsDtor = false) {
618ec727ea7Spatrick     // In the case of dtor, the call to dtor is implicit and hence we should
619ec727ea7Spatrick     // pass nullptr to canCalleeThrow.
620ec727ea7Spatrick     if (Sema::canCalleeThrow(S, IsDtor ? nullptr : cast<Expr>(E), D)) {
621ec727ea7Spatrick       if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
622ec727ea7Spatrick         // co_await promise.final_suspend() could end up calling
623ec727ea7Spatrick         // __builtin_coro_resume for symmetric transfer if await_suspend()
624ec727ea7Spatrick         // returns a handle. In that case, even __builtin_coro_resume is not
625ec727ea7Spatrick         // declared as noexcept and may throw, it does not throw _into_ the
626ec727ea7Spatrick         // coroutine that just suspended, but rather throws back out from
627ec727ea7Spatrick         // whoever called coroutine_handle::resume(), hence we claim that
628ec727ea7Spatrick         // logically it does not throw.
629ec727ea7Spatrick         if (FD->getBuiltinID() == Builtin::BI__builtin_coro_resume)
630ec727ea7Spatrick           return;
631ec727ea7Spatrick       }
632ec727ea7Spatrick       if (ThrowingDecls.empty()) {
633*12c85518Srobert         // [dcl.fct.def.coroutine]p15
634*12c85518Srobert         //   The expression co_await promise.final_suspend() shall not be
635*12c85518Srobert         //   potentially-throwing ([except.spec]).
636*12c85518Srobert         //
637ec727ea7Spatrick         // First time seeing an error, emit the error message.
638ec727ea7Spatrick         S.Diag(cast<FunctionDecl>(S.CurContext)->getLocation(),
639ec727ea7Spatrick                diag::err_coroutine_promise_final_suspend_requires_nothrow);
640ec727ea7Spatrick       }
641ec727ea7Spatrick       ThrowingDecls.insert(D);
642ec727ea7Spatrick     }
643ec727ea7Spatrick   };
644*12c85518Srobert 
645*12c85518Srobert   if (auto *CE = dyn_cast<CXXConstructExpr>(E)) {
646*12c85518Srobert     CXXConstructorDecl *Ctor = CE->getConstructor();
647ec727ea7Spatrick     checkDeclNoexcept(Ctor);
648ec727ea7Spatrick     // Check the corresponding destructor of the constructor.
649*12c85518Srobert     checkDeclNoexcept(Ctor->getParent()->getDestructor(), /*IsDtor=*/true);
650*12c85518Srobert   } else if (auto *CE = dyn_cast<CallExpr>(E)) {
651*12c85518Srobert     if (CE->isTypeDependent())
652*12c85518Srobert       return;
653*12c85518Srobert 
654*12c85518Srobert     checkDeclNoexcept(CE->getCalleeDecl());
655*12c85518Srobert     QualType ReturnType = CE->getCallReturnType(S.getASTContext());
656ec727ea7Spatrick     // Check the destructor of the call return type, if any.
657ec727ea7Spatrick     if (ReturnType.isDestructedType() ==
658ec727ea7Spatrick         QualType::DestructionKind::DK_cxx_destructor) {
659ec727ea7Spatrick       const auto *T =
660ec727ea7Spatrick           cast<RecordType>(ReturnType.getCanonicalType().getTypePtr());
661*12c85518Srobert       checkDeclNoexcept(cast<CXXRecordDecl>(T->getDecl())->getDestructor(),
662*12c85518Srobert                         /*IsDtor=*/true);
663ec727ea7Spatrick     }
664*12c85518Srobert   } else
665ec727ea7Spatrick     for (const auto *Child : E->children()) {
666ec727ea7Spatrick       if (!Child)
667ec727ea7Spatrick         continue;
668ec727ea7Spatrick       checkNoThrow(S, Child, ThrowingDecls);
669ec727ea7Spatrick     }
670ec727ea7Spatrick }
671ec727ea7Spatrick 
checkFinalSuspendNoThrow(const Stmt * FinalSuspend)672ec727ea7Spatrick bool Sema::checkFinalSuspendNoThrow(const Stmt *FinalSuspend) {
673ec727ea7Spatrick   llvm::SmallPtrSet<const Decl *, 4> ThrowingDecls;
674ec727ea7Spatrick   // We first collect all declarations that should not throw but not declared
675ec727ea7Spatrick   // with noexcept. We then sort them based on the location before printing.
676ec727ea7Spatrick   // This is to avoid emitting the same note multiple times on the same
677ec727ea7Spatrick   // declaration, and also provide a deterministic order for the messages.
678ec727ea7Spatrick   checkNoThrow(*this, FinalSuspend, ThrowingDecls);
679ec727ea7Spatrick   auto SortedDecls = llvm::SmallVector<const Decl *, 4>{ThrowingDecls.begin(),
680ec727ea7Spatrick                                                         ThrowingDecls.end()};
681ec727ea7Spatrick   sort(SortedDecls, [](const Decl *A, const Decl *B) {
682ec727ea7Spatrick     return A->getEndLoc() < B->getEndLoc();
683ec727ea7Spatrick   });
684ec727ea7Spatrick   for (const auto *D : SortedDecls) {
685ec727ea7Spatrick     Diag(D->getEndLoc(), diag::note_coroutine_function_declare_noexcept);
686ec727ea7Spatrick   }
687ec727ea7Spatrick   return ThrowingDecls.empty();
688ec727ea7Spatrick }
689ec727ea7Spatrick 
ActOnCoroutineBodyStart(Scope * SC,SourceLocation KWLoc,StringRef Keyword)690e5dd7070Spatrick bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc,
691e5dd7070Spatrick                                    StringRef Keyword) {
692e5dd7070Spatrick   if (!checkCoroutineContext(*this, KWLoc, Keyword))
693e5dd7070Spatrick     return false;
694e5dd7070Spatrick   auto *ScopeInfo = getCurFunction();
695e5dd7070Spatrick   assert(ScopeInfo->CoroutinePromise);
696e5dd7070Spatrick 
697e5dd7070Spatrick   // If we have existing coroutine statements then we have already built
698e5dd7070Spatrick   // the initial and final suspend points.
699e5dd7070Spatrick   if (!ScopeInfo->NeedsCoroutineSuspends)
700e5dd7070Spatrick     return true;
701e5dd7070Spatrick 
702e5dd7070Spatrick   ScopeInfo->setNeedsCoroutineSuspends(false);
703e5dd7070Spatrick 
704e5dd7070Spatrick   auto *Fn = cast<FunctionDecl>(CurContext);
705e5dd7070Spatrick   SourceLocation Loc = Fn->getLocation();
706e5dd7070Spatrick   // Build the initial suspend point
707e5dd7070Spatrick   auto buildSuspends = [&](StringRef Name) mutable -> StmtResult {
708*12c85518Srobert     ExprResult Operand = buildPromiseCall(*this, ScopeInfo->CoroutinePromise,
709*12c85518Srobert                                           Loc, Name, std::nullopt);
710*12c85518Srobert     if (Operand.isInvalid())
711*12c85518Srobert       return StmtError();
712e5dd7070Spatrick     ExprResult Suspend =
713*12c85518Srobert         buildOperatorCoawaitCall(*this, SC, Loc, Operand.get());
714e5dd7070Spatrick     if (Suspend.isInvalid())
715e5dd7070Spatrick       return StmtError();
716*12c85518Srobert     Suspend = BuildResolvedCoawaitExpr(Loc, Operand.get(), Suspend.get(),
717e5dd7070Spatrick                                        /*IsImplicit*/ true);
718e5dd7070Spatrick     Suspend = ActOnFinishFullExpr(Suspend.get(), /*DiscardedValue*/ false);
719e5dd7070Spatrick     if (Suspend.isInvalid()) {
720e5dd7070Spatrick       Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required)
721e5dd7070Spatrick           << ((Name == "initial_suspend") ? 0 : 1);
722e5dd7070Spatrick       Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword;
723e5dd7070Spatrick       return StmtError();
724e5dd7070Spatrick     }
725e5dd7070Spatrick     return cast<Stmt>(Suspend.get());
726e5dd7070Spatrick   };
727e5dd7070Spatrick 
728e5dd7070Spatrick   StmtResult InitSuspend = buildSuspends("initial_suspend");
729e5dd7070Spatrick   if (InitSuspend.isInvalid())
730e5dd7070Spatrick     return true;
731e5dd7070Spatrick 
732e5dd7070Spatrick   StmtResult FinalSuspend = buildSuspends("final_suspend");
733ec727ea7Spatrick   if (FinalSuspend.isInvalid() || !checkFinalSuspendNoThrow(FinalSuspend.get()))
734e5dd7070Spatrick     return true;
735e5dd7070Spatrick 
736e5dd7070Spatrick   ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get());
737e5dd7070Spatrick 
738e5dd7070Spatrick   return true;
739e5dd7070Spatrick }
740e5dd7070Spatrick 
741e5dd7070Spatrick // Recursively walks up the scope hierarchy until either a 'catch' or a function
742e5dd7070Spatrick // scope is found, whichever comes first.
isWithinCatchScope(Scope * S)743e5dd7070Spatrick static bool isWithinCatchScope(Scope *S) {
744e5dd7070Spatrick   // 'co_await' and 'co_yield' keywords are disallowed within catch blocks, but
745e5dd7070Spatrick   // lambdas that use 'co_await' are allowed. The loop below ends when a
746e5dd7070Spatrick   // function scope is found in order to ensure the following behavior:
747e5dd7070Spatrick   //
748e5dd7070Spatrick   // void foo() {      // <- function scope
749e5dd7070Spatrick   //   try {           //
750e5dd7070Spatrick   //     co_await x;   // <- 'co_await' is OK within a function scope
751e5dd7070Spatrick   //   } catch {       // <- catch scope
752e5dd7070Spatrick   //     co_await x;   // <- 'co_await' is not OK within a catch scope
753e5dd7070Spatrick   //     []() {        // <- function scope
754e5dd7070Spatrick   //       co_await x; // <- 'co_await' is OK within a function scope
755e5dd7070Spatrick   //     }();
756e5dd7070Spatrick   //   }
757e5dd7070Spatrick   // }
758*12c85518Srobert   while (S && !S->isFunctionScope()) {
759*12c85518Srobert     if (S->isCatchScope())
760e5dd7070Spatrick       return true;
761e5dd7070Spatrick     S = S->getParent();
762e5dd7070Spatrick   }
763e5dd7070Spatrick   return false;
764e5dd7070Spatrick }
765e5dd7070Spatrick 
766e5dd7070Spatrick // [expr.await]p2, emphasis added: "An await-expression shall appear only in
767e5dd7070Spatrick // a *potentially evaluated* expression within the compound-statement of a
768e5dd7070Spatrick // function-body *outside of a handler* [...] A context within a function
769e5dd7070Spatrick // where an await-expression can appear is called a suspension context of the
770e5dd7070Spatrick // function."
checkSuspensionContext(Sema & S,SourceLocation Loc,StringRef Keyword)771*12c85518Srobert static bool checkSuspensionContext(Sema &S, SourceLocation Loc,
772e5dd7070Spatrick                                    StringRef Keyword) {
773e5dd7070Spatrick   // First emphasis of [expr.await]p2: must be a potentially evaluated context.
774e5dd7070Spatrick   // That is, 'co_await' and 'co_yield' cannot appear in subexpressions of
775e5dd7070Spatrick   // \c sizeof.
776*12c85518Srobert   if (S.isUnevaluatedContext()) {
777e5dd7070Spatrick     S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword;
778*12c85518Srobert     return false;
779*12c85518Srobert   }
780e5dd7070Spatrick 
781e5dd7070Spatrick   // Second emphasis of [expr.await]p2: must be outside of an exception handler.
782*12c85518Srobert   if (isWithinCatchScope(S.getCurScope())) {
783e5dd7070Spatrick     S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword;
784*12c85518Srobert     return false;
785*12c85518Srobert   }
786*12c85518Srobert 
787*12c85518Srobert   return true;
788e5dd7070Spatrick }
789e5dd7070Spatrick 
ActOnCoawaitExpr(Scope * S,SourceLocation Loc,Expr * E)790e5dd7070Spatrick ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) {
791*12c85518Srobert   if (!checkSuspensionContext(*this, Loc, "co_await"))
792*12c85518Srobert     return ExprError();
793*12c85518Srobert 
794e5dd7070Spatrick   if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) {
795e5dd7070Spatrick     CorrectDelayedTyposInExpr(E);
796e5dd7070Spatrick     return ExprError();
797e5dd7070Spatrick   }
798e5dd7070Spatrick 
799*12c85518Srobert   if (E->hasPlaceholderType()) {
800e5dd7070Spatrick     ExprResult R = CheckPlaceholderExpr(E);
801e5dd7070Spatrick     if (R.isInvalid()) return ExprError();
802e5dd7070Spatrick     E = R.get();
803e5dd7070Spatrick   }
804*12c85518Srobert   ExprResult Lookup = BuildOperatorCoawaitLookupExpr(S, Loc);
805e5dd7070Spatrick   if (Lookup.isInvalid())
806e5dd7070Spatrick     return ExprError();
807e5dd7070Spatrick   return BuildUnresolvedCoawaitExpr(Loc, E,
808e5dd7070Spatrick                                    cast<UnresolvedLookupExpr>(Lookup.get()));
809e5dd7070Spatrick }
810e5dd7070Spatrick 
BuildOperatorCoawaitLookupExpr(Scope * S,SourceLocation Loc)811*12c85518Srobert ExprResult Sema::BuildOperatorCoawaitLookupExpr(Scope *S, SourceLocation Loc) {
812*12c85518Srobert   DeclarationName OpName =
813*12c85518Srobert       Context.DeclarationNames.getCXXOperatorName(OO_Coawait);
814*12c85518Srobert   LookupResult Operators(*this, OpName, SourceLocation(),
815*12c85518Srobert                          Sema::LookupOperatorName);
816*12c85518Srobert   LookupName(Operators, S);
817*12c85518Srobert 
818*12c85518Srobert   assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
819*12c85518Srobert   const auto &Functions = Operators.asUnresolvedSet();
820*12c85518Srobert   bool IsOverloaded =
821*12c85518Srobert       Functions.size() > 1 ||
822*12c85518Srobert       (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin()));
823*12c85518Srobert   Expr *CoawaitOp = UnresolvedLookupExpr::Create(
824*12c85518Srobert       Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(),
825*12c85518Srobert       DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, IsOverloaded,
826*12c85518Srobert       Functions.begin(), Functions.end());
827*12c85518Srobert   assert(CoawaitOp);
828*12c85518Srobert   return CoawaitOp;
829*12c85518Srobert }
830*12c85518Srobert 
831*12c85518Srobert // Attempts to resolve and build a CoawaitExpr from "raw" inputs, bailing out to
832*12c85518Srobert // DependentCoawaitExpr if needed.
BuildUnresolvedCoawaitExpr(SourceLocation Loc,Expr * Operand,UnresolvedLookupExpr * Lookup)833*12c85518Srobert ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *Operand,
834e5dd7070Spatrick                                             UnresolvedLookupExpr *Lookup) {
835e5dd7070Spatrick   auto *FSI = checkCoroutineContext(*this, Loc, "co_await");
836e5dd7070Spatrick   if (!FSI)
837e5dd7070Spatrick     return ExprError();
838e5dd7070Spatrick 
839*12c85518Srobert   if (Operand->hasPlaceholderType()) {
840*12c85518Srobert     ExprResult R = CheckPlaceholderExpr(Operand);
841e5dd7070Spatrick     if (R.isInvalid())
842e5dd7070Spatrick       return ExprError();
843*12c85518Srobert     Operand = R.get();
844e5dd7070Spatrick   }
845e5dd7070Spatrick 
846e5dd7070Spatrick   auto *Promise = FSI->CoroutinePromise;
847e5dd7070Spatrick   if (Promise->getType()->isDependentType()) {
848*12c85518Srobert     Expr *Res = new (Context)
849*12c85518Srobert         DependentCoawaitExpr(Loc, Context.DependentTy, Operand, Lookup);
850e5dd7070Spatrick     return Res;
851e5dd7070Spatrick   }
852e5dd7070Spatrick 
853e5dd7070Spatrick   auto *RD = Promise->getType()->getAsCXXRecordDecl();
854*12c85518Srobert   auto *Transformed = Operand;
855e5dd7070Spatrick   if (lookupMember(*this, "await_transform", RD, Loc)) {
856*12c85518Srobert     ExprResult R =
857*12c85518Srobert         buildPromiseCall(*this, Promise, Loc, "await_transform", Operand);
858e5dd7070Spatrick     if (R.isInvalid()) {
859e5dd7070Spatrick       Diag(Loc,
860e5dd7070Spatrick            diag::note_coroutine_promise_implicit_await_transform_required_here)
861*12c85518Srobert           << Operand->getSourceRange();
862e5dd7070Spatrick       return ExprError();
863e5dd7070Spatrick     }
864*12c85518Srobert     Transformed = R.get();
865e5dd7070Spatrick   }
866*12c85518Srobert   ExprResult Awaiter = BuildOperatorCoawaitCall(Loc, Transformed, Lookup);
867*12c85518Srobert   if (Awaiter.isInvalid())
868e5dd7070Spatrick     return ExprError();
869e5dd7070Spatrick 
870*12c85518Srobert   return BuildResolvedCoawaitExpr(Loc, Operand, Awaiter.get());
871e5dd7070Spatrick }
872e5dd7070Spatrick 
BuildResolvedCoawaitExpr(SourceLocation Loc,Expr * Operand,Expr * Awaiter,bool IsImplicit)873*12c85518Srobert ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *Operand,
874*12c85518Srobert                                           Expr *Awaiter, bool IsImplicit) {
875e5dd7070Spatrick   auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit);
876e5dd7070Spatrick   if (!Coroutine)
877e5dd7070Spatrick     return ExprError();
878e5dd7070Spatrick 
879*12c85518Srobert   if (Awaiter->hasPlaceholderType()) {
880*12c85518Srobert     ExprResult R = CheckPlaceholderExpr(Awaiter);
881e5dd7070Spatrick     if (R.isInvalid()) return ExprError();
882*12c85518Srobert     Awaiter = R.get();
883e5dd7070Spatrick   }
884e5dd7070Spatrick 
885*12c85518Srobert   if (Awaiter->getType()->isDependentType()) {
886e5dd7070Spatrick     Expr *Res = new (Context)
887*12c85518Srobert         CoawaitExpr(Loc, Context.DependentTy, Operand, Awaiter, IsImplicit);
888e5dd7070Spatrick     return Res;
889e5dd7070Spatrick   }
890e5dd7070Spatrick 
891e5dd7070Spatrick   // If the expression is a temporary, materialize it as an lvalue so that we
892e5dd7070Spatrick   // can use it multiple times.
893*12c85518Srobert   if (Awaiter->isPRValue())
894*12c85518Srobert     Awaiter = CreateMaterializeTemporaryExpr(Awaiter->getType(), Awaiter, true);
895e5dd7070Spatrick 
896e5dd7070Spatrick   // The location of the `co_await` token cannot be used when constructing
897e5dd7070Spatrick   // the member call expressions since it's before the location of `Expr`, which
898e5dd7070Spatrick   // is used as the start of the member call expression.
899*12c85518Srobert   SourceLocation CallLoc = Awaiter->getExprLoc();
900e5dd7070Spatrick 
901e5dd7070Spatrick   // Build the await_ready, await_suspend, await_resume calls.
902*12c85518Srobert   ReadySuspendResumeResult RSS =
903*12c85518Srobert       buildCoawaitCalls(*this, Coroutine->CoroutinePromise, CallLoc, Awaiter);
904e5dd7070Spatrick   if (RSS.IsInvalid)
905e5dd7070Spatrick     return ExprError();
906e5dd7070Spatrick 
907*12c85518Srobert   Expr *Res = new (Context)
908*12c85518Srobert       CoawaitExpr(Loc, Operand, Awaiter, RSS.Results[0], RSS.Results[1],
909e5dd7070Spatrick                   RSS.Results[2], RSS.OpaqueValue, IsImplicit);
910e5dd7070Spatrick 
911e5dd7070Spatrick   return Res;
912e5dd7070Spatrick }
913e5dd7070Spatrick 
ActOnCoyieldExpr(Scope * S,SourceLocation Loc,Expr * E)914e5dd7070Spatrick ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) {
915*12c85518Srobert   if (!checkSuspensionContext(*this, Loc, "co_yield"))
916*12c85518Srobert     return ExprError();
917*12c85518Srobert 
918e5dd7070Spatrick   if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) {
919e5dd7070Spatrick     CorrectDelayedTyposInExpr(E);
920e5dd7070Spatrick     return ExprError();
921e5dd7070Spatrick   }
922e5dd7070Spatrick 
923e5dd7070Spatrick   // Build yield_value call.
924e5dd7070Spatrick   ExprResult Awaitable = buildPromiseCall(
925e5dd7070Spatrick       *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E);
926e5dd7070Spatrick   if (Awaitable.isInvalid())
927e5dd7070Spatrick     return ExprError();
928e5dd7070Spatrick 
929e5dd7070Spatrick   // Build 'operator co_await' call.
930e5dd7070Spatrick   Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get());
931e5dd7070Spatrick   if (Awaitable.isInvalid())
932e5dd7070Spatrick     return ExprError();
933e5dd7070Spatrick 
934e5dd7070Spatrick   return BuildCoyieldExpr(Loc, Awaitable.get());
935e5dd7070Spatrick }
BuildCoyieldExpr(SourceLocation Loc,Expr * E)936e5dd7070Spatrick ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) {
937e5dd7070Spatrick   auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield");
938e5dd7070Spatrick   if (!Coroutine)
939e5dd7070Spatrick     return ExprError();
940e5dd7070Spatrick 
941*12c85518Srobert   if (E->hasPlaceholderType()) {
942e5dd7070Spatrick     ExprResult R = CheckPlaceholderExpr(E);
943e5dd7070Spatrick     if (R.isInvalid()) return ExprError();
944e5dd7070Spatrick     E = R.get();
945e5dd7070Spatrick   }
946e5dd7070Spatrick 
947*12c85518Srobert   Expr *Operand = E;
948*12c85518Srobert 
949e5dd7070Spatrick   if (E->getType()->isDependentType()) {
950*12c85518Srobert     Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, Operand, E);
951e5dd7070Spatrick     return Res;
952e5dd7070Spatrick   }
953e5dd7070Spatrick 
954e5dd7070Spatrick   // If the expression is a temporary, materialize it as an lvalue so that we
955e5dd7070Spatrick   // can use it multiple times.
956a9ac8606Spatrick   if (E->isPRValue())
957e5dd7070Spatrick     E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
958e5dd7070Spatrick 
959e5dd7070Spatrick   // Build the await_ready, await_suspend, await_resume calls.
960a9ac8606Spatrick   ReadySuspendResumeResult RSS = buildCoawaitCalls(
961a9ac8606Spatrick       *this, Coroutine->CoroutinePromise, Loc, E);
962e5dd7070Spatrick   if (RSS.IsInvalid)
963e5dd7070Spatrick     return ExprError();
964e5dd7070Spatrick 
965e5dd7070Spatrick   Expr *Res =
966*12c85518Srobert       new (Context) CoyieldExpr(Loc, Operand, E, RSS.Results[0], RSS.Results[1],
967e5dd7070Spatrick                                 RSS.Results[2], RSS.OpaqueValue);
968e5dd7070Spatrick 
969e5dd7070Spatrick   return Res;
970e5dd7070Spatrick }
971e5dd7070Spatrick 
ActOnCoreturnStmt(Scope * S,SourceLocation Loc,Expr * E)972e5dd7070Spatrick StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) {
973e5dd7070Spatrick   if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) {
974e5dd7070Spatrick     CorrectDelayedTyposInExpr(E);
975e5dd7070Spatrick     return StmtError();
976e5dd7070Spatrick   }
977e5dd7070Spatrick   return BuildCoreturnStmt(Loc, E);
978e5dd7070Spatrick }
979e5dd7070Spatrick 
BuildCoreturnStmt(SourceLocation Loc,Expr * E,bool IsImplicit)980e5dd7070Spatrick StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E,
981e5dd7070Spatrick                                    bool IsImplicit) {
982e5dd7070Spatrick   auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit);
983e5dd7070Spatrick   if (!FSI)
984e5dd7070Spatrick     return StmtError();
985e5dd7070Spatrick 
986*12c85518Srobert   if (E && E->hasPlaceholderType() &&
987*12c85518Srobert       !E->hasPlaceholderType(BuiltinType::Overload)) {
988e5dd7070Spatrick     ExprResult R = CheckPlaceholderExpr(E);
989e5dd7070Spatrick     if (R.isInvalid()) return StmtError();
990e5dd7070Spatrick     E = R.get();
991e5dd7070Spatrick   }
992e5dd7070Spatrick 
993e5dd7070Spatrick   VarDecl *Promise = FSI->CoroutinePromise;
994e5dd7070Spatrick   ExprResult PC;
995e5dd7070Spatrick   if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) {
996a9ac8606Spatrick     getNamedReturnInfo(E, SimplerImplicitMoveMode::ForceOn);
997e5dd7070Spatrick     PC = buildPromiseCall(*this, Promise, Loc, "return_value", E);
998e5dd7070Spatrick   } else {
999e5dd7070Spatrick     E = MakeFullDiscardedValueExpr(E).get();
1000*12c85518Srobert     PC = buildPromiseCall(*this, Promise, Loc, "return_void", std::nullopt);
1001e5dd7070Spatrick   }
1002e5dd7070Spatrick   if (PC.isInvalid())
1003e5dd7070Spatrick     return StmtError();
1004e5dd7070Spatrick 
1005e5dd7070Spatrick   Expr *PCE = ActOnFinishFullExpr(PC.get(), /*DiscardedValue*/ false).get();
1006e5dd7070Spatrick 
1007e5dd7070Spatrick   Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit);
1008e5dd7070Spatrick   return Res;
1009e5dd7070Spatrick }
1010e5dd7070Spatrick 
1011e5dd7070Spatrick /// Look up the std::nothrow object.
buildStdNoThrowDeclRef(Sema & S,SourceLocation Loc)1012e5dd7070Spatrick static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) {
1013e5dd7070Spatrick   NamespaceDecl *Std = S.getStdNamespace();
1014e5dd7070Spatrick   assert(Std && "Should already be diagnosed");
1015e5dd7070Spatrick 
1016e5dd7070Spatrick   LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc,
1017e5dd7070Spatrick                       Sema::LookupOrdinaryName);
1018e5dd7070Spatrick   if (!S.LookupQualifiedName(Result, Std)) {
1019*12c85518Srobert     // <coroutine> is not requred to include <new>, so we couldn't omit
1020*12c85518Srobert     // the check here.
1021e5dd7070Spatrick     S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found);
1022e5dd7070Spatrick     return nullptr;
1023e5dd7070Spatrick   }
1024e5dd7070Spatrick 
1025e5dd7070Spatrick   auto *VD = Result.getAsSingle<VarDecl>();
1026e5dd7070Spatrick   if (!VD) {
1027e5dd7070Spatrick     Result.suppressDiagnostics();
1028e5dd7070Spatrick     // We found something weird. Complain about the first thing we found.
1029e5dd7070Spatrick     NamedDecl *Found = *Result.begin();
1030e5dd7070Spatrick     S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow);
1031e5dd7070Spatrick     return nullptr;
1032e5dd7070Spatrick   }
1033e5dd7070Spatrick 
1034e5dd7070Spatrick   ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc);
1035e5dd7070Spatrick   if (DR.isInvalid())
1036e5dd7070Spatrick     return nullptr;
1037e5dd7070Spatrick 
1038e5dd7070Spatrick   return DR.get();
1039e5dd7070Spatrick }
1040e5dd7070Spatrick 
getTypeSourceInfoForStdAlignValT(Sema & S,SourceLocation Loc)1041*12c85518Srobert static TypeSourceInfo *getTypeSourceInfoForStdAlignValT(Sema &S,
1042*12c85518Srobert                                                         SourceLocation Loc) {
1043*12c85518Srobert   EnumDecl *StdAlignValT = S.getStdAlignValT();
1044*12c85518Srobert   QualType StdAlignValDecl = S.Context.getTypeDeclType(StdAlignValT);
1045*12c85518Srobert   return S.Context.getTrivialTypeSourceInfo(StdAlignValDecl);
1046*12c85518Srobert }
1047e5dd7070Spatrick 
1048*12c85518Srobert // Find an appropriate delete for the promise.
findDeleteForPromise(Sema & S,SourceLocation Loc,QualType PromiseType,FunctionDecl * & OperatorDelete)1049*12c85518Srobert static bool findDeleteForPromise(Sema &S, SourceLocation Loc, QualType PromiseType,
1050*12c85518Srobert                                  FunctionDecl *&OperatorDelete) {
1051e5dd7070Spatrick   DeclarationName DeleteName =
1052e5dd7070Spatrick       S.Context.DeclarationNames.getCXXOperatorName(OO_Delete);
1053e5dd7070Spatrick 
1054e5dd7070Spatrick   auto *PointeeRD = PromiseType->getAsCXXRecordDecl();
1055e5dd7070Spatrick   assert(PointeeRD && "PromiseType must be a CxxRecordDecl type");
1056e5dd7070Spatrick 
1057*12c85518Srobert   const bool Overaligned = S.getLangOpts().CoroAlignedAllocation;
1058e5dd7070Spatrick 
1059*12c85518Srobert   // [dcl.fct.def.coroutine]p12
1060*12c85518Srobert   // The deallocation function's name is looked up by searching for it in the
1061*12c85518Srobert   // scope of the promise type. If nothing is found, a search is performed in
1062*12c85518Srobert   // the global scope.
1063*12c85518Srobert   if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete,
1064*12c85518Srobert                                  /*Diagnose*/ true, /*WantSize*/ true,
1065*12c85518Srobert                                  /*WantAligned*/ Overaligned))
1066*12c85518Srobert     return false;
1067*12c85518Srobert 
1068*12c85518Srobert   // [dcl.fct.def.coroutine]p12
1069*12c85518Srobert   //   If both a usual deallocation function with only a pointer parameter and a
1070*12c85518Srobert   //   usual deallocation function with both a pointer parameter and a size
1071*12c85518Srobert   //   parameter are found, then the selected deallocation function shall be the
1072*12c85518Srobert   //   one with two parameters. Otherwise, the selected deallocation function
1073*12c85518Srobert   //   shall be the function with one parameter.
1074e5dd7070Spatrick   if (!OperatorDelete) {
1075e5dd7070Spatrick     // Look for a global declaration.
1076*12c85518Srobert     // Coroutines can always provide their required size.
1077*12c85518Srobert     const bool CanProvideSize = true;
1078*12c85518Srobert     // Sema::FindUsualDeallocationFunction will try to find the one with two
1079*12c85518Srobert     // parameters first. It will return the deallocation function with one
1080*12c85518Srobert     // parameter if failed.
1081e5dd7070Spatrick     OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize,
1082e5dd7070Spatrick                                                      Overaligned, DeleteName);
1083*12c85518Srobert 
1084*12c85518Srobert     if (!OperatorDelete)
1085*12c85518Srobert       return false;
1086e5dd7070Spatrick   }
1087*12c85518Srobert 
1088e5dd7070Spatrick   S.MarkFunctionReferenced(Loc, OperatorDelete);
1089*12c85518Srobert   return true;
1090e5dd7070Spatrick }
1091e5dd7070Spatrick 
1092e5dd7070Spatrick 
CheckCompletedCoroutineBody(FunctionDecl * FD,Stmt * & Body)1093e5dd7070Spatrick void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) {
1094e5dd7070Spatrick   FunctionScopeInfo *Fn = getCurFunction();
1095e5dd7070Spatrick   assert(Fn && Fn->isCoroutine() && "not a coroutine");
1096e5dd7070Spatrick   if (!Body) {
1097e5dd7070Spatrick     assert(FD->isInvalidDecl() &&
1098e5dd7070Spatrick            "a null body is only allowed for invalid declarations");
1099e5dd7070Spatrick     return;
1100e5dd7070Spatrick   }
1101e5dd7070Spatrick   // We have a function that uses coroutine keywords, but we failed to build
1102e5dd7070Spatrick   // the promise type.
1103e5dd7070Spatrick   if (!Fn->CoroutinePromise)
1104e5dd7070Spatrick     return FD->setInvalidDecl();
1105e5dd7070Spatrick 
1106e5dd7070Spatrick   if (isa<CoroutineBodyStmt>(Body)) {
1107e5dd7070Spatrick     // Nothing todo. the body is already a transformed coroutine body statement.
1108e5dd7070Spatrick     return;
1109e5dd7070Spatrick   }
1110e5dd7070Spatrick 
1111*12c85518Srobert   // The always_inline attribute doesn't reliably apply to a coroutine,
1112*12c85518Srobert   // because the coroutine will be split into pieces and some pieces
1113*12c85518Srobert   // might be called indirectly, as in a virtual call. Even the ramp
1114*12c85518Srobert   // function cannot be inlined at -O0, due to pipeline ordering
1115*12c85518Srobert   // problems (see https://llvm.org/PR53413). Tell the user about it.
1116*12c85518Srobert   if (FD->hasAttr<AlwaysInlineAttr>())
1117*12c85518Srobert     Diag(FD->getLocation(), diag::warn_always_inline_coroutine);
1118*12c85518Srobert 
1119*12c85518Srobert   // [stmt.return.coroutine]p1:
1120*12c85518Srobert   //   A coroutine shall not enclose a return statement ([stmt.return]).
1121e5dd7070Spatrick   if (Fn->FirstReturnLoc.isValid()) {
1122e5dd7070Spatrick     assert(Fn->FirstCoroutineStmtLoc.isValid() &&
1123e5dd7070Spatrick                    "first coroutine location not set");
1124e5dd7070Spatrick     Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine);
1125e5dd7070Spatrick     Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1126e5dd7070Spatrick             << Fn->getFirstCoroutineStmtKeyword();
1127e5dd7070Spatrick   }
1128*12c85518Srobert 
1129*12c85518Srobert   // Coroutines will get splitted into pieces. The GNU address of label
1130*12c85518Srobert   // extension wouldn't be meaningful in coroutines.
1131*12c85518Srobert   for (AddrLabelExpr *ALE : Fn->AddrLabels)
1132*12c85518Srobert     Diag(ALE->getBeginLoc(), diag::err_coro_invalid_addr_of_label);
1133*12c85518Srobert 
1134e5dd7070Spatrick   CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body);
1135e5dd7070Spatrick   if (Builder.isInvalid() || !Builder.buildStatements())
1136e5dd7070Spatrick     return FD->setInvalidDecl();
1137e5dd7070Spatrick 
1138e5dd7070Spatrick   // Build body for the coroutine wrapper statement.
1139e5dd7070Spatrick   Body = CoroutineBodyStmt::Create(Context, Builder);
1140e5dd7070Spatrick }
1141e5dd7070Spatrick 
CoroutineStmtBuilder(Sema & S,FunctionDecl & FD,sema::FunctionScopeInfo & Fn,Stmt * Body)1142e5dd7070Spatrick CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD,
1143e5dd7070Spatrick                                            sema::FunctionScopeInfo &Fn,
1144e5dd7070Spatrick                                            Stmt *Body)
1145e5dd7070Spatrick     : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()),
1146e5dd7070Spatrick       IsPromiseDependentType(
1147e5dd7070Spatrick           !Fn.CoroutinePromise ||
1148e5dd7070Spatrick           Fn.CoroutinePromise->getType()->isDependentType()) {
1149e5dd7070Spatrick   this->Body = Body;
1150e5dd7070Spatrick 
1151e5dd7070Spatrick   for (auto KV : Fn.CoroutineParameterMoves)
1152e5dd7070Spatrick     this->ParamMovesVector.push_back(KV.second);
1153e5dd7070Spatrick   this->ParamMoves = this->ParamMovesVector;
1154e5dd7070Spatrick 
1155e5dd7070Spatrick   if (!IsPromiseDependentType) {
1156e5dd7070Spatrick     PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl();
1157e5dd7070Spatrick     assert(PromiseRecordDecl && "Type should have already been checked");
1158e5dd7070Spatrick   }
1159e5dd7070Spatrick   this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend();
1160e5dd7070Spatrick }
1161e5dd7070Spatrick 
buildStatements()1162e5dd7070Spatrick bool CoroutineStmtBuilder::buildStatements() {
1163e5dd7070Spatrick   assert(this->IsValid && "coroutine already invalid");
1164e5dd7070Spatrick   this->IsValid = makeReturnObject();
1165e5dd7070Spatrick   if (this->IsValid && !IsPromiseDependentType)
1166e5dd7070Spatrick     buildDependentStatements();
1167e5dd7070Spatrick   return this->IsValid;
1168e5dd7070Spatrick }
1169e5dd7070Spatrick 
buildDependentStatements()1170e5dd7070Spatrick bool CoroutineStmtBuilder::buildDependentStatements() {
1171e5dd7070Spatrick   assert(this->IsValid && "coroutine already invalid");
1172e5dd7070Spatrick   assert(!this->IsPromiseDependentType &&
1173e5dd7070Spatrick          "coroutine cannot have a dependent promise type");
1174e5dd7070Spatrick   this->IsValid = makeOnException() && makeOnFallthrough() &&
1175e5dd7070Spatrick                   makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() &&
1176e5dd7070Spatrick                   makeNewAndDeleteExpr();
1177e5dd7070Spatrick   return this->IsValid;
1178e5dd7070Spatrick }
1179e5dd7070Spatrick 
makePromiseStmt()1180e5dd7070Spatrick bool CoroutineStmtBuilder::makePromiseStmt() {
1181e5dd7070Spatrick   // Form a declaration statement for the promise declaration, so that AST
1182e5dd7070Spatrick   // visitors can more easily find it.
1183e5dd7070Spatrick   StmtResult PromiseStmt =
1184e5dd7070Spatrick       S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc);
1185e5dd7070Spatrick   if (PromiseStmt.isInvalid())
1186e5dd7070Spatrick     return false;
1187e5dd7070Spatrick 
1188e5dd7070Spatrick   this->Promise = PromiseStmt.get();
1189e5dd7070Spatrick   return true;
1190e5dd7070Spatrick }
1191e5dd7070Spatrick 
makeInitialAndFinalSuspend()1192e5dd7070Spatrick bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() {
1193e5dd7070Spatrick   if (Fn.hasInvalidCoroutineSuspends())
1194e5dd7070Spatrick     return false;
1195e5dd7070Spatrick   this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first);
1196e5dd7070Spatrick   this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second);
1197e5dd7070Spatrick   return true;
1198e5dd7070Spatrick }
1199e5dd7070Spatrick 
diagReturnOnAllocFailure(Sema & S,Expr * E,CXXRecordDecl * PromiseRecordDecl,FunctionScopeInfo & Fn)1200e5dd7070Spatrick static bool diagReturnOnAllocFailure(Sema &S, Expr *E,
1201e5dd7070Spatrick                                      CXXRecordDecl *PromiseRecordDecl,
1202e5dd7070Spatrick                                      FunctionScopeInfo &Fn) {
1203e5dd7070Spatrick   auto Loc = E->getExprLoc();
1204e5dd7070Spatrick   if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) {
1205e5dd7070Spatrick     auto *Decl = DeclRef->getDecl();
1206e5dd7070Spatrick     if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) {
1207e5dd7070Spatrick       if (Method->isStatic())
1208e5dd7070Spatrick         return true;
1209e5dd7070Spatrick       else
1210e5dd7070Spatrick         Loc = Decl->getLocation();
1211e5dd7070Spatrick     }
1212e5dd7070Spatrick   }
1213e5dd7070Spatrick 
1214e5dd7070Spatrick   S.Diag(
1215e5dd7070Spatrick       Loc,
1216e5dd7070Spatrick       diag::err_coroutine_promise_get_return_object_on_allocation_failure)
1217e5dd7070Spatrick       << PromiseRecordDecl;
1218e5dd7070Spatrick   S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1219e5dd7070Spatrick       << Fn.getFirstCoroutineStmtKeyword();
1220e5dd7070Spatrick   return false;
1221e5dd7070Spatrick }
1222e5dd7070Spatrick 
makeReturnOnAllocFailure()1223e5dd7070Spatrick bool CoroutineStmtBuilder::makeReturnOnAllocFailure() {
1224e5dd7070Spatrick   assert(!IsPromiseDependentType &&
1225e5dd7070Spatrick          "cannot make statement while the promise type is dependent");
1226e5dd7070Spatrick 
1227*12c85518Srobert   // [dcl.fct.def.coroutine]p10
1228*12c85518Srobert   //   If a search for the name get_return_object_on_allocation_failure in
1229*12c85518Srobert   // the scope of the promise type ([class.member.lookup]) finds any
1230*12c85518Srobert   // declarations, then the result of a call to an allocation function used to
1231*12c85518Srobert   // obtain storage for the coroutine state is assumed to return nullptr if it
1232*12c85518Srobert   // fails to obtain storage, ... If the allocation function returns nullptr,
1233*12c85518Srobert   // ... and the return value is obtained by a call to
1234*12c85518Srobert   // T::get_return_object_on_allocation_failure(), where T is the
1235*12c85518Srobert   // promise type.
1236e5dd7070Spatrick   DeclarationName DN =
1237e5dd7070Spatrick       S.PP.getIdentifierInfo("get_return_object_on_allocation_failure");
1238e5dd7070Spatrick   LookupResult Found(S, DN, Loc, Sema::LookupMemberName);
1239e5dd7070Spatrick   if (!S.LookupQualifiedName(Found, PromiseRecordDecl))
1240e5dd7070Spatrick     return true;
1241e5dd7070Spatrick 
1242e5dd7070Spatrick   CXXScopeSpec SS;
1243e5dd7070Spatrick   ExprResult DeclNameExpr =
1244e5dd7070Spatrick       S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
1245e5dd7070Spatrick   if (DeclNameExpr.isInvalid())
1246e5dd7070Spatrick     return false;
1247e5dd7070Spatrick 
1248e5dd7070Spatrick   if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn))
1249e5dd7070Spatrick     return false;
1250e5dd7070Spatrick 
1251e5dd7070Spatrick   ExprResult ReturnObjectOnAllocationFailure =
1252e5dd7070Spatrick       S.BuildCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc);
1253e5dd7070Spatrick   if (ReturnObjectOnAllocationFailure.isInvalid())
1254e5dd7070Spatrick     return false;
1255e5dd7070Spatrick 
1256e5dd7070Spatrick   StmtResult ReturnStmt =
1257e5dd7070Spatrick       S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get());
1258e5dd7070Spatrick   if (ReturnStmt.isInvalid()) {
1259e5dd7070Spatrick     S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here)
1260e5dd7070Spatrick         << DN;
1261e5dd7070Spatrick     S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1262e5dd7070Spatrick         << Fn.getFirstCoroutineStmtKeyword();
1263e5dd7070Spatrick     return false;
1264e5dd7070Spatrick   }
1265e5dd7070Spatrick 
1266e5dd7070Spatrick   this->ReturnStmtOnAllocFailure = ReturnStmt.get();
1267e5dd7070Spatrick   return true;
1268e5dd7070Spatrick }
1269e5dd7070Spatrick 
1270*12c85518Srobert // Collect placement arguments for allocation function of coroutine FD.
1271*12c85518Srobert // Return true if we collect placement arguments succesfully. Return false,
1272*12c85518Srobert // otherwise.
collectPlacementArgs(Sema & S,FunctionDecl & FD,SourceLocation Loc,SmallVectorImpl<Expr * > & PlacementArgs)1273*12c85518Srobert static bool collectPlacementArgs(Sema &S, FunctionDecl &FD, SourceLocation Loc,
1274*12c85518Srobert                                  SmallVectorImpl<Expr *> &PlacementArgs) {
1275e5dd7070Spatrick   if (auto *MD = dyn_cast<CXXMethodDecl>(&FD)) {
1276e5dd7070Spatrick     if (MD->isInstance() && !isLambdaCallOperator(MD)) {
1277e5dd7070Spatrick       ExprResult ThisExpr = S.ActOnCXXThis(Loc);
1278e5dd7070Spatrick       if (ThisExpr.isInvalid())
1279e5dd7070Spatrick         return false;
1280e5dd7070Spatrick       ThisExpr = S.CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get());
1281e5dd7070Spatrick       if (ThisExpr.isInvalid())
1282e5dd7070Spatrick         return false;
1283e5dd7070Spatrick       PlacementArgs.push_back(ThisExpr.get());
1284e5dd7070Spatrick     }
1285e5dd7070Spatrick   }
1286*12c85518Srobert 
1287e5dd7070Spatrick   for (auto *PD : FD.parameters()) {
1288e5dd7070Spatrick     if (PD->getType()->isDependentType())
1289e5dd7070Spatrick       continue;
1290e5dd7070Spatrick 
1291e5dd7070Spatrick     // Build a reference to the parameter.
1292e5dd7070Spatrick     auto PDLoc = PD->getLocation();
1293e5dd7070Spatrick     ExprResult PDRefExpr =
1294e5dd7070Spatrick         S.BuildDeclRefExpr(PD, PD->getOriginalType().getNonReferenceType(),
1295e5dd7070Spatrick                            ExprValueKind::VK_LValue, PDLoc);
1296e5dd7070Spatrick     if (PDRefExpr.isInvalid())
1297e5dd7070Spatrick       return false;
1298e5dd7070Spatrick 
1299e5dd7070Spatrick     PlacementArgs.push_back(PDRefExpr.get());
1300e5dd7070Spatrick   }
1301e5dd7070Spatrick 
1302*12c85518Srobert   return true;
1303e5dd7070Spatrick }
1304e5dd7070Spatrick 
makeNewAndDeleteExpr()1305*12c85518Srobert bool CoroutineStmtBuilder::makeNewAndDeleteExpr() {
1306*12c85518Srobert   // Form and check allocation and deallocation calls.
1307*12c85518Srobert   assert(!IsPromiseDependentType &&
1308*12c85518Srobert          "cannot make statement while the promise type is dependent");
1309*12c85518Srobert   QualType PromiseType = Fn.CoroutinePromise->getType();
1310*12c85518Srobert 
1311*12c85518Srobert   if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type))
1312*12c85518Srobert     return false;
1313*12c85518Srobert 
1314*12c85518Srobert   const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr;
1315*12c85518Srobert 
1316*12c85518Srobert   // According to [dcl.fct.def.coroutine]p9, Lookup allocation functions using a
1317*12c85518Srobert   // parameter list composed of the requested size of the coroutine state being
1318*12c85518Srobert   // allocated, followed by the coroutine function's arguments. If a matching
1319*12c85518Srobert   // allocation function exists, use it. Otherwise, use an allocation function
1320*12c85518Srobert   // that just takes the requested size.
1321*12c85518Srobert   //
1322*12c85518Srobert   // [dcl.fct.def.coroutine]p9
1323*12c85518Srobert   //   An implementation may need to allocate additional storage for a
1324*12c85518Srobert   //   coroutine.
1325*12c85518Srobert   // This storage is known as the coroutine state and is obtained by calling a
1326*12c85518Srobert   // non-array allocation function ([basic.stc.dynamic.allocation]). The
1327*12c85518Srobert   // allocation function's name is looked up by searching for it in the scope of
1328*12c85518Srobert   // the promise type.
1329*12c85518Srobert   // - If any declarations are found, overload resolution is performed on a
1330*12c85518Srobert   // function call created by assembling an argument list. The first argument is
1331*12c85518Srobert   // the amount of space requested, and has type std::size_t. The
1332*12c85518Srobert   // lvalues p1 ... pn are the succeeding arguments.
1333*12c85518Srobert   //
1334*12c85518Srobert   // ...where "p1 ... pn" are defined earlier as:
1335*12c85518Srobert   //
1336*12c85518Srobert   // [dcl.fct.def.coroutine]p3
1337*12c85518Srobert   //   The promise type of a coroutine is `std::coroutine_traits<R, P1, ...,
1338*12c85518Srobert   //   Pn>`
1339*12c85518Srobert   // , where R is the return type of the function, and `P1, ..., Pn` are the
1340*12c85518Srobert   // sequence of types of the non-object function parameters, preceded by the
1341*12c85518Srobert   // type of the object parameter ([dcl.fct]) if the coroutine is a non-static
1342*12c85518Srobert   // member function. [dcl.fct.def.coroutine]p4 In the following, p_i is an
1343*12c85518Srobert   // lvalue of type P_i, where p1 denotes the object parameter and p_i+1 denotes
1344*12c85518Srobert   // the i-th non-object function parameter for a non-static member function,
1345*12c85518Srobert   // and p_i denotes the i-th function parameter otherwise. For a non-static
1346*12c85518Srobert   // member function, q_1 is an lvalue that denotes *this; any other q_i is an
1347*12c85518Srobert   // lvalue that denotes the parameter copy corresponding to p_i.
1348*12c85518Srobert 
1349*12c85518Srobert   FunctionDecl *OperatorNew = nullptr;
1350*12c85518Srobert   SmallVector<Expr *, 1> PlacementArgs;
1351*12c85518Srobert 
1352*12c85518Srobert   const bool PromiseContainsNew = [this, &PromiseType]() -> bool {
1353*12c85518Srobert     DeclarationName NewName =
1354*12c85518Srobert         S.getASTContext().DeclarationNames.getCXXOperatorName(OO_New);
1355*12c85518Srobert     LookupResult R(S, NewName, Loc, Sema::LookupOrdinaryName);
1356*12c85518Srobert 
1357*12c85518Srobert     if (PromiseType->isRecordType())
1358*12c85518Srobert       S.LookupQualifiedName(R, PromiseType->getAsCXXRecordDecl());
1359*12c85518Srobert 
1360*12c85518Srobert     return !R.empty() && !R.isAmbiguous();
1361*12c85518Srobert   }();
1362*12c85518Srobert 
1363*12c85518Srobert   // Helper function to indicate whether the last lookup found the aligned
1364*12c85518Srobert   // allocation function.
1365*12c85518Srobert   bool PassAlignment = S.getLangOpts().CoroAlignedAllocation;
1366*12c85518Srobert   auto LookupAllocationFunction = [&](Sema::AllocationFunctionScope NewScope =
1367*12c85518Srobert                                           Sema::AFS_Both,
1368*12c85518Srobert                                       bool WithoutPlacementArgs = false,
1369*12c85518Srobert                                       bool ForceNonAligned = false) {
1370*12c85518Srobert     // [dcl.fct.def.coroutine]p9
1371*12c85518Srobert     //   The allocation function's name is looked up by searching for it in the
1372*12c85518Srobert     // scope of the promise type.
1373*12c85518Srobert     // - If any declarations are found, ...
1374*12c85518Srobert     // - If no declarations are found in the scope of the promise type, a search
1375*12c85518Srobert     // is performed in the global scope.
1376*12c85518Srobert     if (NewScope == Sema::AFS_Both)
1377*12c85518Srobert       NewScope = PromiseContainsNew ? Sema::AFS_Class : Sema::AFS_Global;
1378*12c85518Srobert 
1379*12c85518Srobert     PassAlignment = !ForceNonAligned && S.getLangOpts().CoroAlignedAllocation;
1380*12c85518Srobert     FunctionDecl *UnusedResult = nullptr;
1381*12c85518Srobert     S.FindAllocationFunctions(Loc, SourceRange(), NewScope,
1382e5dd7070Spatrick                               /*DeleteScope*/ Sema::AFS_Both, PromiseType,
1383*12c85518Srobert                               /*isArray*/ false, PassAlignment,
1384*12c85518Srobert                               WithoutPlacementArgs ? MultiExprArg{}
1385*12c85518Srobert                                                    : PlacementArgs,
1386*12c85518Srobert                               OperatorNew, UnusedResult, /*Diagnose*/ false);
1387*12c85518Srobert   };
1388*12c85518Srobert 
1389*12c85518Srobert   // We don't expect to call to global operator new with (size, p0, …, pn).
1390*12c85518Srobert   // So if we choose to lookup the allocation function in global scope, we
1391*12c85518Srobert   // shouldn't lookup placement arguments.
1392*12c85518Srobert   if (PromiseContainsNew && !collectPlacementArgs(S, FD, Loc, PlacementArgs))
1393*12c85518Srobert     return false;
1394*12c85518Srobert 
1395*12c85518Srobert   LookupAllocationFunction();
1396*12c85518Srobert 
1397*12c85518Srobert   if (PromiseContainsNew && !PlacementArgs.empty()) {
1398*12c85518Srobert     // [dcl.fct.def.coroutine]p9
1399*12c85518Srobert     //   If no viable function is found ([over.match.viable]), overload
1400*12c85518Srobert     //   resolution
1401*12c85518Srobert     // is performed again on a function call created by passing just the amount
1402*12c85518Srobert     // of space required as an argument of type std::size_t.
1403*12c85518Srobert     //
1404*12c85518Srobert     // Proposed Change of [dcl.fct.def.coroutine]p9 in P2014R0:
1405*12c85518Srobert     //   Otherwise, overload resolution is performed again on a function call
1406*12c85518Srobert     //   created
1407*12c85518Srobert     // by passing the amount of space requested as an argument of type
1408*12c85518Srobert     // std::size_t as the first argument, and the requested alignment as
1409*12c85518Srobert     // an argument of type std:align_val_t as the second argument.
1410*12c85518Srobert     if (!OperatorNew ||
1411*12c85518Srobert         (S.getLangOpts().CoroAlignedAllocation && !PassAlignment))
1412*12c85518Srobert       LookupAllocationFunction(/*NewScope*/ Sema::AFS_Class,
1413*12c85518Srobert                                /*WithoutPlacementArgs*/ true);
1414*12c85518Srobert   }
1415*12c85518Srobert 
1416*12c85518Srobert   // Proposed Change of [dcl.fct.def.coroutine]p12 in P2014R0:
1417*12c85518Srobert   //   Otherwise, overload resolution is performed again on a function call
1418*12c85518Srobert   //   created
1419*12c85518Srobert   // by passing the amount of space requested as an argument of type
1420*12c85518Srobert   // std::size_t as the first argument, and the lvalues p1 ... pn as the
1421*12c85518Srobert   // succeeding arguments. Otherwise, overload resolution is performed again
1422*12c85518Srobert   // on a function call created by passing just the amount of space required as
1423*12c85518Srobert   // an argument of type std::size_t.
1424*12c85518Srobert   //
1425*12c85518Srobert   // So within the proposed change in P2014RO, the priority order of aligned
1426*12c85518Srobert   // allocation functions wiht promise_type is:
1427*12c85518Srobert   //
1428*12c85518Srobert   //    void* operator new( std::size_t, std::align_val_t, placement_args... );
1429*12c85518Srobert   //    void* operator new( std::size_t, std::align_val_t);
1430*12c85518Srobert   //    void* operator new( std::size_t, placement_args... );
1431*12c85518Srobert   //    void* operator new( std::size_t);
1432*12c85518Srobert 
1433*12c85518Srobert   // Helper variable to emit warnings.
1434*12c85518Srobert   bool FoundNonAlignedInPromise = false;
1435*12c85518Srobert   if (PromiseContainsNew && S.getLangOpts().CoroAlignedAllocation)
1436*12c85518Srobert     if (!OperatorNew || !PassAlignment) {
1437*12c85518Srobert       FoundNonAlignedInPromise = OperatorNew;
1438*12c85518Srobert 
1439*12c85518Srobert       LookupAllocationFunction(/*NewScope*/ Sema::AFS_Class,
1440*12c85518Srobert                                /*WithoutPlacementArgs*/ false,
1441*12c85518Srobert                                /*ForceNonAligned*/ true);
1442*12c85518Srobert 
1443*12c85518Srobert       if (!OperatorNew && !PlacementArgs.empty())
1444*12c85518Srobert         LookupAllocationFunction(/*NewScope*/ Sema::AFS_Class,
1445*12c85518Srobert                                  /*WithoutPlacementArgs*/ true,
1446*12c85518Srobert                                  /*ForceNonAligned*/ true);
1447e5dd7070Spatrick     }
1448e5dd7070Spatrick 
1449e5dd7070Spatrick   bool IsGlobalOverload =
1450e5dd7070Spatrick       OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext());
1451e5dd7070Spatrick   // If we didn't find a class-local new declaration and non-throwing new
1452e5dd7070Spatrick   // was is required then we need to lookup the non-throwing global operator
1453e5dd7070Spatrick   // instead.
1454e5dd7070Spatrick   if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) {
1455e5dd7070Spatrick     auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc);
1456e5dd7070Spatrick     if (!StdNoThrow)
1457e5dd7070Spatrick       return false;
1458e5dd7070Spatrick     PlacementArgs = {StdNoThrow};
1459e5dd7070Spatrick     OperatorNew = nullptr;
1460*12c85518Srobert     LookupAllocationFunction(Sema::AFS_Global);
1461e5dd7070Spatrick   }
1462e5dd7070Spatrick 
1463*12c85518Srobert   // If we found a non-aligned allocation function in the promise_type,
1464*12c85518Srobert   // it indicates the user forgot to update the allocation function. Let's emit
1465*12c85518Srobert   // a warning here.
1466*12c85518Srobert   if (FoundNonAlignedInPromise) {
1467*12c85518Srobert     S.Diag(OperatorNew->getLocation(),
1468*12c85518Srobert            diag::warn_non_aligned_allocation_function)
1469*12c85518Srobert         << &FD;
1470*12c85518Srobert   }
1471*12c85518Srobert 
1472*12c85518Srobert   if (!OperatorNew) {
1473*12c85518Srobert     if (PromiseContainsNew)
1474*12c85518Srobert       S.Diag(Loc, diag::err_coroutine_unusable_new) << PromiseType << &FD;
1475*12c85518Srobert     else if (RequiresNoThrowAlloc)
1476*12c85518Srobert       S.Diag(Loc, diag::err_coroutine_unfound_nothrow_new)
1477*12c85518Srobert           << &FD << S.getLangOpts().CoroAlignedAllocation;
1478*12c85518Srobert 
1479e5dd7070Spatrick     return false;
1480*12c85518Srobert   }
1481e5dd7070Spatrick 
1482e5dd7070Spatrick   if (RequiresNoThrowAlloc) {
1483e5dd7070Spatrick     const auto *FT = OperatorNew->getType()->castAs<FunctionProtoType>();
1484e5dd7070Spatrick     if (!FT->isNothrow(/*ResultIfDependent*/ false)) {
1485e5dd7070Spatrick       S.Diag(OperatorNew->getLocation(),
1486e5dd7070Spatrick              diag::err_coroutine_promise_new_requires_nothrow)
1487e5dd7070Spatrick           << OperatorNew;
1488e5dd7070Spatrick       S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
1489e5dd7070Spatrick           << OperatorNew;
1490e5dd7070Spatrick       return false;
1491e5dd7070Spatrick     }
1492e5dd7070Spatrick   }
1493e5dd7070Spatrick 
1494*12c85518Srobert   FunctionDecl *OperatorDelete = nullptr;
1495*12c85518Srobert   if (!findDeleteForPromise(S, Loc, PromiseType, OperatorDelete)) {
1496*12c85518Srobert     // FIXME: We should add an error here. According to:
1497*12c85518Srobert     // [dcl.fct.def.coroutine]p12
1498*12c85518Srobert     //   If no usual deallocation function is found, the program is ill-formed.
1499e5dd7070Spatrick     return false;
1500*12c85518Srobert   }
1501e5dd7070Spatrick 
1502e5dd7070Spatrick   Expr *FramePtr =
1503a9ac8606Spatrick       S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_frame, {});
1504e5dd7070Spatrick 
1505e5dd7070Spatrick   Expr *FrameSize =
1506a9ac8606Spatrick       S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_size, {});
1507e5dd7070Spatrick 
1508*12c85518Srobert   Expr *FrameAlignment = nullptr;
1509e5dd7070Spatrick 
1510*12c85518Srobert   if (S.getLangOpts().CoroAlignedAllocation) {
1511*12c85518Srobert     FrameAlignment =
1512*12c85518Srobert         S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_align, {});
1513*12c85518Srobert 
1514*12c85518Srobert     TypeSourceInfo *AlignValTy = getTypeSourceInfoForStdAlignValT(S, Loc);
1515*12c85518Srobert     if (!AlignValTy)
1516*12c85518Srobert       return false;
1517*12c85518Srobert 
1518*12c85518Srobert     FrameAlignment = S.BuildCXXNamedCast(Loc, tok::kw_static_cast, AlignValTy,
1519*12c85518Srobert                                          FrameAlignment, SourceRange(Loc, Loc),
1520*12c85518Srobert                                          SourceRange(Loc, Loc))
1521*12c85518Srobert                          .get();
1522*12c85518Srobert   }
1523*12c85518Srobert 
1524*12c85518Srobert   // Make new call.
1525e5dd7070Spatrick   ExprResult NewRef =
1526e5dd7070Spatrick       S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc);
1527e5dd7070Spatrick   if (NewRef.isInvalid())
1528e5dd7070Spatrick     return false;
1529e5dd7070Spatrick 
1530e5dd7070Spatrick   SmallVector<Expr *, 2> NewArgs(1, FrameSize);
1531*12c85518Srobert   if (S.getLangOpts().CoroAlignedAllocation && PassAlignment)
1532*12c85518Srobert     NewArgs.push_back(FrameAlignment);
1533*12c85518Srobert 
1534*12c85518Srobert   if (OperatorNew->getNumParams() > NewArgs.size())
1535*12c85518Srobert     llvm::append_range(NewArgs, PlacementArgs);
1536e5dd7070Spatrick 
1537e5dd7070Spatrick   ExprResult NewExpr =
1538e5dd7070Spatrick       S.BuildCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc);
1539e5dd7070Spatrick   NewExpr = S.ActOnFinishFullExpr(NewExpr.get(), /*DiscardedValue*/ false);
1540e5dd7070Spatrick   if (NewExpr.isInvalid())
1541e5dd7070Spatrick     return false;
1542e5dd7070Spatrick 
1543e5dd7070Spatrick   // Make delete call.
1544e5dd7070Spatrick 
1545e5dd7070Spatrick   QualType OpDeleteQualType = OperatorDelete->getType();
1546e5dd7070Spatrick 
1547e5dd7070Spatrick   ExprResult DeleteRef =
1548e5dd7070Spatrick       S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc);
1549e5dd7070Spatrick   if (DeleteRef.isInvalid())
1550e5dd7070Spatrick     return false;
1551e5dd7070Spatrick 
1552e5dd7070Spatrick   Expr *CoroFree =
1553a9ac8606Spatrick       S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_free, {FramePtr});
1554e5dd7070Spatrick 
1555e5dd7070Spatrick   SmallVector<Expr *, 2> DeleteArgs{CoroFree};
1556e5dd7070Spatrick 
1557*12c85518Srobert   // [dcl.fct.def.coroutine]p12
1558*12c85518Srobert   //   The selected deallocation function shall be called with the address of
1559*12c85518Srobert   //   the block of storage to be reclaimed as its first argument. If a
1560*12c85518Srobert   //   deallocation function with a parameter of type std::size_t is
1561*12c85518Srobert   //   used, the size of the block is passed as the corresponding argument.
1562e5dd7070Spatrick   const auto *OpDeleteType =
1563e5dd7070Spatrick       OpDeleteQualType.getTypePtr()->castAs<FunctionProtoType>();
1564*12c85518Srobert   if (OpDeleteType->getNumParams() > DeleteArgs.size() &&
1565*12c85518Srobert       S.getASTContext().hasSameUnqualifiedType(
1566*12c85518Srobert           OpDeleteType->getParamType(DeleteArgs.size()), FrameSize->getType()))
1567e5dd7070Spatrick     DeleteArgs.push_back(FrameSize);
1568e5dd7070Spatrick 
1569*12c85518Srobert   // Proposed Change of [dcl.fct.def.coroutine]p12 in P2014R0:
1570*12c85518Srobert   //   If deallocation function lookup finds a usual deallocation function with
1571*12c85518Srobert   //   a pointer parameter, size parameter and alignment parameter then this
1572*12c85518Srobert   //   will be the selected deallocation function, otherwise if lookup finds a
1573*12c85518Srobert   //   usual deallocation function with both a pointer parameter and a size
1574*12c85518Srobert   //   parameter, then this will be the selected deallocation function.
1575*12c85518Srobert   //   Otherwise, if lookup finds a usual deallocation function with only a
1576*12c85518Srobert   //   pointer parameter, then this will be the selected deallocation
1577*12c85518Srobert   //   function.
1578*12c85518Srobert   //
1579*12c85518Srobert   // So we are not forced to pass alignment to the deallocation function.
1580*12c85518Srobert   if (S.getLangOpts().CoroAlignedAllocation &&
1581*12c85518Srobert       OpDeleteType->getNumParams() > DeleteArgs.size() &&
1582*12c85518Srobert       S.getASTContext().hasSameUnqualifiedType(
1583*12c85518Srobert           OpDeleteType->getParamType(DeleteArgs.size()),
1584*12c85518Srobert           FrameAlignment->getType()))
1585*12c85518Srobert     DeleteArgs.push_back(FrameAlignment);
1586*12c85518Srobert 
1587e5dd7070Spatrick   ExprResult DeleteExpr =
1588e5dd7070Spatrick       S.BuildCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc);
1589e5dd7070Spatrick   DeleteExpr =
1590e5dd7070Spatrick       S.ActOnFinishFullExpr(DeleteExpr.get(), /*DiscardedValue*/ false);
1591e5dd7070Spatrick   if (DeleteExpr.isInvalid())
1592e5dd7070Spatrick     return false;
1593e5dd7070Spatrick 
1594e5dd7070Spatrick   this->Allocate = NewExpr.get();
1595e5dd7070Spatrick   this->Deallocate = DeleteExpr.get();
1596e5dd7070Spatrick 
1597e5dd7070Spatrick   return true;
1598e5dd7070Spatrick }
1599e5dd7070Spatrick 
makeOnFallthrough()1600e5dd7070Spatrick bool CoroutineStmtBuilder::makeOnFallthrough() {
1601e5dd7070Spatrick   assert(!IsPromiseDependentType &&
1602e5dd7070Spatrick          "cannot make statement while the promise type is dependent");
1603e5dd7070Spatrick 
1604*12c85518Srobert   // [dcl.fct.def.coroutine]/p6
1605*12c85518Srobert   // If searches for the names return_void and return_value in the scope of
1606*12c85518Srobert   // the promise type each find any declarations, the program is ill-formed.
1607*12c85518Srobert   // [Note 1: If return_void is found, flowing off the end of a coroutine is
1608*12c85518Srobert   // equivalent to a co_return with no operand. Otherwise, flowing off the end
1609*12c85518Srobert   // of a coroutine results in undefined behavior ([stmt.return.coroutine]). —
1610*12c85518Srobert   // end note]
1611e5dd7070Spatrick   bool HasRVoid, HasRValue;
1612e5dd7070Spatrick   LookupResult LRVoid =
1613e5dd7070Spatrick       lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid);
1614e5dd7070Spatrick   LookupResult LRValue =
1615e5dd7070Spatrick       lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue);
1616e5dd7070Spatrick 
1617e5dd7070Spatrick   StmtResult Fallthrough;
1618e5dd7070Spatrick   if (HasRVoid && HasRValue) {
1619e5dd7070Spatrick     // FIXME Improve this diagnostic
1620e5dd7070Spatrick     S.Diag(FD.getLocation(),
1621e5dd7070Spatrick            diag::err_coroutine_promise_incompatible_return_functions)
1622e5dd7070Spatrick         << PromiseRecordDecl;
1623e5dd7070Spatrick     S.Diag(LRVoid.getRepresentativeDecl()->getLocation(),
1624e5dd7070Spatrick            diag::note_member_first_declared_here)
1625e5dd7070Spatrick         << LRVoid.getLookupName();
1626e5dd7070Spatrick     S.Diag(LRValue.getRepresentativeDecl()->getLocation(),
1627e5dd7070Spatrick            diag::note_member_first_declared_here)
1628e5dd7070Spatrick         << LRValue.getLookupName();
1629e5dd7070Spatrick     return false;
1630e5dd7070Spatrick   } else if (!HasRVoid && !HasRValue) {
1631*12c85518Srobert     // We need to set 'Fallthrough'. Otherwise the other analysis part might
1632*12c85518Srobert     // think the coroutine has defined a return_value method. So it might emit
1633*12c85518Srobert     // **false** positive warning. e.g.,
1634*12c85518Srobert     //
1635*12c85518Srobert     //    promise_without_return_func foo() {
1636*12c85518Srobert     //        co_await something();
1637*12c85518Srobert     //    }
1638*12c85518Srobert     //
1639*12c85518Srobert     // Then AnalysisBasedWarning would emit a warning about `foo()` lacking a
1640*12c85518Srobert     // co_return statements, which isn't correct.
1641*12c85518Srobert     Fallthrough = S.ActOnNullStmt(PromiseRecordDecl->getLocation());
1642*12c85518Srobert     if (Fallthrough.isInvalid())
1643e5dd7070Spatrick       return false;
1644e5dd7070Spatrick   } else if (HasRVoid) {
1645e5dd7070Spatrick     Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr,
1646e5dd7070Spatrick                                       /*IsImplicit*/false);
1647e5dd7070Spatrick     Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get());
1648e5dd7070Spatrick     if (Fallthrough.isInvalid())
1649e5dd7070Spatrick       return false;
1650e5dd7070Spatrick   }
1651e5dd7070Spatrick 
1652e5dd7070Spatrick   this->OnFallthrough = Fallthrough.get();
1653e5dd7070Spatrick   return true;
1654e5dd7070Spatrick }
1655e5dd7070Spatrick 
makeOnException()1656e5dd7070Spatrick bool CoroutineStmtBuilder::makeOnException() {
1657e5dd7070Spatrick   // Try to form 'p.unhandled_exception();'
1658e5dd7070Spatrick   assert(!IsPromiseDependentType &&
1659e5dd7070Spatrick          "cannot make statement while the promise type is dependent");
1660e5dd7070Spatrick 
1661e5dd7070Spatrick   const bool RequireUnhandledException = S.getLangOpts().CXXExceptions;
1662e5dd7070Spatrick 
1663e5dd7070Spatrick   if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) {
1664e5dd7070Spatrick     auto DiagID =
1665e5dd7070Spatrick         RequireUnhandledException
1666e5dd7070Spatrick             ? diag::err_coroutine_promise_unhandled_exception_required
1667e5dd7070Spatrick             : diag::
1668e5dd7070Spatrick                   warn_coroutine_promise_unhandled_exception_required_with_exceptions;
1669e5dd7070Spatrick     S.Diag(Loc, DiagID) << PromiseRecordDecl;
1670e5dd7070Spatrick     S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
1671e5dd7070Spatrick         << PromiseRecordDecl;
1672e5dd7070Spatrick     return !RequireUnhandledException;
1673e5dd7070Spatrick   }
1674e5dd7070Spatrick 
1675e5dd7070Spatrick   // If exceptions are disabled, don't try to build OnException.
1676e5dd7070Spatrick   if (!S.getLangOpts().CXXExceptions)
1677e5dd7070Spatrick     return true;
1678e5dd7070Spatrick 
1679*12c85518Srobert   ExprResult UnhandledException = buildPromiseCall(
1680*12c85518Srobert       S, Fn.CoroutinePromise, Loc, "unhandled_exception", std::nullopt);
1681e5dd7070Spatrick   UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc,
1682e5dd7070Spatrick                                              /*DiscardedValue*/ false);
1683e5dd7070Spatrick   if (UnhandledException.isInvalid())
1684e5dd7070Spatrick     return false;
1685e5dd7070Spatrick 
1686e5dd7070Spatrick   // Since the body of the coroutine will be wrapped in try-catch, it will
1687e5dd7070Spatrick   // be incompatible with SEH __try if present in a function.
1688e5dd7070Spatrick   if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) {
1689e5dd7070Spatrick     S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions);
1690e5dd7070Spatrick     S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1691e5dd7070Spatrick         << Fn.getFirstCoroutineStmtKeyword();
1692e5dd7070Spatrick     return false;
1693e5dd7070Spatrick   }
1694e5dd7070Spatrick 
1695e5dd7070Spatrick   this->OnException = UnhandledException.get();
1696e5dd7070Spatrick   return true;
1697e5dd7070Spatrick }
1698e5dd7070Spatrick 
makeReturnObject()1699e5dd7070Spatrick bool CoroutineStmtBuilder::makeReturnObject() {
1700*12c85518Srobert   // [dcl.fct.def.coroutine]p7
1701*12c85518Srobert   // The expression promise.get_return_object() is used to initialize the
1702*12c85518Srobert   // returned reference or prvalue result object of a call to a coroutine.
1703*12c85518Srobert   ExprResult ReturnObject = buildPromiseCall(S, Fn.CoroutinePromise, Loc,
1704*12c85518Srobert                                              "get_return_object", std::nullopt);
1705e5dd7070Spatrick   if (ReturnObject.isInvalid())
1706e5dd7070Spatrick     return false;
1707e5dd7070Spatrick 
1708e5dd7070Spatrick   this->ReturnValue = ReturnObject.get();
1709e5dd7070Spatrick   return true;
1710e5dd7070Spatrick }
1711e5dd7070Spatrick 
noteMemberDeclaredHere(Sema & S,Expr * E,FunctionScopeInfo & Fn)1712e5dd7070Spatrick static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) {
1713e5dd7070Spatrick   if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) {
1714e5dd7070Spatrick     auto *MethodDecl = MbrRef->getMethodDecl();
1715e5dd7070Spatrick     S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here)
1716e5dd7070Spatrick         << MethodDecl;
1717e5dd7070Spatrick   }
1718e5dd7070Spatrick   S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1719e5dd7070Spatrick       << Fn.getFirstCoroutineStmtKeyword();
1720e5dd7070Spatrick }
1721e5dd7070Spatrick 
makeGroDeclAndReturnStmt()1722e5dd7070Spatrick bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
1723e5dd7070Spatrick   assert(!IsPromiseDependentType &&
1724e5dd7070Spatrick          "cannot make statement while the promise type is dependent");
1725e5dd7070Spatrick   assert(this->ReturnValue && "ReturnValue must be already formed");
1726e5dd7070Spatrick 
1727e5dd7070Spatrick   QualType const GroType = this->ReturnValue->getType();
1728e5dd7070Spatrick   assert(!GroType->isDependentType() &&
1729e5dd7070Spatrick          "get_return_object type must no longer be dependent");
1730e5dd7070Spatrick 
1731e5dd7070Spatrick   QualType const FnRetType = FD.getReturnType();
1732e5dd7070Spatrick   assert(!FnRetType->isDependentType() &&
1733e5dd7070Spatrick          "get_return_object type must no longer be dependent");
1734e5dd7070Spatrick 
1735e5dd7070Spatrick   if (FnRetType->isVoidType()) {
1736e5dd7070Spatrick     ExprResult Res =
1737e5dd7070Spatrick         S.ActOnFinishFullExpr(this->ReturnValue, Loc, /*DiscardedValue*/ false);
1738e5dd7070Spatrick     if (Res.isInvalid())
1739e5dd7070Spatrick       return false;
1740e5dd7070Spatrick 
1741e5dd7070Spatrick     return true;
1742e5dd7070Spatrick   }
1743e5dd7070Spatrick 
1744e5dd7070Spatrick   if (GroType->isVoidType()) {
1745e5dd7070Spatrick     // Trigger a nice error message.
1746e5dd7070Spatrick     InitializedEntity Entity =
1747a9ac8606Spatrick         InitializedEntity::InitializeResult(Loc, FnRetType);
1748a9ac8606Spatrick     S.PerformCopyInitialization(Entity, SourceLocation(), ReturnValue);
1749e5dd7070Spatrick     noteMemberDeclaredHere(S, ReturnValue, Fn);
1750e5dd7070Spatrick     return false;
1751e5dd7070Spatrick   }
1752e5dd7070Spatrick 
1753*12c85518Srobert   StmtResult ReturnStmt = S.BuildReturnStmt(Loc, ReturnValue);
1754e5dd7070Spatrick   if (ReturnStmt.isInvalid()) {
1755e5dd7070Spatrick     noteMemberDeclaredHere(S, ReturnValue, Fn);
1756e5dd7070Spatrick     return false;
1757e5dd7070Spatrick   }
1758e5dd7070Spatrick 
1759e5dd7070Spatrick   this->ReturnStmt = ReturnStmt.get();
1760e5dd7070Spatrick   return true;
1761e5dd7070Spatrick }
1762e5dd7070Spatrick 
1763e5dd7070Spatrick // Create a static_cast\<T&&>(expr).
castForMoving(Sema & S,Expr * E,QualType T=QualType ())1764e5dd7070Spatrick static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) {
1765e5dd7070Spatrick   if (T.isNull())
1766e5dd7070Spatrick     T = E->getType();
1767e5dd7070Spatrick   QualType TargetType = S.BuildReferenceType(
1768e5dd7070Spatrick       T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName());
1769e5dd7070Spatrick   SourceLocation ExprLoc = E->getBeginLoc();
1770e5dd7070Spatrick   TypeSourceInfo *TargetLoc =
1771e5dd7070Spatrick       S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc);
1772e5dd7070Spatrick 
1773e5dd7070Spatrick   return S
1774e5dd7070Spatrick       .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E,
1775e5dd7070Spatrick                          SourceRange(ExprLoc, ExprLoc), E->getSourceRange())
1776e5dd7070Spatrick       .get();
1777e5dd7070Spatrick }
1778e5dd7070Spatrick 
1779e5dd7070Spatrick /// Build a variable declaration for move parameter.
buildVarDecl(Sema & S,SourceLocation Loc,QualType Type,IdentifierInfo * II)1780e5dd7070Spatrick static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type,
1781e5dd7070Spatrick                              IdentifierInfo *II) {
1782e5dd7070Spatrick   TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc);
1783e5dd7070Spatrick   VarDecl *Decl = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type,
1784e5dd7070Spatrick                                   TInfo, SC_None);
1785e5dd7070Spatrick   Decl->setImplicit();
1786e5dd7070Spatrick   return Decl;
1787e5dd7070Spatrick }
1788e5dd7070Spatrick 
1789e5dd7070Spatrick // Build statements that move coroutine function parameters to the coroutine
1790e5dd7070Spatrick // frame, and store them on the function scope info.
buildCoroutineParameterMoves(SourceLocation Loc)1791e5dd7070Spatrick bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) {
1792e5dd7070Spatrick   assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
1793e5dd7070Spatrick   auto *FD = cast<FunctionDecl>(CurContext);
1794e5dd7070Spatrick 
1795e5dd7070Spatrick   auto *ScopeInfo = getCurFunction();
1796e5dd7070Spatrick   if (!ScopeInfo->CoroutineParameterMoves.empty())
1797e5dd7070Spatrick     return false;
1798e5dd7070Spatrick 
1799*12c85518Srobert   // [dcl.fct.def.coroutine]p13
1800*12c85518Srobert   //   When a coroutine is invoked, after initializing its parameters
1801*12c85518Srobert   //   ([expr.call]), a copy is created for each coroutine parameter. For a
1802*12c85518Srobert   //   parameter of type cv T, the copy is a variable of type cv T with
1803*12c85518Srobert   //   automatic storage duration that is direct-initialized from an xvalue of
1804*12c85518Srobert   //   type T referring to the parameter.
1805e5dd7070Spatrick   for (auto *PD : FD->parameters()) {
1806e5dd7070Spatrick     if (PD->getType()->isDependentType())
1807e5dd7070Spatrick       continue;
1808e5dd7070Spatrick 
1809e5dd7070Spatrick     ExprResult PDRefExpr =
1810e5dd7070Spatrick         BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(),
1811e5dd7070Spatrick                          ExprValueKind::VK_LValue, Loc); // FIXME: scope?
1812e5dd7070Spatrick     if (PDRefExpr.isInvalid())
1813e5dd7070Spatrick       return false;
1814e5dd7070Spatrick 
1815e5dd7070Spatrick     Expr *CExpr = nullptr;
1816e5dd7070Spatrick     if (PD->getType()->getAsCXXRecordDecl() ||
1817e5dd7070Spatrick         PD->getType()->isRValueReferenceType())
1818e5dd7070Spatrick       CExpr = castForMoving(*this, PDRefExpr.get());
1819e5dd7070Spatrick     else
1820e5dd7070Spatrick       CExpr = PDRefExpr.get();
1821*12c85518Srobert     // [dcl.fct.def.coroutine]p13
1822*12c85518Srobert     //   The initialization and destruction of each parameter copy occurs in the
1823*12c85518Srobert     //   context of the called coroutine.
1824*12c85518Srobert     auto *D = buildVarDecl(*this, Loc, PD->getType(), PD->getIdentifier());
1825e5dd7070Spatrick     AddInitializerToDecl(D, CExpr, /*DirectInit=*/true);
1826e5dd7070Spatrick 
1827e5dd7070Spatrick     // Convert decl to a statement.
1828e5dd7070Spatrick     StmtResult Stmt = ActOnDeclStmt(ConvertDeclToDeclGroup(D), Loc, Loc);
1829e5dd7070Spatrick     if (Stmt.isInvalid())
1830e5dd7070Spatrick       return false;
1831e5dd7070Spatrick 
1832e5dd7070Spatrick     ScopeInfo->CoroutineParameterMoves.insert(std::make_pair(PD, Stmt.get()));
1833e5dd7070Spatrick   }
1834e5dd7070Spatrick   return true;
1835e5dd7070Spatrick }
1836e5dd7070Spatrick 
BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args)1837e5dd7070Spatrick StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) {
1838e5dd7070Spatrick   CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args);
1839e5dd7070Spatrick   if (!Res)
1840e5dd7070Spatrick     return StmtError();
1841e5dd7070Spatrick   return Res;
1842e5dd7070Spatrick }
1843e5dd7070Spatrick 
lookupCoroutineTraits(SourceLocation KwLoc,SourceLocation FuncLoc,NamespaceDecl * & Namespace)1844e5dd7070Spatrick ClassTemplateDecl *Sema::lookupCoroutineTraits(SourceLocation KwLoc,
1845*12c85518Srobert                                                SourceLocation FuncLoc,
1846*12c85518Srobert                                                NamespaceDecl *&Namespace) {
1847e5dd7070Spatrick   if (!StdCoroutineTraitsCache) {
1848*12c85518Srobert     // Because coroutines moved from std::experimental in the TS to std in
1849*12c85518Srobert     // C++20, we look in both places to give users time to transition their
1850*12c85518Srobert     // TS-specific code to C++20.  Diagnostics are given when the TS usage is
1851*12c85518Srobert     // discovered.
1852*12c85518Srobert     // TODO: Become stricter when <experimental/coroutine> is removed.
1853*12c85518Srobert 
1854*12c85518Srobert     IdentifierInfo const &TraitIdent =
1855*12c85518Srobert         PP.getIdentifierTable().get("coroutine_traits");
1856*12c85518Srobert 
1857*12c85518Srobert     NamespaceDecl *StdSpace = getStdNamespace();
1858*12c85518Srobert     LookupResult ResStd(*this, &TraitIdent, FuncLoc, LookupOrdinaryName);
1859*12c85518Srobert     bool InStd = StdSpace && LookupQualifiedName(ResStd, StdSpace);
1860*12c85518Srobert 
1861*12c85518Srobert     NamespaceDecl *ExpSpace = lookupStdExperimentalNamespace();
1862*12c85518Srobert     LookupResult ResExp(*this, &TraitIdent, FuncLoc, LookupOrdinaryName);
1863*12c85518Srobert     bool InExp = ExpSpace && LookupQualifiedName(ResExp, ExpSpace);
1864*12c85518Srobert 
1865*12c85518Srobert     if (!InStd && !InExp) {
1866*12c85518Srobert       // The goggles, they found nothing!
1867e5dd7070Spatrick       Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
1868*12c85518Srobert           << "std::coroutine_traits";
1869e5dd7070Spatrick       return nullptr;
1870e5dd7070Spatrick     }
1871*12c85518Srobert 
1872*12c85518Srobert     // Prefer ::std to std::experimental.
1873*12c85518Srobert     LookupResult &Result = InStd ? ResStd : ResExp;
1874*12c85518Srobert     CoroTraitsNamespaceCache = InStd ? StdSpace : ExpSpace;
1875*12c85518Srobert 
1876*12c85518Srobert     // coroutine_traits is required to be a class template.
1877*12c85518Srobert     StdCoroutineTraitsCache = Result.getAsSingle<ClassTemplateDecl>();
1878*12c85518Srobert     if (!StdCoroutineTraitsCache) {
1879e5dd7070Spatrick       Result.suppressDiagnostics();
1880e5dd7070Spatrick       NamedDecl *Found = *Result.begin();
1881e5dd7070Spatrick       Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits);
1882e5dd7070Spatrick       return nullptr;
1883e5dd7070Spatrick     }
1884*12c85518Srobert 
1885*12c85518Srobert     if (InExp) {
1886*12c85518Srobert       // Found in std::experimental
1887*12c85518Srobert       Diag(KwLoc, diag::warn_deprecated_coroutine_namespace)
1888*12c85518Srobert           << "coroutine_traits";
1889*12c85518Srobert       ResExp.suppressDiagnostics();
1890*12c85518Srobert       NamedDecl *Found = *ResExp.begin();
1891*12c85518Srobert       Diag(Found->getLocation(), diag::note_entity_declared_at) << Found;
1892*12c85518Srobert 
1893*12c85518Srobert       if (InStd &&
1894*12c85518Srobert           StdCoroutineTraitsCache != ResExp.getAsSingle<ClassTemplateDecl>()) {
1895*12c85518Srobert         // Also found something different in std
1896*12c85518Srobert         Diag(KwLoc,
1897*12c85518Srobert              diag::err_mixed_use_std_and_experimental_namespace_for_coroutine);
1898*12c85518Srobert         Diag(StdCoroutineTraitsCache->getLocation(),
1899*12c85518Srobert              diag::note_entity_declared_at)
1900*12c85518Srobert             << StdCoroutineTraitsCache;
1901*12c85518Srobert 
1902*12c85518Srobert         return nullptr;
1903e5dd7070Spatrick       }
1904e5dd7070Spatrick     }
1905*12c85518Srobert   }
1906*12c85518Srobert   Namespace = CoroTraitsNamespaceCache;
1907e5dd7070Spatrick   return StdCoroutineTraitsCache;
1908e5dd7070Spatrick }
1909