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

source code of clang/lib/Sema/SemaCoroutine.cpp