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