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 
29 using namespace clang;
30 using namespace sema;
31 
lookupMember(Sema & S,const char * Name,CXXRecordDecl * RD,SourceLocation Loc,bool & Res)32 static 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 
lookupMember(Sema & S,const char * Name,CXXRecordDecl * RD,SourceLocation Loc)43 static 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.
lookupPromiseType(Sema & S,const FunctionDecl * FD,SourceLocation KwLoc)52 static 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>.
lookupCoroutineHandleType(Sema & S,QualType PromiseType,SourceLocation Loc)145 static 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 
isValidCoroutineContext(Sema & S,SourceLocation Loc,StringRef Keyword)188 static 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 
buildOperatorCoawaitLookupExpr(Sema & SemaRef,Scope * S,SourceLocation Loc)254 static 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.
buildOperatorCoawaitCall(Sema & SemaRef,SourceLocation Loc,Expr * E,UnresolvedLookupExpr * Lookup)277 static 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 
buildOperatorCoawaitCall(Sema & SemaRef,Scope * S,SourceLocation Loc,Expr * E)285 static 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 
buildBuiltinCall(Sema & S,SourceLocation Loc,Builtin::ID Id,MultiExprArg CallArgs)294 static 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 
buildCoroutineHandle(Sema & S,QualType PromiseType,SourceLocation Loc)314 static 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 
341 struct ReadySuspendResumeResult {
342   enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume };
343   Expr *Results[3];
344   OpaqueValueExpr *OpaqueValue;
345   bool IsInvalid;
346 };
347 
buildMemberCall(Sema & S,Expr * Base,SourceLocation Loc,StringRef Name,MultiExprArg Args)348 static 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.
maybeTailCall(Sema & S,QualType RetType,Expr * E,SourceLocation Loc)377 static 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.
buildCoawaitCalls(Sema & S,VarDecl * CoroPromise,SourceLocation Loc,Expr * E)423 static 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 
buildPromiseCall(Sema & S,VarDecl * Promise,SourceLocation Loc,StringRef Name,MultiExprArg Args)515 static 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 
buildCoroutinePromise(SourceLocation Loc)528 VarDecl *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.
checkCoroutineContext(Sema & S,SourceLocation Loc,StringRef Keyword,bool IsImplicit=false)628 static 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.
checkNoThrow(Sema & S,const Stmt * E,llvm::SmallPtrSetImpl<const Decl * > & ThrowingDecls)658 static 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 
checkFinalSuspendNoThrow(const Stmt * FinalSuspend)711 bool 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 
ActOnCoroutineBodyStart(Scope * SC,SourceLocation KWLoc,StringRef Keyword)729 bool 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.
isWithinCatchScope(Scope * S)781 static 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."
checkSuspensionContext(Sema & S,SourceLocation Loc,StringRef Keyword)809 static 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 
ActOnCoawaitExpr(Scope * S,SourceLocation Loc,Expr * E)822 ExprResult 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 
BuildUnresolvedCoawaitExpr(SourceLocation Loc,Expr * E,UnresolvedLookupExpr * Lookup)842 ExprResult 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 
BuildResolvedCoawaitExpr(SourceLocation Loc,Expr * E,bool IsImplicit)880 ExprResult 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 
ActOnCoyieldExpr(Scope * S,SourceLocation Loc,Expr * E)921 ExprResult 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 }
BuildCoyieldExpr(SourceLocation Loc,Expr * E)942 ExprResult 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 
ActOnCoreturnStmt(Scope * S,SourceLocation Loc,Expr * E)976 StmtResult 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 
BuildCoreturnStmt(SourceLocation Loc,Expr * E,bool IsImplicit)984 StmtResult 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     auto NRVOCandidate = this->getCopyElisionCandidate(E->getType(), E, CES_AsIfByStdMove);
1000     if (NRVOCandidate) {
1001       InitializedEntity Entity =
1002           InitializedEntity::InitializeResult(Loc, E->getType(), NRVOCandidate);
1003       ExprResult MoveResult = this->PerformMoveOrCopyInitialization(
1004           Entity, NRVOCandidate, E->getType(), E);
1005       if (MoveResult.get())
1006         E = MoveResult.get();
1007     }
1008   }
1009 
1010   // FIXME: If the operand is a reference to a variable that's about to go out
1011   // of scope, we should treat the operand as an xvalue for this overload
1012   // resolution.
1013   VarDecl *Promise = FSI->CoroutinePromise;
1014   ExprResult PC;
1015   if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) {
1016     PC = buildPromiseCall(*this, Promise, Loc, "return_value", E);
1017   } else {
1018     E = MakeFullDiscardedValueExpr(E).get();
1019     PC = buildPromiseCall(*this, Promise, Loc, "return_void", None);
1020   }
1021   if (PC.isInvalid())
1022     return StmtError();
1023 
1024   Expr *PCE = ActOnFinishFullExpr(PC.get(), /*DiscardedValue*/ false).get();
1025 
1026   Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit);
1027   return Res;
1028 }
1029 
1030 /// Look up the std::nothrow object.
buildStdNoThrowDeclRef(Sema & S,SourceLocation Loc)1031 static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) {
1032   NamespaceDecl *Std = S.getStdNamespace();
1033   assert(Std && "Should already be diagnosed");
1034 
1035   LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc,
1036                       Sema::LookupOrdinaryName);
1037   if (!S.LookupQualifiedName(Result, Std)) {
1038     // FIXME: <experimental/coroutine> should have been included already.
1039     // If we require it to include <new> then this diagnostic is no longer
1040     // needed.
1041     S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found);
1042     return nullptr;
1043   }
1044 
1045   auto *VD = Result.getAsSingle<VarDecl>();
1046   if (!VD) {
1047     Result.suppressDiagnostics();
1048     // We found something weird. Complain about the first thing we found.
1049     NamedDecl *Found = *Result.begin();
1050     S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow);
1051     return nullptr;
1052   }
1053 
1054   ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc);
1055   if (DR.isInvalid())
1056     return nullptr;
1057 
1058   return DR.get();
1059 }
1060 
1061 // Find an appropriate delete for the promise.
findDeleteForPromise(Sema & S,SourceLocation Loc,QualType PromiseType)1062 static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc,
1063                                           QualType PromiseType) {
1064   FunctionDecl *OperatorDelete = nullptr;
1065 
1066   DeclarationName DeleteName =
1067       S.Context.DeclarationNames.getCXXOperatorName(OO_Delete);
1068 
1069   auto *PointeeRD = PromiseType->getAsCXXRecordDecl();
1070   assert(PointeeRD && "PromiseType must be a CxxRecordDecl type");
1071 
1072   if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete))
1073     return nullptr;
1074 
1075   if (!OperatorDelete) {
1076     // Look for a global declaration.
1077     const bool CanProvideSize = S.isCompleteType(Loc, PromiseType);
1078     const bool Overaligned = false;
1079     OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize,
1080                                                      Overaligned, DeleteName);
1081   }
1082   S.MarkFunctionReferenced(Loc, OperatorDelete);
1083   return OperatorDelete;
1084 }
1085 
1086 
CheckCompletedCoroutineBody(FunctionDecl * FD,Stmt * & Body)1087 void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) {
1088   FunctionScopeInfo *Fn = getCurFunction();
1089   assert(Fn && Fn->isCoroutine() && "not a coroutine");
1090   if (!Body) {
1091     assert(FD->isInvalidDecl() &&
1092            "a null body is only allowed for invalid declarations");
1093     return;
1094   }
1095   // We have a function that uses coroutine keywords, but we failed to build
1096   // the promise type.
1097   if (!Fn->CoroutinePromise)
1098     return FD->setInvalidDecl();
1099 
1100   if (isa<CoroutineBodyStmt>(Body)) {
1101     // Nothing todo. the body is already a transformed coroutine body statement.
1102     return;
1103   }
1104 
1105   // Coroutines [stmt.return]p1:
1106   //   A return statement shall not appear in a coroutine.
1107   if (Fn->FirstReturnLoc.isValid()) {
1108     assert(Fn->FirstCoroutineStmtLoc.isValid() &&
1109                    "first coroutine location not set");
1110     Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine);
1111     Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1112             << Fn->getFirstCoroutineStmtKeyword();
1113   }
1114   CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body);
1115   if (Builder.isInvalid() || !Builder.buildStatements())
1116     return FD->setInvalidDecl();
1117 
1118   // Build body for the coroutine wrapper statement.
1119   Body = CoroutineBodyStmt::Create(Context, Builder);
1120 }
1121 
CoroutineStmtBuilder(Sema & S,FunctionDecl & FD,sema::FunctionScopeInfo & Fn,Stmt * Body)1122 CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD,
1123                                            sema::FunctionScopeInfo &Fn,
1124                                            Stmt *Body)
1125     : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()),
1126       IsPromiseDependentType(
1127           !Fn.CoroutinePromise ||
1128           Fn.CoroutinePromise->getType()->isDependentType()) {
1129   this->Body = Body;
1130 
1131   for (auto KV : Fn.CoroutineParameterMoves)
1132     this->ParamMovesVector.push_back(KV.second);
1133   this->ParamMoves = this->ParamMovesVector;
1134 
1135   if (!IsPromiseDependentType) {
1136     PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl();
1137     assert(PromiseRecordDecl && "Type should have already been checked");
1138   }
1139   this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend();
1140 }
1141 
buildStatements()1142 bool CoroutineStmtBuilder::buildStatements() {
1143   assert(this->IsValid && "coroutine already invalid");
1144   this->IsValid = makeReturnObject();
1145   if (this->IsValid && !IsPromiseDependentType)
1146     buildDependentStatements();
1147   return this->IsValid;
1148 }
1149 
buildDependentStatements()1150 bool CoroutineStmtBuilder::buildDependentStatements() {
1151   assert(this->IsValid && "coroutine already invalid");
1152   assert(!this->IsPromiseDependentType &&
1153          "coroutine cannot have a dependent promise type");
1154   this->IsValid = makeOnException() && makeOnFallthrough() &&
1155                   makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() &&
1156                   makeNewAndDeleteExpr();
1157   return this->IsValid;
1158 }
1159 
makePromiseStmt()1160 bool CoroutineStmtBuilder::makePromiseStmt() {
1161   // Form a declaration statement for the promise declaration, so that AST
1162   // visitors can more easily find it.
1163   StmtResult PromiseStmt =
1164       S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc);
1165   if (PromiseStmt.isInvalid())
1166     return false;
1167 
1168   this->Promise = PromiseStmt.get();
1169   return true;
1170 }
1171 
makeInitialAndFinalSuspend()1172 bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() {
1173   if (Fn.hasInvalidCoroutineSuspends())
1174     return false;
1175   this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first);
1176   this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second);
1177   return true;
1178 }
1179 
diagReturnOnAllocFailure(Sema & S,Expr * E,CXXRecordDecl * PromiseRecordDecl,FunctionScopeInfo & Fn)1180 static bool diagReturnOnAllocFailure(Sema &S, Expr *E,
1181                                      CXXRecordDecl *PromiseRecordDecl,
1182                                      FunctionScopeInfo &Fn) {
1183   auto Loc = E->getExprLoc();
1184   if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) {
1185     auto *Decl = DeclRef->getDecl();
1186     if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) {
1187       if (Method->isStatic())
1188         return true;
1189       else
1190         Loc = Decl->getLocation();
1191     }
1192   }
1193 
1194   S.Diag(
1195       Loc,
1196       diag::err_coroutine_promise_get_return_object_on_allocation_failure)
1197       << PromiseRecordDecl;
1198   S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1199       << Fn.getFirstCoroutineStmtKeyword();
1200   return false;
1201 }
1202 
makeReturnOnAllocFailure()1203 bool CoroutineStmtBuilder::makeReturnOnAllocFailure() {
1204   assert(!IsPromiseDependentType &&
1205          "cannot make statement while the promise type is dependent");
1206 
1207   // [dcl.fct.def.coroutine]/8
1208   // The unqualified-id get_return_object_on_allocation_failure is looked up in
1209   // the scope of class P by class member access lookup (3.4.5). ...
1210   // If an allocation function returns nullptr, ... the coroutine return value
1211   // is obtained by a call to ... get_return_object_on_allocation_failure().
1212 
1213   DeclarationName DN =
1214       S.PP.getIdentifierInfo("get_return_object_on_allocation_failure");
1215   LookupResult Found(S, DN, Loc, Sema::LookupMemberName);
1216   if (!S.LookupQualifiedName(Found, PromiseRecordDecl))
1217     return true;
1218 
1219   CXXScopeSpec SS;
1220   ExprResult DeclNameExpr =
1221       S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
1222   if (DeclNameExpr.isInvalid())
1223     return false;
1224 
1225   if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn))
1226     return false;
1227 
1228   ExprResult ReturnObjectOnAllocationFailure =
1229       S.BuildCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc);
1230   if (ReturnObjectOnAllocationFailure.isInvalid())
1231     return false;
1232 
1233   StmtResult ReturnStmt =
1234       S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get());
1235   if (ReturnStmt.isInvalid()) {
1236     S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here)
1237         << DN;
1238     S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1239         << Fn.getFirstCoroutineStmtKeyword();
1240     return false;
1241   }
1242 
1243   this->ReturnStmtOnAllocFailure = ReturnStmt.get();
1244   return true;
1245 }
1246 
makeNewAndDeleteExpr()1247 bool CoroutineStmtBuilder::makeNewAndDeleteExpr() {
1248   // Form and check allocation and deallocation calls.
1249   assert(!IsPromiseDependentType &&
1250          "cannot make statement while the promise type is dependent");
1251   QualType PromiseType = Fn.CoroutinePromise->getType();
1252 
1253   if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type))
1254     return false;
1255 
1256   const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr;
1257 
1258   // [dcl.fct.def.coroutine]/7
1259   // Lookup allocation functions using a parameter list composed of the
1260   // requested size of the coroutine state being allocated, followed by
1261   // the coroutine function's arguments. If a matching allocation function
1262   // exists, use it. Otherwise, use an allocation function that just takes
1263   // the requested size.
1264 
1265   FunctionDecl *OperatorNew = nullptr;
1266   FunctionDecl *OperatorDelete = nullptr;
1267   FunctionDecl *UnusedResult = nullptr;
1268   bool PassAlignment = false;
1269   SmallVector<Expr *, 1> PlacementArgs;
1270 
1271   // [dcl.fct.def.coroutine]/7
1272   // "The allocation function’s name is looked up in the scope of P.
1273   // [...] If the lookup finds an allocation function in the scope of P,
1274   // overload resolution is performed on a function call created by assembling
1275   // an argument list. The first argument is the amount of space requested,
1276   // and has type std::size_t. The lvalues p1 ... pn are the succeeding
1277   // arguments."
1278   //
1279   // ...where "p1 ... pn" are defined earlier as:
1280   //
1281   // [dcl.fct.def.coroutine]/3
1282   // "For a coroutine f that is a non-static member function, let P1 denote the
1283   // type of the implicit object parameter (13.3.1) and P2 ... Pn be the types
1284   // of the function parameters; otherwise let P1 ... Pn be the types of the
1285   // function parameters. Let p1 ... pn be lvalues denoting those objects."
1286   if (auto *MD = dyn_cast<CXXMethodDecl>(&FD)) {
1287     if (MD->isInstance() && !isLambdaCallOperator(MD)) {
1288       ExprResult ThisExpr = S.ActOnCXXThis(Loc);
1289       if (ThisExpr.isInvalid())
1290         return false;
1291       ThisExpr = S.CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get());
1292       if (ThisExpr.isInvalid())
1293         return false;
1294       PlacementArgs.push_back(ThisExpr.get());
1295     }
1296   }
1297   for (auto *PD : FD.parameters()) {
1298     if (PD->getType()->isDependentType())
1299       continue;
1300 
1301     // Build a reference to the parameter.
1302     auto PDLoc = PD->getLocation();
1303     ExprResult PDRefExpr =
1304         S.BuildDeclRefExpr(PD, PD->getOriginalType().getNonReferenceType(),
1305                            ExprValueKind::VK_LValue, PDLoc);
1306     if (PDRefExpr.isInvalid())
1307       return false;
1308 
1309     PlacementArgs.push_back(PDRefExpr.get());
1310   }
1311   S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Class,
1312                             /*DeleteScope*/ Sema::AFS_Both, PromiseType,
1313                             /*isArray*/ false, PassAlignment, PlacementArgs,
1314                             OperatorNew, UnusedResult, /*Diagnose*/ false);
1315 
1316   // [dcl.fct.def.coroutine]/7
1317   // "If no matching function is found, overload resolution is performed again
1318   // on a function call created by passing just the amount of space required as
1319   // an argument of type std::size_t."
1320   if (!OperatorNew && !PlacementArgs.empty()) {
1321     PlacementArgs.clear();
1322     S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Class,
1323                               /*DeleteScope*/ Sema::AFS_Both, PromiseType,
1324                               /*isArray*/ false, PassAlignment, PlacementArgs,
1325                               OperatorNew, UnusedResult, /*Diagnose*/ false);
1326   }
1327 
1328   // [dcl.fct.def.coroutine]/7
1329   // "The allocation function’s name is looked up in the scope of P. If this
1330   // lookup fails, the allocation function’s name is looked up in the global
1331   // scope."
1332   if (!OperatorNew) {
1333     S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Global,
1334                               /*DeleteScope*/ Sema::AFS_Both, PromiseType,
1335                               /*isArray*/ false, PassAlignment, PlacementArgs,
1336                               OperatorNew, UnusedResult);
1337   }
1338 
1339   bool IsGlobalOverload =
1340       OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext());
1341   // If we didn't find a class-local new declaration and non-throwing new
1342   // was is required then we need to lookup the non-throwing global operator
1343   // instead.
1344   if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) {
1345     auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc);
1346     if (!StdNoThrow)
1347       return false;
1348     PlacementArgs = {StdNoThrow};
1349     OperatorNew = nullptr;
1350     S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Both,
1351                               /*DeleteScope*/ Sema::AFS_Both, PromiseType,
1352                               /*isArray*/ false, PassAlignment, PlacementArgs,
1353                               OperatorNew, UnusedResult);
1354   }
1355 
1356   if (!OperatorNew)
1357     return false;
1358 
1359   if (RequiresNoThrowAlloc) {
1360     const auto *FT = OperatorNew->getType()->castAs<FunctionProtoType>();
1361     if (!FT->isNothrow(/*ResultIfDependent*/ false)) {
1362       S.Diag(OperatorNew->getLocation(),
1363              diag::err_coroutine_promise_new_requires_nothrow)
1364           << OperatorNew;
1365       S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
1366           << OperatorNew;
1367       return false;
1368     }
1369   }
1370 
1371   if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr)
1372     return false;
1373 
1374   Expr *FramePtr =
1375       buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
1376 
1377   Expr *FrameSize =
1378       buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_size, {});
1379 
1380   // Make new call.
1381 
1382   ExprResult NewRef =
1383       S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc);
1384   if (NewRef.isInvalid())
1385     return false;
1386 
1387   SmallVector<Expr *, 2> NewArgs(1, FrameSize);
1388   for (auto Arg : PlacementArgs)
1389     NewArgs.push_back(Arg);
1390 
1391   ExprResult NewExpr =
1392       S.BuildCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc);
1393   NewExpr = S.ActOnFinishFullExpr(NewExpr.get(), /*DiscardedValue*/ false);
1394   if (NewExpr.isInvalid())
1395     return false;
1396 
1397   // Make delete call.
1398 
1399   QualType OpDeleteQualType = OperatorDelete->getType();
1400 
1401   ExprResult DeleteRef =
1402       S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc);
1403   if (DeleteRef.isInvalid())
1404     return false;
1405 
1406   Expr *CoroFree =
1407       buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_free, {FramePtr});
1408 
1409   SmallVector<Expr *, 2> DeleteArgs{CoroFree};
1410 
1411   // Check if we need to pass the size.
1412   const auto *OpDeleteType =
1413       OpDeleteQualType.getTypePtr()->castAs<FunctionProtoType>();
1414   if (OpDeleteType->getNumParams() > 1)
1415     DeleteArgs.push_back(FrameSize);
1416 
1417   ExprResult DeleteExpr =
1418       S.BuildCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc);
1419   DeleteExpr =
1420       S.ActOnFinishFullExpr(DeleteExpr.get(), /*DiscardedValue*/ false);
1421   if (DeleteExpr.isInvalid())
1422     return false;
1423 
1424   this->Allocate = NewExpr.get();
1425   this->Deallocate = DeleteExpr.get();
1426 
1427   return true;
1428 }
1429 
makeOnFallthrough()1430 bool CoroutineStmtBuilder::makeOnFallthrough() {
1431   assert(!IsPromiseDependentType &&
1432          "cannot make statement while the promise type is dependent");
1433 
1434   // [dcl.fct.def.coroutine]/4
1435   // The unqualified-ids 'return_void' and 'return_value' are looked up in
1436   // the scope of class P. If both are found, the program is ill-formed.
1437   bool HasRVoid, HasRValue;
1438   LookupResult LRVoid =
1439       lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid);
1440   LookupResult LRValue =
1441       lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue);
1442 
1443   StmtResult Fallthrough;
1444   if (HasRVoid && HasRValue) {
1445     // FIXME Improve this diagnostic
1446     S.Diag(FD.getLocation(),
1447            diag::err_coroutine_promise_incompatible_return_functions)
1448         << PromiseRecordDecl;
1449     S.Diag(LRVoid.getRepresentativeDecl()->getLocation(),
1450            diag::note_member_first_declared_here)
1451         << LRVoid.getLookupName();
1452     S.Diag(LRValue.getRepresentativeDecl()->getLocation(),
1453            diag::note_member_first_declared_here)
1454         << LRValue.getLookupName();
1455     return false;
1456   } else if (!HasRVoid && !HasRValue) {
1457     // FIXME: The PDTS currently specifies this case as UB, not ill-formed.
1458     // However we still diagnose this as an error since until the PDTS is fixed.
1459     S.Diag(FD.getLocation(),
1460            diag::err_coroutine_promise_requires_return_function)
1461         << PromiseRecordDecl;
1462     S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
1463         << PromiseRecordDecl;
1464     return false;
1465   } else if (HasRVoid) {
1466     // If the unqualified-id return_void is found, flowing off the end of a
1467     // coroutine is equivalent to a co_return with no operand. Otherwise,
1468     // flowing off the end of a coroutine results in undefined behavior.
1469     Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr,
1470                                       /*IsImplicit*/false);
1471     Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get());
1472     if (Fallthrough.isInvalid())
1473       return false;
1474   }
1475 
1476   this->OnFallthrough = Fallthrough.get();
1477   return true;
1478 }
1479 
makeOnException()1480 bool CoroutineStmtBuilder::makeOnException() {
1481   // Try to form 'p.unhandled_exception();'
1482   assert(!IsPromiseDependentType &&
1483          "cannot make statement while the promise type is dependent");
1484 
1485   const bool RequireUnhandledException = S.getLangOpts().CXXExceptions;
1486 
1487   if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) {
1488     auto DiagID =
1489         RequireUnhandledException
1490             ? diag::err_coroutine_promise_unhandled_exception_required
1491             : diag::
1492                   warn_coroutine_promise_unhandled_exception_required_with_exceptions;
1493     S.Diag(Loc, DiagID) << PromiseRecordDecl;
1494     S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
1495         << PromiseRecordDecl;
1496     return !RequireUnhandledException;
1497   }
1498 
1499   // If exceptions are disabled, don't try to build OnException.
1500   if (!S.getLangOpts().CXXExceptions)
1501     return true;
1502 
1503   ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc,
1504                                                    "unhandled_exception", None);
1505   UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc,
1506                                              /*DiscardedValue*/ false);
1507   if (UnhandledException.isInvalid())
1508     return false;
1509 
1510   // Since the body of the coroutine will be wrapped in try-catch, it will
1511   // be incompatible with SEH __try if present in a function.
1512   if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) {
1513     S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions);
1514     S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1515         << Fn.getFirstCoroutineStmtKeyword();
1516     return false;
1517   }
1518 
1519   this->OnException = UnhandledException.get();
1520   return true;
1521 }
1522 
makeReturnObject()1523 bool CoroutineStmtBuilder::makeReturnObject() {
1524   // Build implicit 'p.get_return_object()' expression and form initialization
1525   // of return type from it.
1526   ExprResult ReturnObject =
1527       buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None);
1528   if (ReturnObject.isInvalid())
1529     return false;
1530 
1531   this->ReturnValue = ReturnObject.get();
1532   return true;
1533 }
1534 
noteMemberDeclaredHere(Sema & S,Expr * E,FunctionScopeInfo & Fn)1535 static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) {
1536   if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) {
1537     auto *MethodDecl = MbrRef->getMethodDecl();
1538     S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here)
1539         << MethodDecl;
1540   }
1541   S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1542       << Fn.getFirstCoroutineStmtKeyword();
1543 }
1544 
makeGroDeclAndReturnStmt()1545 bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
1546   assert(!IsPromiseDependentType &&
1547          "cannot make statement while the promise type is dependent");
1548   assert(this->ReturnValue && "ReturnValue must be already formed");
1549 
1550   QualType const GroType = this->ReturnValue->getType();
1551   assert(!GroType->isDependentType() &&
1552          "get_return_object type must no longer be dependent");
1553 
1554   QualType const FnRetType = FD.getReturnType();
1555   assert(!FnRetType->isDependentType() &&
1556          "get_return_object type must no longer be dependent");
1557 
1558   if (FnRetType->isVoidType()) {
1559     ExprResult Res =
1560         S.ActOnFinishFullExpr(this->ReturnValue, Loc, /*DiscardedValue*/ false);
1561     if (Res.isInvalid())
1562       return false;
1563 
1564     this->ResultDecl = Res.get();
1565     return true;
1566   }
1567 
1568   if (GroType->isVoidType()) {
1569     // Trigger a nice error message.
1570     InitializedEntity Entity =
1571         InitializedEntity::InitializeResult(Loc, FnRetType, false);
1572     S.PerformMoveOrCopyInitialization(Entity, nullptr, FnRetType, ReturnValue);
1573     noteMemberDeclaredHere(S, ReturnValue, Fn);
1574     return false;
1575   }
1576 
1577   auto *GroDecl = VarDecl::Create(
1578       S.Context, &FD, FD.getLocation(), FD.getLocation(),
1579       &S.PP.getIdentifierTable().get("__coro_gro"), GroType,
1580       S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None);
1581   GroDecl->setImplicit();
1582 
1583   S.CheckVariableDeclarationType(GroDecl);
1584   if (GroDecl->isInvalidDecl())
1585     return false;
1586 
1587   InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl);
1588   ExprResult Res = S.PerformMoveOrCopyInitialization(Entity, nullptr, GroType,
1589                                                      this->ReturnValue);
1590   if (Res.isInvalid())
1591     return false;
1592 
1593   Res = S.ActOnFinishFullExpr(Res.get(), /*DiscardedValue*/ false);
1594   if (Res.isInvalid())
1595     return false;
1596 
1597   S.AddInitializerToDecl(GroDecl, Res.get(),
1598                          /*DirectInit=*/false);
1599 
1600   S.FinalizeDeclaration(GroDecl);
1601 
1602   // Form a declaration statement for the return declaration, so that AST
1603   // visitors can more easily find it.
1604   StmtResult GroDeclStmt =
1605       S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(GroDecl), Loc, Loc);
1606   if (GroDeclStmt.isInvalid())
1607     return false;
1608 
1609   this->ResultDecl = GroDeclStmt.get();
1610 
1611   ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc);
1612   if (declRef.isInvalid())
1613     return false;
1614 
1615   StmtResult ReturnStmt = S.BuildReturnStmt(Loc, declRef.get());
1616   if (ReturnStmt.isInvalid()) {
1617     noteMemberDeclaredHere(S, ReturnValue, Fn);
1618     return false;
1619   }
1620   if (cast<clang::ReturnStmt>(ReturnStmt.get())->getNRVOCandidate() == GroDecl)
1621     GroDecl->setNRVOVariable(true);
1622 
1623   this->ReturnStmt = ReturnStmt.get();
1624   return true;
1625 }
1626 
1627 // Create a static_cast\<T&&>(expr).
castForMoving(Sema & S,Expr * E,QualType T=QualType ())1628 static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) {
1629   if (T.isNull())
1630     T = E->getType();
1631   QualType TargetType = S.BuildReferenceType(
1632       T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName());
1633   SourceLocation ExprLoc = E->getBeginLoc();
1634   TypeSourceInfo *TargetLoc =
1635       S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc);
1636 
1637   return S
1638       .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E,
1639                          SourceRange(ExprLoc, ExprLoc), E->getSourceRange())
1640       .get();
1641 }
1642 
1643 /// Build a variable declaration for move parameter.
buildVarDecl(Sema & S,SourceLocation Loc,QualType Type,IdentifierInfo * II)1644 static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type,
1645                              IdentifierInfo *II) {
1646   TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc);
1647   VarDecl *Decl = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type,
1648                                   TInfo, SC_None);
1649   Decl->setImplicit();
1650   return Decl;
1651 }
1652 
1653 // Build statements that move coroutine function parameters to the coroutine
1654 // frame, and store them on the function scope info.
buildCoroutineParameterMoves(SourceLocation Loc)1655 bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) {
1656   assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
1657   auto *FD = cast<FunctionDecl>(CurContext);
1658 
1659   auto *ScopeInfo = getCurFunction();
1660   if (!ScopeInfo->CoroutineParameterMoves.empty())
1661     return false;
1662 
1663   for (auto *PD : FD->parameters()) {
1664     if (PD->getType()->isDependentType())
1665       continue;
1666 
1667     ExprResult PDRefExpr =
1668         BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(),
1669                          ExprValueKind::VK_LValue, Loc); // FIXME: scope?
1670     if (PDRefExpr.isInvalid())
1671       return false;
1672 
1673     Expr *CExpr = nullptr;
1674     if (PD->getType()->getAsCXXRecordDecl() ||
1675         PD->getType()->isRValueReferenceType())
1676       CExpr = castForMoving(*this, PDRefExpr.get());
1677     else
1678       CExpr = PDRefExpr.get();
1679 
1680     auto D = buildVarDecl(*this, Loc, PD->getType(), PD->getIdentifier());
1681     AddInitializerToDecl(D, CExpr, /*DirectInit=*/true);
1682 
1683     // Convert decl to a statement.
1684     StmtResult Stmt = ActOnDeclStmt(ConvertDeclToDeclGroup(D), Loc, Loc);
1685     if (Stmt.isInvalid())
1686       return false;
1687 
1688     ScopeInfo->CoroutineParameterMoves.insert(std::make_pair(PD, Stmt.get()));
1689   }
1690   return true;
1691 }
1692 
BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args)1693 StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) {
1694   CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args);
1695   if (!Res)
1696     return StmtError();
1697   return Res;
1698 }
1699 
lookupCoroutineTraits(SourceLocation KwLoc,SourceLocation FuncLoc)1700 ClassTemplateDecl *Sema::lookupCoroutineTraits(SourceLocation KwLoc,
1701                                                SourceLocation FuncLoc) {
1702   if (!StdCoroutineTraitsCache) {
1703     if (auto StdExp = lookupStdExperimentalNamespace()) {
1704       LookupResult Result(*this,
1705                           &PP.getIdentifierTable().get("coroutine_traits"),
1706                           FuncLoc, LookupOrdinaryName);
1707       if (!LookupQualifiedName(Result, StdExp)) {
1708         Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
1709             << "std::experimental::coroutine_traits";
1710         return nullptr;
1711       }
1712       if (!(StdCoroutineTraitsCache =
1713                 Result.getAsSingle<ClassTemplateDecl>())) {
1714         Result.suppressDiagnostics();
1715         NamedDecl *Found = *Result.begin();
1716         Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits);
1717         return nullptr;
1718       }
1719     }
1720   }
1721   return StdCoroutineTraitsCache;
1722 }
1723