1 //===--- SemaCoroutines.cpp - Semantic Analysis for Coroutines ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for C++ Coroutines.
12 //===----------------------------------------------------------------------===//
14 #include "CoroutineStmtBuilder.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/ExprCXX.h"
17 #include "clang/AST/StmtCXX.h"
18 #include "clang/Lex/Preprocessor.h"
19 #include "clang/Sema/Initialization.h"
20 #include "clang/Sema/Overload.h"
21 #include "clang/Sema/SemaInternal.h"
23 using namespace clang;
26 static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
27 SourceLocation Loc, bool &Res) {
28 DeclarationName DN = S.PP.getIdentifierInfo(Name);
29 LookupResult LR(S, DN, Loc, Sema::LookupMemberName);
30 // Suppress diagnostics when a private member is selected. The same warnings
31 // will be produced again when building the call.
32 LR.suppressDiagnostics();
33 Res = S.LookupQualifiedName(LR, RD);
37 static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
40 lookupMember(S, Name, RD, Loc, Res);
44 /// Look up the std::coroutine_traits<...>::promise_type for the given
46 static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD,
47 SourceLocation KwLoc) {
48 const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>();
49 const SourceLocation FuncLoc = FD->getLocation();
50 // FIXME: Cache std::coroutine_traits once we've found it.
51 NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace();
53 S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
54 << "std::experimental::coroutine_traits";
58 LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_traits"),
59 FuncLoc, Sema::LookupOrdinaryName);
60 if (!S.LookupQualifiedName(Result, StdExp)) {
61 S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
62 << "std::experimental::coroutine_traits";
66 ClassTemplateDecl *CoroTraits = Result.getAsSingle<ClassTemplateDecl>();
68 Result.suppressDiagnostics();
69 // We found something weird. Complain about the first thing we found.
70 NamedDecl *Found = *Result.begin();
71 S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits);
75 // Form template argument list for coroutine_traits<R, P1, P2, ...> according
76 // to [dcl.fct.def.coroutine]3
77 TemplateArgumentListInfo Args(KwLoc, KwLoc);
78 auto AddArg = [&](QualType T) {
79 Args.addArgument(TemplateArgumentLoc(
80 TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc)));
82 AddArg(FnType->getReturnType());
83 // If the function is a non-static member function, add the type
84 // of the implicit object parameter before the formal parameters.
85 if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
86 if (MD->isInstance()) {
87 // [over.match.funcs]4
88 // For non-static member functions, the type of the implicit object
90 // -- "lvalue reference to cv X" for functions declared without a
91 // ref-qualifier or with the & ref-qualifier
92 // -- "rvalue reference to cv X" for functions declared with the &&
95 MD->getThisType(S.Context)->getAs<PointerType>()->getPointeeType();
96 T = FnType->getRefQualifier() == RQ_RValue
97 ? S.Context.getRValueReferenceType(T)
98 : S.Context.getLValueReferenceType(T, /*SpelledAsLValue*/ true);
102 for (QualType T : FnType->getParamTypes())
105 // Build the template-id.
107 S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args);
108 if (CoroTrait.isNull())
110 if (S.RequireCompleteType(KwLoc, CoroTrait,
111 diag::err_coroutine_type_missing_specialization))
114 auto *RD = CoroTrait->getAsCXXRecordDecl();
115 assert(RD && "specialization of class template is not a class?");
117 // Look up the ::promise_type member.
118 LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc,
119 Sema::LookupOrdinaryName);
120 S.LookupQualifiedName(R, RD);
121 auto *Promise = R.getAsSingle<TypeDecl>();
124 diag::err_implied_std_coroutine_traits_promise_type_not_found)
128 // The promise type is required to be a class type.
129 QualType PromiseType = S.Context.getTypeDeclType(Promise);
131 auto buildElaboratedType = [&]() {
132 auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, StdExp);
133 NNS = NestedNameSpecifier::Create(S.Context, NNS, false,
134 CoroTrait.getTypePtr());
135 return S.Context.getElaboratedType(ETK_None, NNS, PromiseType);
138 if (!PromiseType->getAsCXXRecordDecl()) {
140 diag::err_implied_std_coroutine_traits_promise_type_not_class)
141 << buildElaboratedType();
144 if (S.RequireCompleteType(FuncLoc, buildElaboratedType(),
145 diag::err_coroutine_promise_type_incomplete))
151 /// Look up the std::experimental::coroutine_handle<PromiseType>.
152 static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType,
153 SourceLocation Loc) {
154 if (PromiseType.isNull())
157 NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace();
158 assert(StdExp && "Should already be diagnosed");
160 LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"),
161 Loc, Sema::LookupOrdinaryName);
162 if (!S.LookupQualifiedName(Result, StdExp)) {
163 S.Diag(Loc, diag::err_implied_coroutine_type_not_found)
164 << "std::experimental::coroutine_handle";
168 ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>();
170 Result.suppressDiagnostics();
171 // We found something weird. Complain about the first thing we found.
172 NamedDecl *Found = *Result.begin();
173 S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle);
177 // Form template argument list for coroutine_handle<Promise>.
178 TemplateArgumentListInfo Args(Loc, Loc);
179 Args.addArgument(TemplateArgumentLoc(
180 TemplateArgument(PromiseType),
181 S.Context.getTrivialTypeSourceInfo(PromiseType, Loc)));
183 // Build the template-id.
184 QualType CoroHandleType =
185 S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args);
186 if (CoroHandleType.isNull())
188 if (S.RequireCompleteType(Loc, CoroHandleType,
189 diag::err_coroutine_type_missing_specialization))
192 return CoroHandleType;
195 static bool isValidCoroutineContext(Sema &S, SourceLocation Loc,
197 // 'co_await' and 'co_yield' are not permitted in unevaluated operands.
198 if (S.isUnevaluatedContext()) {
199 S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword;
203 // Any other usage must be within a function.
204 auto *FD = dyn_cast<FunctionDecl>(S.CurContext);
206 S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext)
207 ? diag::err_coroutine_objc_method
208 : diag::err_coroutine_outside_function) << Keyword;
212 // An enumeration for mapping the diagnostic type to the correct diagnostic
214 enum InvalidFuncDiag {
224 bool Diagnosed = false;
225 auto DiagInvalid = [&](InvalidFuncDiag ID) {
226 S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword;
231 // Diagnose when a constructor, destructor, copy/move assignment operator,
232 // or the function 'main' are declared as a coroutine.
233 auto *MD = dyn_cast<CXXMethodDecl>(FD);
234 if (MD && isa<CXXConstructorDecl>(MD))
235 return DiagInvalid(DiagCtor);
236 else if (MD && isa<CXXDestructorDecl>(MD))
237 return DiagInvalid(DiagDtor);
238 else if (MD && MD->isCopyAssignmentOperator())
239 return DiagInvalid(DiagCopyAssign);
240 else if (MD && MD->isMoveAssignmentOperator())
241 return DiagInvalid(DiagMoveAssign);
242 else if (FD->isMain())
243 return DiagInvalid(DiagMain);
245 // Emit a diagnostics for each of the following conditions which is not met.
246 if (FD->isConstexpr())
247 DiagInvalid(DiagConstexpr);
248 if (FD->getReturnType()->isUndeducedType())
249 DiagInvalid(DiagAutoRet);
250 if (FD->isVariadic())
251 DiagInvalid(DiagVarargs);
256 static ExprResult buildOperatorCoawaitLookupExpr(Sema &SemaRef, Scope *S,
257 SourceLocation Loc) {
258 DeclarationName OpName =
259 SemaRef.Context.DeclarationNames.getCXXOperatorName(OO_Coawait);
260 LookupResult Operators(SemaRef, OpName, SourceLocation(),
261 Sema::LookupOperatorName);
262 SemaRef.LookupName(Operators, S);
264 assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
265 const auto &Functions = Operators.asUnresolvedSet();
267 Functions.size() > 1 ||
268 (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin()));
269 Expr *CoawaitOp = UnresolvedLookupExpr::Create(
270 SemaRef.Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(),
271 DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, IsOverloaded,
272 Functions.begin(), Functions.end());
277 /// Build a call to 'operator co_await' if there is a suitable operator for
278 /// the given expression.
279 static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, SourceLocation Loc,
281 UnresolvedLookupExpr *Lookup) {
282 UnresolvedSet<16> Functions;
283 Functions.append(Lookup->decls_begin(), Lookup->decls_end());
284 return SemaRef.CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E);
287 static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S,
288 SourceLocation Loc, Expr *E) {
289 ExprResult R = buildOperatorCoawaitLookupExpr(SemaRef, S, Loc);
292 return buildOperatorCoawaitCall(SemaRef, Loc, E,
293 cast<UnresolvedLookupExpr>(R.get()));
296 static Expr *buildBuiltinCall(Sema &S, SourceLocation Loc, Builtin::ID Id,
297 MultiExprArg CallArgs) {
298 StringRef Name = S.Context.BuiltinInfo.getName(Id);
299 LookupResult R(S, &S.Context.Idents.get(Name), Loc, Sema::LookupOrdinaryName);
300 S.LookupName(R, S.TUScope, /*AllowBuiltinCreation=*/true);
302 auto *BuiltInDecl = R.getAsSingle<FunctionDecl>();
303 assert(BuiltInDecl && "failed to find builtin declaration");
306 S.BuildDeclRefExpr(BuiltInDecl, BuiltInDecl->getType(), VK_LValue, Loc);
307 assert(DeclRef.isUsable() && "Builtin reference cannot fail");
310 S.ActOnCallExpr(/*Scope=*/nullptr, DeclRef.get(), Loc, CallArgs, Loc);
312 assert(!Call.isInvalid() && "Call to builtin cannot fail!");
316 static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType,
317 SourceLocation Loc) {
318 QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc);
319 if (CoroHandleType.isNull())
322 DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType);
323 LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc,
324 Sema::LookupOrdinaryName);
325 if (!S.LookupQualifiedName(Found, LookupCtx)) {
326 S.Diag(Loc, diag::err_coroutine_handle_missing_member)
332 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
335 ExprResult FromAddr =
336 S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
337 if (FromAddr.isInvalid())
340 return S.ActOnCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc);
343 struct ReadySuspendResumeResult {
344 enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume };
346 OpaqueValueExpr *OpaqueValue;
350 static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc,
351 StringRef Name, MultiExprArg Args) {
352 DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc);
354 // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&.
356 ExprResult Result = S.BuildMemberReferenceExpr(
357 Base, Base->getType(), Loc, /*IsPtr=*/false, SS,
358 SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr,
360 if (Result.isInvalid())
363 return S.ActOnCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr);
366 // See if return type is coroutine-handle and if so, invoke builtin coro-resume
367 // on its address. This is to enable experimental support for coroutine-handle
368 // returning await_suspend that results in a guranteed tail call to the target
370 static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E,
371 SourceLocation Loc) {
372 if (RetType->isReferenceType())
374 Type const *T = RetType.getTypePtr();
375 if (!T->isClassType() && !T->isStructureType())
378 // FIXME: Add convertability check to coroutine_handle<>. Possibly via
379 // EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment
380 // a private function in SemaExprCXX.cpp
382 ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", None);
383 if (AddressExpr.isInvalid())
386 Expr *JustAddress = AddressExpr.get();
387 // FIXME: Check that the type of AddressExpr is void*
388 return buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_resume,
392 /// Build calls to await_ready, await_suspend, and await_resume for a co_await
394 static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise,
395 SourceLocation Loc, Expr *E) {
396 OpaqueValueExpr *Operand = new (S.Context)
397 OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E);
399 // Assume invalid until we see otherwise.
400 ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/true};
402 ExprResult CoroHandleRes = buildCoroutineHandle(S, CoroPromise->getType(), Loc);
403 if (CoroHandleRes.isInvalid())
405 Expr *CoroHandle = CoroHandleRes.get();
407 const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"};
408 MultiExprArg Args[] = {None, CoroHandle, None};
409 for (size_t I = 0, N = llvm::array_lengthof(Funcs); I != N; ++I) {
410 ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], Args[I]);
411 if (Result.isInvalid())
413 Calls.Results[I] = Result.get();
416 // Assume the calls are valid; all further checking should make them invalid.
417 Calls.IsInvalid = false;
419 using ACT = ReadySuspendResumeResult::AwaitCallType;
420 CallExpr *AwaitReady = cast<CallExpr>(Calls.Results[ACT::ACT_Ready]);
421 if (!AwaitReady->getType()->isDependentType()) {
422 // [expr.await]p3 [...]
423 // — await-ready is the expression e.await_ready(), contextually converted
425 ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady);
426 if (Conv.isInvalid()) {
427 S.Diag(AwaitReady->getDirectCallee()->getLocStart(),
428 diag::note_await_ready_no_bool_conversion);
429 S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
430 << AwaitReady->getDirectCallee() << E->getSourceRange();
431 Calls.IsInvalid = true;
433 Calls.Results[ACT::ACT_Ready] = Conv.get();
435 CallExpr *AwaitSuspend = cast<CallExpr>(Calls.Results[ACT::ACT_Suspend]);
436 if (!AwaitSuspend->getType()->isDependentType()) {
437 // [expr.await]p3 [...]
438 // - await-suspend is the expression e.await_suspend(h), which shall be
439 // a prvalue of type void or bool.
440 QualType RetType = AwaitSuspend->getCallReturnType(S.Context);
442 // Experimental support for coroutine_handle returning await_suspend.
443 if (Expr *TailCallSuspend = maybeTailCall(S, RetType, AwaitSuspend, Loc))
444 Calls.Results[ACT::ACT_Suspend] = TailCallSuspend;
446 // non-class prvalues always have cv-unqualified types
447 if (RetType->isReferenceType() ||
448 (!RetType->isBooleanType() && !RetType->isVoidType())) {
449 S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(),
450 diag::err_await_suspend_invalid_return_type)
452 S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
453 << AwaitSuspend->getDirectCallee();
454 Calls.IsInvalid = true;
462 static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise,
463 SourceLocation Loc, StringRef Name,
466 // Form a reference to the promise.
467 ExprResult PromiseRef = S.BuildDeclRefExpr(
468 Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc);
469 if (PromiseRef.isInvalid())
472 return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args);
475 VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) {
476 assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
477 auto *FD = cast<FunctionDecl>(CurContext);
478 bool IsThisDependentType = [&] {
479 if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(FD))
480 return MD->isInstance() && MD->getThisType(Context)->isDependentType();
485 QualType T = FD->getType()->isDependentType() || IsThisDependentType
486 ? Context.DependentTy
487 : lookupPromiseType(*this, FD, Loc);
491 auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(),
492 &PP.getIdentifierTable().get("__promise"), T,
493 Context.getTrivialTypeSourceInfo(T, Loc), SC_None);
494 CheckVariableDeclarationType(VD);
495 if (VD->isInvalidDecl())
497 ActOnUninitializedDecl(VD);
499 assert(!VD->isInvalidDecl());
503 /// Check that this is a context in which a coroutine suspension can appear.
504 static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc,
506 bool IsImplicit = false) {
507 if (!isValidCoroutineContext(S, Loc, Keyword))
510 assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope");
512 auto *ScopeInfo = S.getCurFunction();
513 assert(ScopeInfo && "missing function scope for function");
515 if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit)
516 ScopeInfo->setFirstCoroutineStmt(Loc, Keyword);
518 if (ScopeInfo->CoroutinePromise)
521 ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc);
522 if (!ScopeInfo->CoroutinePromise)
528 bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc,
530 if (!checkCoroutineContext(*this, KWLoc, Keyword))
532 auto *ScopeInfo = getCurFunction();
533 assert(ScopeInfo->CoroutinePromise);
535 // If we have existing coroutine statements then we have already built
536 // the initial and final suspend points.
537 if (!ScopeInfo->NeedsCoroutineSuspends)
540 ScopeInfo->setNeedsCoroutineSuspends(false);
542 auto *Fn = cast<FunctionDecl>(CurContext);
543 SourceLocation Loc = Fn->getLocation();
544 // Build the initial suspend point
545 auto buildSuspends = [&](StringRef Name) mutable -> StmtResult {
547 buildPromiseCall(*this, ScopeInfo->CoroutinePromise, Loc, Name, None);
548 if (Suspend.isInvalid())
550 Suspend = buildOperatorCoawaitCall(*this, SC, Loc, Suspend.get());
551 if (Suspend.isInvalid())
553 Suspend = BuildResolvedCoawaitExpr(Loc, Suspend.get(),
554 /*IsImplicit*/ true);
555 Suspend = ActOnFinishFullExpr(Suspend.get());
556 if (Suspend.isInvalid()) {
557 Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required)
558 << ((Name == "initial_suspend") ? 0 : 1);
559 Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword;
562 return cast<Stmt>(Suspend.get());
565 StmtResult InitSuspend = buildSuspends("initial_suspend");
566 if (InitSuspend.isInvalid())
569 StmtResult FinalSuspend = buildSuspends("final_suspend");
570 if (FinalSuspend.isInvalid())
573 ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get());
578 ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) {
579 if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) {
580 CorrectDelayedTyposInExpr(E);
584 if (E->getType()->isPlaceholderType()) {
585 ExprResult R = CheckPlaceholderExpr(E);
586 if (R.isInvalid()) return ExprError();
589 ExprResult Lookup = buildOperatorCoawaitLookupExpr(*this, S, Loc);
590 if (Lookup.isInvalid())
592 return BuildUnresolvedCoawaitExpr(Loc, E,
593 cast<UnresolvedLookupExpr>(Lookup.get()));
596 ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *E,
597 UnresolvedLookupExpr *Lookup) {
598 auto *FSI = checkCoroutineContext(*this, Loc, "co_await");
602 if (E->getType()->isPlaceholderType()) {
603 ExprResult R = CheckPlaceholderExpr(E);
609 auto *Promise = FSI->CoroutinePromise;
610 if (Promise->getType()->isDependentType()) {
612 new (Context) DependentCoawaitExpr(Loc, Context.DependentTy, E, Lookup);
616 auto *RD = Promise->getType()->getAsCXXRecordDecl();
617 if (lookupMember(*this, "await_transform", RD, Loc)) {
618 ExprResult R = buildPromiseCall(*this, Promise, Loc, "await_transform", E);
621 diag::note_coroutine_promise_implicit_await_transform_required_here)
622 << E->getSourceRange();
627 ExprResult Awaitable = buildOperatorCoawaitCall(*this, Loc, E, Lookup);
628 if (Awaitable.isInvalid())
631 return BuildResolvedCoawaitExpr(Loc, Awaitable.get());
634 ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *E,
636 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit);
640 if (E->getType()->isPlaceholderType()) {
641 ExprResult R = CheckPlaceholderExpr(E);
642 if (R.isInvalid()) return ExprError();
646 if (E->getType()->isDependentType()) {
647 Expr *Res = new (Context)
648 CoawaitExpr(Loc, Context.DependentTy, E, IsImplicit);
652 // If the expression is a temporary, materialize it as an lvalue so that we
653 // can use it multiple times.
654 if (E->getValueKind() == VK_RValue)
655 E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
657 // Build the await_ready, await_suspend, await_resume calls.
658 ReadySuspendResumeResult RSS =
659 buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E);
664 new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1],
665 RSS.Results[2], RSS.OpaqueValue, IsImplicit);
670 ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) {
671 if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) {
672 CorrectDelayedTyposInExpr(E);
676 // Build yield_value call.
677 ExprResult Awaitable = buildPromiseCall(
678 *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E);
679 if (Awaitable.isInvalid())
682 // Build 'operator co_await' call.
683 Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get());
684 if (Awaitable.isInvalid())
687 return BuildCoyieldExpr(Loc, Awaitable.get());
689 ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) {
690 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield");
694 if (E->getType()->isPlaceholderType()) {
695 ExprResult R = CheckPlaceholderExpr(E);
696 if (R.isInvalid()) return ExprError();
700 if (E->getType()->isDependentType()) {
701 Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, E);
705 // If the expression is a temporary, materialize it as an lvalue so that we
706 // can use it multiple times.
707 if (E->getValueKind() == VK_RValue)
708 E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
710 // Build the await_ready, await_suspend, await_resume calls.
711 ReadySuspendResumeResult RSS =
712 buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E);
717 new (Context) CoyieldExpr(Loc, E, RSS.Results[0], RSS.Results[1],
718 RSS.Results[2], RSS.OpaqueValue);
723 StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) {
724 if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) {
725 CorrectDelayedTyposInExpr(E);
728 return BuildCoreturnStmt(Loc, E);
731 StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E,
733 auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit);
737 if (E && E->getType()->isPlaceholderType() &&
738 !E->getType()->isSpecificPlaceholderType(BuiltinType::Overload)) {
739 ExprResult R = CheckPlaceholderExpr(E);
740 if (R.isInvalid()) return StmtError();
744 // FIXME: If the operand is a reference to a variable that's about to go out
745 // of scope, we should treat the operand as an xvalue for this overload
747 VarDecl *Promise = FSI->CoroutinePromise;
749 if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) {
750 PC = buildPromiseCall(*this, Promise, Loc, "return_value", E);
752 E = MakeFullDiscardedValueExpr(E).get();
753 PC = buildPromiseCall(*this, Promise, Loc, "return_void", None);
758 Expr *PCE = ActOnFinishFullExpr(PC.get()).get();
760 Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit);
764 /// Look up the std::nothrow object.
765 static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) {
766 NamespaceDecl *Std = S.getStdNamespace();
767 assert(Std && "Should already be diagnosed");
769 LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc,
770 Sema::LookupOrdinaryName);
771 if (!S.LookupQualifiedName(Result, Std)) {
772 // FIXME: <experimental/coroutine> should have been included already.
773 // If we require it to include <new> then this diagnostic is no longer
775 S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found);
779 auto *VD = Result.getAsSingle<VarDecl>();
781 Result.suppressDiagnostics();
782 // We found something weird. Complain about the first thing we found.
783 NamedDecl *Found = *Result.begin();
784 S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow);
788 ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc);
795 // Find an appropriate delete for the promise.
796 static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc,
797 QualType PromiseType) {
798 FunctionDecl *OperatorDelete = nullptr;
800 DeclarationName DeleteName =
801 S.Context.DeclarationNames.getCXXOperatorName(OO_Delete);
803 auto *PointeeRD = PromiseType->getAsCXXRecordDecl();
804 assert(PointeeRD && "PromiseType must be a CxxRecordDecl type");
806 if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete))
809 if (!OperatorDelete) {
810 // Look for a global declaration.
811 const bool CanProvideSize = S.isCompleteType(Loc, PromiseType);
812 const bool Overaligned = false;
813 OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize,
814 Overaligned, DeleteName);
816 S.MarkFunctionReferenced(Loc, OperatorDelete);
817 return OperatorDelete;
821 void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) {
822 FunctionScopeInfo *Fn = getCurFunction();
823 assert(Fn && Fn->isCoroutine() && "not a coroutine");
825 assert(FD->isInvalidDecl() &&
826 "a null body is only allowed for invalid declarations");
829 // We have a function that uses coroutine keywords, but we failed to build
831 if (!Fn->CoroutinePromise)
832 return FD->setInvalidDecl();
834 if (isa<CoroutineBodyStmt>(Body)) {
835 // Nothing todo. the body is already a transformed coroutine body statement.
839 // Coroutines [stmt.return]p1:
840 // A return statement shall not appear in a coroutine.
841 if (Fn->FirstReturnLoc.isValid()) {
842 assert(Fn->FirstCoroutineStmtLoc.isValid() &&
843 "first coroutine location not set");
844 Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine);
845 Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
846 << Fn->getFirstCoroutineStmtKeyword();
848 CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body);
849 if (Builder.isInvalid() || !Builder.buildStatements())
850 return FD->setInvalidDecl();
852 // Build body for the coroutine wrapper statement.
853 Body = CoroutineBodyStmt::Create(Context, Builder);
856 CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD,
857 sema::FunctionScopeInfo &Fn,
859 : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()),
860 IsPromiseDependentType(
861 !Fn.CoroutinePromise ||
862 Fn.CoroutinePromise->getType()->isDependentType()) {
864 if (!IsPromiseDependentType) {
865 PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl();
866 assert(PromiseRecordDecl && "Type should have already been checked");
868 this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend();
871 bool CoroutineStmtBuilder::buildStatements() {
872 assert(this->IsValid && "coroutine already invalid");
873 this->IsValid = makeReturnObject() && makeParamMoves();
874 if (this->IsValid && !IsPromiseDependentType)
875 buildDependentStatements();
876 return this->IsValid;
879 bool CoroutineStmtBuilder::buildDependentStatements() {
880 assert(this->IsValid && "coroutine already invalid");
881 assert(!this->IsPromiseDependentType &&
882 "coroutine cannot have a dependent promise type");
883 this->IsValid = makeOnException() && makeOnFallthrough() &&
884 makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() &&
885 makeNewAndDeleteExpr();
886 return this->IsValid;
889 bool CoroutineStmtBuilder::buildParameterMoves() {
890 assert(this->IsValid && "coroutine already invalid");
891 assert(this->ParamMoves.empty() && "param moves already built");
892 return this->IsValid = makeParamMoves();
895 bool CoroutineStmtBuilder::makePromiseStmt() {
896 // Form a declaration statement for the promise declaration, so that AST
897 // visitors can more easily find it.
898 StmtResult PromiseStmt =
899 S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc);
900 if (PromiseStmt.isInvalid())
903 this->Promise = PromiseStmt.get();
907 bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() {
908 if (Fn.hasInvalidCoroutineSuspends())
910 this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first);
911 this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second);
915 static bool diagReturnOnAllocFailure(Sema &S, Expr *E,
916 CXXRecordDecl *PromiseRecordDecl,
917 FunctionScopeInfo &Fn) {
918 auto Loc = E->getExprLoc();
919 if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) {
920 auto *Decl = DeclRef->getDecl();
921 if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) {
922 if (Method->isStatic())
925 Loc = Decl->getLocation();
931 diag::err_coroutine_promise_get_return_object_on_allocation_failure)
932 << PromiseRecordDecl;
933 S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
934 << Fn.getFirstCoroutineStmtKeyword();
938 bool CoroutineStmtBuilder::makeReturnOnAllocFailure() {
939 assert(!IsPromiseDependentType &&
940 "cannot make statement while the promise type is dependent");
942 // [dcl.fct.def.coroutine]/8
943 // The unqualified-id get_return_object_on_allocation_failure is looked up in
944 // the scope of class P by class member access lookup (3.4.5). ...
945 // If an allocation function returns nullptr, ... the coroutine return value
946 // is obtained by a call to ... get_return_object_on_allocation_failure().
949 S.PP.getIdentifierInfo("get_return_object_on_allocation_failure");
950 LookupResult Found(S, DN, Loc, Sema::LookupMemberName);
951 if (!S.LookupQualifiedName(Found, PromiseRecordDecl))
955 ExprResult DeclNameExpr =
956 S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
957 if (DeclNameExpr.isInvalid())
960 if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn))
963 ExprResult ReturnObjectOnAllocationFailure =
964 S.ActOnCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc);
965 if (ReturnObjectOnAllocationFailure.isInvalid())
968 StmtResult ReturnStmt =
969 S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get());
970 if (ReturnStmt.isInvalid()) {
971 S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here)
973 S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
974 << Fn.getFirstCoroutineStmtKeyword();
978 this->ReturnStmtOnAllocFailure = ReturnStmt.get();
982 bool CoroutineStmtBuilder::makeNewAndDeleteExpr() {
983 // Form and check allocation and deallocation calls.
984 assert(!IsPromiseDependentType &&
985 "cannot make statement while the promise type is dependent");
986 QualType PromiseType = Fn.CoroutinePromise->getType();
988 if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type))
991 const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr;
993 // FIXME: Add support for stateful allocators.
995 FunctionDecl *OperatorNew = nullptr;
996 FunctionDecl *OperatorDelete = nullptr;
997 FunctionDecl *UnusedResult = nullptr;
998 bool PassAlignment = false;
999 SmallVector<Expr *, 1> PlacementArgs;
1001 S.FindAllocationFunctions(Loc, SourceRange(),
1002 /*UseGlobal*/ false, PromiseType,
1003 /*isArray*/ false, PassAlignment, PlacementArgs,
1004 OperatorNew, UnusedResult);
1006 bool IsGlobalOverload =
1007 OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext());
1008 // If we didn't find a class-local new declaration and non-throwing new
1009 // was is required then we need to lookup the non-throwing global operator
1011 if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) {
1012 auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc);
1015 PlacementArgs = {StdNoThrow};
1016 OperatorNew = nullptr;
1017 S.FindAllocationFunctions(Loc, SourceRange(),
1018 /*UseGlobal*/ true, PromiseType,
1019 /*isArray*/ false, PassAlignment, PlacementArgs,
1020 OperatorNew, UnusedResult);
1023 assert(OperatorNew && "expected definition of operator new to be found");
1025 if (RequiresNoThrowAlloc) {
1026 const auto *FT = OperatorNew->getType()->getAs<FunctionProtoType>();
1027 if (!FT->isNothrow(S.Context, /*ResultIfDependent*/ false)) {
1028 S.Diag(OperatorNew->getLocation(),
1029 diag::err_coroutine_promise_new_requires_nothrow)
1031 S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
1037 if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr)
1041 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
1044 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_size, {});
1049 S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc);
1050 if (NewRef.isInvalid())
1053 SmallVector<Expr *, 2> NewArgs(1, FrameSize);
1054 for (auto Arg : PlacementArgs)
1055 NewArgs.push_back(Arg);
1057 ExprResult NewExpr =
1058 S.ActOnCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc);
1059 NewExpr = S.ActOnFinishFullExpr(NewExpr.get());
1060 if (NewExpr.isInvalid())
1063 // Make delete call.
1065 QualType OpDeleteQualType = OperatorDelete->getType();
1067 ExprResult DeleteRef =
1068 S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc);
1069 if (DeleteRef.isInvalid())
1073 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_free, {FramePtr});
1075 SmallVector<Expr *, 2> DeleteArgs{CoroFree};
1077 // Check if we need to pass the size.
1078 const auto *OpDeleteType =
1079 OpDeleteQualType.getTypePtr()->getAs<FunctionProtoType>();
1080 if (OpDeleteType->getNumParams() > 1)
1081 DeleteArgs.push_back(FrameSize);
1083 ExprResult DeleteExpr =
1084 S.ActOnCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc);
1085 DeleteExpr = S.ActOnFinishFullExpr(DeleteExpr.get());
1086 if (DeleteExpr.isInvalid())
1089 this->Allocate = NewExpr.get();
1090 this->Deallocate = DeleteExpr.get();
1095 bool CoroutineStmtBuilder::makeOnFallthrough() {
1096 assert(!IsPromiseDependentType &&
1097 "cannot make statement while the promise type is dependent");
1099 // [dcl.fct.def.coroutine]/4
1100 // The unqualified-ids 'return_void' and 'return_value' are looked up in
1101 // the scope of class P. If both are found, the program is ill-formed.
1102 bool HasRVoid, HasRValue;
1103 LookupResult LRVoid =
1104 lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid);
1105 LookupResult LRValue =
1106 lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue);
1108 StmtResult Fallthrough;
1109 if (HasRVoid && HasRValue) {
1110 // FIXME Improve this diagnostic
1111 S.Diag(FD.getLocation(),
1112 diag::err_coroutine_promise_incompatible_return_functions)
1113 << PromiseRecordDecl;
1114 S.Diag(LRVoid.getRepresentativeDecl()->getLocation(),
1115 diag::note_member_first_declared_here)
1116 << LRVoid.getLookupName();
1117 S.Diag(LRValue.getRepresentativeDecl()->getLocation(),
1118 diag::note_member_first_declared_here)
1119 << LRValue.getLookupName();
1121 } else if (!HasRVoid && !HasRValue) {
1122 // FIXME: The PDTS currently specifies this case as UB, not ill-formed.
1123 // However we still diagnose this as an error since until the PDTS is fixed.
1124 S.Diag(FD.getLocation(),
1125 diag::err_coroutine_promise_requires_return_function)
1126 << PromiseRecordDecl;
1127 S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
1128 << PromiseRecordDecl;
1130 } else if (HasRVoid) {
1131 // If the unqualified-id return_void is found, flowing off the end of a
1132 // coroutine is equivalent to a co_return with no operand. Otherwise,
1133 // flowing off the end of a coroutine results in undefined behavior.
1134 Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr,
1135 /*IsImplicit*/false);
1136 Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get());
1137 if (Fallthrough.isInvalid())
1141 this->OnFallthrough = Fallthrough.get();
1145 bool CoroutineStmtBuilder::makeOnException() {
1146 // Try to form 'p.unhandled_exception();'
1147 assert(!IsPromiseDependentType &&
1148 "cannot make statement while the promise type is dependent");
1150 const bool RequireUnhandledException = S.getLangOpts().CXXExceptions;
1152 if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) {
1154 RequireUnhandledException
1155 ? diag::err_coroutine_promise_unhandled_exception_required
1157 warn_coroutine_promise_unhandled_exception_required_with_exceptions;
1158 S.Diag(Loc, DiagID) << PromiseRecordDecl;
1159 S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
1160 << PromiseRecordDecl;
1161 return !RequireUnhandledException;
1164 // If exceptions are disabled, don't try to build OnException.
1165 if (!S.getLangOpts().CXXExceptions)
1168 ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc,
1169 "unhandled_exception", None);
1170 UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc);
1171 if (UnhandledException.isInvalid())
1174 // Since the body of the coroutine will be wrapped in try-catch, it will
1175 // be incompatible with SEH __try if present in a function.
1176 if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) {
1177 S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions);
1178 S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1179 << Fn.getFirstCoroutineStmtKeyword();
1183 this->OnException = UnhandledException.get();
1187 bool CoroutineStmtBuilder::makeReturnObject() {
1188 // Build implicit 'p.get_return_object()' expression and form initialization
1189 // of return type from it.
1190 ExprResult ReturnObject =
1191 buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None);
1192 if (ReturnObject.isInvalid())
1195 this->ReturnValue = ReturnObject.get();
1199 static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) {
1200 if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) {
1201 auto *MethodDecl = MbrRef->getMethodDecl();
1202 S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here)
1205 S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1206 << Fn.getFirstCoroutineStmtKeyword();
1209 bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
1210 assert(!IsPromiseDependentType &&
1211 "cannot make statement while the promise type is dependent");
1212 assert(this->ReturnValue && "ReturnValue must be already formed");
1214 QualType const GroType = this->ReturnValue->getType();
1215 assert(!GroType->isDependentType() &&
1216 "get_return_object type must no longer be dependent");
1218 QualType const FnRetType = FD.getReturnType();
1219 assert(!FnRetType->isDependentType() &&
1220 "get_return_object type must no longer be dependent");
1222 if (FnRetType->isVoidType()) {
1223 ExprResult Res = S.ActOnFinishFullExpr(this->ReturnValue, Loc);
1224 if (Res.isInvalid())
1227 this->ResultDecl = Res.get();
1231 if (GroType->isVoidType()) {
1232 // Trigger a nice error message.
1233 InitializedEntity Entity =
1234 InitializedEntity::InitializeResult(Loc, FnRetType, false);
1235 S.PerformMoveOrCopyInitialization(Entity, nullptr, FnRetType, ReturnValue);
1236 noteMemberDeclaredHere(S, ReturnValue, Fn);
1240 auto *GroDecl = VarDecl::Create(
1241 S.Context, &FD, FD.getLocation(), FD.getLocation(),
1242 &S.PP.getIdentifierTable().get("__coro_gro"), GroType,
1243 S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None);
1245 S.CheckVariableDeclarationType(GroDecl);
1246 if (GroDecl->isInvalidDecl())
1249 InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl);
1250 ExprResult Res = S.PerformMoveOrCopyInitialization(Entity, nullptr, GroType,
1252 if (Res.isInvalid())
1255 Res = S.ActOnFinishFullExpr(Res.get());
1256 if (Res.isInvalid())
1259 if (GroType == FnRetType) {
1260 GroDecl->setNRVOVariable(true);
1263 S.AddInitializerToDecl(GroDecl, Res.get(),
1264 /*DirectInit=*/false);
1266 S.FinalizeDeclaration(GroDecl);
1268 // Form a declaration statement for the return declaration, so that AST
1269 // visitors can more easily find it.
1270 StmtResult GroDeclStmt =
1271 S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(GroDecl), Loc, Loc);
1272 if (GroDeclStmt.isInvalid())
1275 this->ResultDecl = GroDeclStmt.get();
1277 ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc);
1278 if (declRef.isInvalid())
1281 StmtResult ReturnStmt = S.BuildReturnStmt(Loc, declRef.get());
1282 if (ReturnStmt.isInvalid()) {
1283 noteMemberDeclaredHere(S, ReturnValue, Fn);
1287 this->ReturnStmt = ReturnStmt.get();
1291 // Create a static_cast\<T&&>(expr).
1292 static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) {
1295 QualType TargetType = S.BuildReferenceType(
1296 T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName());
1297 SourceLocation ExprLoc = E->getLocStart();
1298 TypeSourceInfo *TargetLoc =
1299 S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc);
1302 .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E,
1303 SourceRange(ExprLoc, ExprLoc), E->getSourceRange())
1308 /// \brief Build a variable declaration for move parameter.
1309 static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type,
1310 IdentifierInfo *II) {
1311 TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc);
1313 VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type, TInfo, SC_None);
1314 Decl->setImplicit();
1318 bool CoroutineStmtBuilder::makeParamMoves() {
1319 for (auto *paramDecl : FD.parameters()) {
1320 auto Ty = paramDecl->getType();
1321 if (Ty->isDependentType())
1324 // No need to copy scalars, llvm will take care of them.
1325 if (Ty->getAsCXXRecordDecl()) {
1326 ExprResult ParamRef =
1327 S.BuildDeclRefExpr(paramDecl, paramDecl->getType(),
1328 ExprValueKind::VK_LValue, Loc); // FIXME: scope?
1329 if (ParamRef.isInvalid())
1332 Expr *RCast = castForMoving(S, ParamRef.get());
1334 auto D = buildVarDecl(S, Loc, Ty, paramDecl->getIdentifier());
1335 S.AddInitializerToDecl(D, RCast, /*DirectInit=*/true);
1337 // Convert decl to a statement.
1338 StmtResult Stmt = S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(D), Loc, Loc);
1339 if (Stmt.isInvalid())
1342 ParamMovesVector.push_back(Stmt.get());
1346 // Convert to ArrayRef in CtorArgs structure that builder inherits from.
1347 ParamMoves = ParamMovesVector;
1351 StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) {
1352 CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args);