1 //===-- SemaCoroutine.cpp - Semantic Analysis for Coroutines --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements semantic analysis for C++ Coroutines.
11 // This file contains references to sections of the Coroutines TS, which
12 // can be found at http://wg21.link/coroutines.
14 //===----------------------------------------------------------------------===//
16 #include "CoroutineStmtBuilder.h"
17 #include "clang/AST/ASTLambda.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/Basic/Builtins.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Initialization.h"
24 #include "clang/Sema/Overload.h"
25 #include "clang/Sema/ScopeInfo.h"
26 #include "clang/Sema/SemaInternal.h"
27 #include "llvm/ADT/SmallSet.h"
29 using namespace clang
;
32 static LookupResult
lookupMember(Sema
&S
, const char *Name
, CXXRecordDecl
*RD
,
33 SourceLocation Loc
, bool &Res
) {
34 DeclarationName DN
= S
.PP
.getIdentifierInfo(Name
);
35 LookupResult
LR(S
, DN
, Loc
, Sema::LookupMemberName
);
36 // Suppress diagnostics when a private member is selected. The same warnings
37 // will be produced again when building the call.
38 LR
.suppressDiagnostics();
39 Res
= S
.LookupQualifiedName(LR
, RD
);
43 static bool lookupMember(Sema
&S
, const char *Name
, CXXRecordDecl
*RD
,
46 lookupMember(S
, Name
, RD
, Loc
, Res
);
50 /// Look up the std::coroutine_traits<...>::promise_type for the given
52 static QualType
lookupPromiseType(Sema
&S
, const FunctionDecl
*FD
,
53 SourceLocation KwLoc
) {
54 const FunctionProtoType
*FnType
= FD
->getType()->castAs
<FunctionProtoType
>();
55 const SourceLocation FuncLoc
= FD
->getLocation();
57 NamespaceDecl
*CoroNamespace
= nullptr;
58 ClassTemplateDecl
*CoroTraits
=
59 S
.lookupCoroutineTraits(KwLoc
, FuncLoc
, CoroNamespace
);
64 // Form template argument list for coroutine_traits<R, P1, P2, ...> according
65 // to [dcl.fct.def.coroutine]3
66 TemplateArgumentListInfo
Args(KwLoc
, KwLoc
);
67 auto AddArg
= [&](QualType T
) {
68 Args
.addArgument(TemplateArgumentLoc(
69 TemplateArgument(T
), S
.Context
.getTrivialTypeSourceInfo(T
, KwLoc
)));
71 AddArg(FnType
->getReturnType());
72 // If the function is a non-static member function, add the type
73 // of the implicit object parameter before the formal parameters.
74 if (auto *MD
= dyn_cast
<CXXMethodDecl
>(FD
)) {
75 if (MD
->isInstance()) {
76 // [over.match.funcs]4
77 // For non-static member functions, the type of the implicit object
79 // -- "lvalue reference to cv X" for functions declared without a
80 // ref-qualifier or with the & ref-qualifier
81 // -- "rvalue reference to cv X" for functions declared with the &&
83 QualType T
= MD
->getThisType()->castAs
<PointerType
>()->getPointeeType();
84 T
= FnType
->getRefQualifier() == RQ_RValue
85 ? S
.Context
.getRValueReferenceType(T
)
86 : S
.Context
.getLValueReferenceType(T
, /*SpelledAsLValue*/ true);
90 for (QualType T
: FnType
->getParamTypes())
93 // Build the template-id.
95 S
.CheckTemplateIdType(TemplateName(CoroTraits
), KwLoc
, Args
);
96 if (CoroTrait
.isNull())
98 if (S
.RequireCompleteType(KwLoc
, CoroTrait
,
99 diag::err_coroutine_type_missing_specialization
))
102 auto *RD
= CoroTrait
->getAsCXXRecordDecl();
103 assert(RD
&& "specialization of class template is not a class?");
105 // Look up the ::promise_type member.
106 LookupResult
R(S
, &S
.PP
.getIdentifierTable().get("promise_type"), KwLoc
,
107 Sema::LookupOrdinaryName
);
108 S
.LookupQualifiedName(R
, RD
);
109 auto *Promise
= R
.getAsSingle
<TypeDecl
>();
112 diag::err_implied_std_coroutine_traits_promise_type_not_found
)
116 // The promise type is required to be a class type.
117 QualType PromiseType
= S
.Context
.getTypeDeclType(Promise
);
119 auto buildElaboratedType
= [&]() {
120 auto *NNS
= NestedNameSpecifier::Create(S
.Context
, nullptr, CoroNamespace
);
121 NNS
= NestedNameSpecifier::Create(S
.Context
, NNS
, false,
122 CoroTrait
.getTypePtr());
123 return S
.Context
.getElaboratedType(ETK_None
, NNS
, PromiseType
);
126 if (!PromiseType
->getAsCXXRecordDecl()) {
128 diag::err_implied_std_coroutine_traits_promise_type_not_class
)
129 << buildElaboratedType();
132 if (S
.RequireCompleteType(FuncLoc
, buildElaboratedType(),
133 diag::err_coroutine_promise_type_incomplete
))
139 /// Look up the std::coroutine_handle<PromiseType>.
140 static QualType
lookupCoroutineHandleType(Sema
&S
, QualType PromiseType
,
141 SourceLocation Loc
) {
142 if (PromiseType
.isNull())
145 NamespaceDecl
*CoroNamespace
= S
.getCachedCoroNamespace();
146 assert(CoroNamespace
&& "Should already be diagnosed");
148 LookupResult
Result(S
, &S
.PP
.getIdentifierTable().get("coroutine_handle"),
149 Loc
, Sema::LookupOrdinaryName
);
150 if (!S
.LookupQualifiedName(Result
, CoroNamespace
)) {
151 S
.Diag(Loc
, diag::err_implied_coroutine_type_not_found
)
152 << "std::coroutine_handle";
156 ClassTemplateDecl
*CoroHandle
= Result
.getAsSingle
<ClassTemplateDecl
>();
158 Result
.suppressDiagnostics();
159 // We found something weird. Complain about the first thing we found.
160 NamedDecl
*Found
= *Result
.begin();
161 S
.Diag(Found
->getLocation(), diag::err_malformed_std_coroutine_handle
);
165 // Form template argument list for coroutine_handle<Promise>.
166 TemplateArgumentListInfo
Args(Loc
, Loc
);
167 Args
.addArgument(TemplateArgumentLoc(
168 TemplateArgument(PromiseType
),
169 S
.Context
.getTrivialTypeSourceInfo(PromiseType
, Loc
)));
171 // Build the template-id.
172 QualType CoroHandleType
=
173 S
.CheckTemplateIdType(TemplateName(CoroHandle
), Loc
, Args
);
174 if (CoroHandleType
.isNull())
176 if (S
.RequireCompleteType(Loc
, CoroHandleType
,
177 diag::err_coroutine_type_missing_specialization
))
180 return CoroHandleType
;
183 static bool isValidCoroutineContext(Sema
&S
, SourceLocation Loc
,
185 // [expr.await]p2 dictates that 'co_await' and 'co_yield' must be used within
187 // FIXME: This also covers [expr.await]p2: "An await-expression shall not
188 // appear in a default argument." But the diagnostic QoI here could be
189 // improved to inform the user that default arguments specifically are not
191 auto *FD
= dyn_cast
<FunctionDecl
>(S
.CurContext
);
193 S
.Diag(Loc
, isa
<ObjCMethodDecl
>(S
.CurContext
)
194 ? diag::err_coroutine_objc_method
195 : diag::err_coroutine_outside_function
) << Keyword
;
199 // An enumeration for mapping the diagnostic type to the correct diagnostic
201 enum InvalidFuncDiag
{
210 bool Diagnosed
= false;
211 auto DiagInvalid
= [&](InvalidFuncDiag ID
) {
212 S
.Diag(Loc
, diag::err_coroutine_invalid_func_context
) << ID
<< Keyword
;
217 // Diagnose when a constructor, destructor
218 // or the function 'main' are declared as a coroutine.
219 auto *MD
= dyn_cast
<CXXMethodDecl
>(FD
);
220 // [class.ctor]p11: "A constructor shall not be a coroutine."
221 if (MD
&& isa
<CXXConstructorDecl
>(MD
))
222 return DiagInvalid(DiagCtor
);
223 // [class.dtor]p17: "A destructor shall not be a coroutine."
224 else if (MD
&& isa
<CXXDestructorDecl
>(MD
))
225 return DiagInvalid(DiagDtor
);
226 // [basic.start.main]p3: "The function main shall not be a coroutine."
227 else if (FD
->isMain())
228 return DiagInvalid(DiagMain
);
230 // Emit a diagnostics for each of the following conditions which is not met.
231 // [expr.const]p2: "An expression e is a core constant expression unless the
232 // evaluation of e [...] would evaluate one of the following expressions:
233 // [...] an await-expression [...] a yield-expression."
234 if (FD
->isConstexpr())
235 DiagInvalid(FD
->isConsteval() ? DiagConsteval
: DiagConstexpr
);
236 // [dcl.spec.auto]p15: "A function declared with a return type that uses a
237 // placeholder type shall not be a coroutine."
238 if (FD
->getReturnType()->isUndeducedType())
239 DiagInvalid(DiagAutoRet
);
240 // [dcl.fct.def.coroutine]p1
241 // The parameter-declaration-clause of the coroutine shall not terminate with
242 // an ellipsis that is not part of a parameter-declaration.
243 if (FD
->isVariadic())
244 DiagInvalid(DiagVarargs
);
249 /// Build a call to 'operator co_await' if there is a suitable operator for
250 /// the given expression.
251 ExprResult
Sema::BuildOperatorCoawaitCall(SourceLocation Loc
, Expr
*E
,
252 UnresolvedLookupExpr
*Lookup
) {
253 UnresolvedSet
<16> Functions
;
254 Functions
.append(Lookup
->decls_begin(), Lookup
->decls_end());
255 return CreateOverloadedUnaryOp(Loc
, UO_Coawait
, Functions
, E
);
258 static ExprResult
buildOperatorCoawaitCall(Sema
&SemaRef
, Scope
*S
,
259 SourceLocation Loc
, Expr
*E
) {
260 ExprResult R
= SemaRef
.BuildOperatorCoawaitLookupExpr(S
, Loc
);
263 return SemaRef
.BuildOperatorCoawaitCall(Loc
, E
,
264 cast
<UnresolvedLookupExpr
>(R
.get()));
267 static ExprResult
buildCoroutineHandle(Sema
&S
, QualType PromiseType
,
268 SourceLocation Loc
) {
269 QualType CoroHandleType
= lookupCoroutineHandleType(S
, PromiseType
, Loc
);
270 if (CoroHandleType
.isNull())
273 DeclContext
*LookupCtx
= S
.computeDeclContext(CoroHandleType
);
274 LookupResult
Found(S
, &S
.PP
.getIdentifierTable().get("from_address"), Loc
,
275 Sema::LookupOrdinaryName
);
276 if (!S
.LookupQualifiedName(Found
, LookupCtx
)) {
277 S
.Diag(Loc
, diag::err_coroutine_handle_missing_member
)
283 S
.BuildBuiltinCallExpr(Loc
, Builtin::BI__builtin_coro_frame
, {});
286 ExprResult FromAddr
=
287 S
.BuildDeclarationNameExpr(SS
, Found
, /*NeedsADL=*/false);
288 if (FromAddr
.isInvalid())
291 return S
.BuildCallExpr(nullptr, FromAddr
.get(), Loc
, FramePtr
, Loc
);
294 struct ReadySuspendResumeResult
{
295 enum AwaitCallType
{ ACT_Ready
, ACT_Suspend
, ACT_Resume
};
297 OpaqueValueExpr
*OpaqueValue
;
301 static ExprResult
buildMemberCall(Sema
&S
, Expr
*Base
, SourceLocation Loc
,
302 StringRef Name
, MultiExprArg Args
) {
303 DeclarationNameInfo
NameInfo(&S
.PP
.getIdentifierTable().get(Name
), Loc
);
305 // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&.
307 ExprResult Result
= S
.BuildMemberReferenceExpr(
308 Base
, Base
->getType(), Loc
, /*IsPtr=*/false, SS
,
309 SourceLocation(), nullptr, NameInfo
, /*TemplateArgs=*/nullptr,
311 if (Result
.isInvalid())
314 // We meant exactly what we asked for. No need for typo correction.
315 if (auto *TE
= dyn_cast
<TypoExpr
>(Result
.get())) {
316 S
.clearDelayedTypo(TE
);
317 S
.Diag(Loc
, diag::err_no_member
)
318 << NameInfo
.getName() << Base
->getType()->getAsCXXRecordDecl()
319 << Base
->getSourceRange();
323 return S
.BuildCallExpr(nullptr, Result
.get(), Loc
, Args
, Loc
, nullptr);
326 // See if return type is coroutine-handle and if so, invoke builtin coro-resume
327 // on its address. This is to enable experimental support for coroutine-handle
328 // returning await_suspend that results in a guaranteed tail call to the target
330 static Expr
*maybeTailCall(Sema
&S
, QualType RetType
, Expr
*E
,
331 SourceLocation Loc
) {
332 if (RetType
->isReferenceType())
334 Type
const *T
= RetType
.getTypePtr();
335 if (!T
->isClassType() && !T
->isStructureType())
338 // FIXME: Add convertability check to coroutine_handle<>. Possibly via
339 // EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment
340 // a private function in SemaExprCXX.cpp
342 ExprResult AddressExpr
= buildMemberCall(S
, E
, Loc
, "address", None
);
343 if (AddressExpr
.isInvalid())
346 Expr
*JustAddress
= AddressExpr
.get();
348 // Check that the type of AddressExpr is void*
349 if (!JustAddress
->getType().getTypePtr()->isVoidPointerType())
350 S
.Diag(cast
<CallExpr
>(JustAddress
)->getCalleeDecl()->getLocation(),
351 diag::warn_coroutine_handle_address_invalid_return_type
)
352 << JustAddress
->getType();
354 // Clean up temporary objects so that they don't live across suspension points
355 // unnecessarily. We choose to clean up before the call to
356 // __builtin_coro_resume so that the cleanup code are not inserted in-between
357 // the resume call and return instruction, which would interfere with the
358 // musttail call contract.
359 JustAddress
= S
.MaybeCreateExprWithCleanups(JustAddress
);
360 return S
.BuildBuiltinCallExpr(Loc
, Builtin::BI__builtin_coro_resume
,
364 /// Build calls to await_ready, await_suspend, and await_resume for a co_await
366 /// The generated AST tries to clean up temporary objects as early as
367 /// possible so that they don't live across suspension points if possible.
368 /// Having temporary objects living across suspension points unnecessarily can
369 /// lead to large frame size, and also lead to memory corruptions if the
370 /// coroutine frame is destroyed after coming back from suspension. This is done
371 /// by wrapping both the await_ready call and the await_suspend call with
372 /// ExprWithCleanups. In the end of this function, we also need to explicitly
373 /// set cleanup state so that the CoawaitExpr is also wrapped with an
374 /// ExprWithCleanups to clean up the awaiter associated with the co_await
376 static ReadySuspendResumeResult
buildCoawaitCalls(Sema
&S
, VarDecl
*CoroPromise
,
377 SourceLocation Loc
, Expr
*E
) {
378 OpaqueValueExpr
*Operand
= new (S
.Context
)
379 OpaqueValueExpr(Loc
, E
->getType(), VK_LValue
, E
->getObjectKind(), E
);
381 // Assume valid until we see otherwise.
382 // Further operations are responsible for setting IsInalid to true.
383 ReadySuspendResumeResult Calls
= {{}, Operand
, /*IsInvalid=*/false};
385 using ACT
= ReadySuspendResumeResult::AwaitCallType
;
387 auto BuildSubExpr
= [&](ACT CallType
, StringRef Func
,
388 MultiExprArg Arg
) -> Expr
* {
389 ExprResult Result
= buildMemberCall(S
, Operand
, Loc
, Func
, Arg
);
390 if (Result
.isInvalid()) {
391 Calls
.IsInvalid
= true;
394 Calls
.Results
[CallType
] = Result
.get();
398 CallExpr
*AwaitReady
=
399 cast_or_null
<CallExpr
>(BuildSubExpr(ACT::ACT_Ready
, "await_ready", None
));
402 if (!AwaitReady
->getType()->isDependentType()) {
403 // [expr.await]p3 [...]
404 // — await-ready is the expression e.await_ready(), contextually converted
406 ExprResult Conv
= S
.PerformContextuallyConvertToBool(AwaitReady
);
407 if (Conv
.isInvalid()) {
408 S
.Diag(AwaitReady
->getDirectCallee()->getBeginLoc(),
409 diag::note_await_ready_no_bool_conversion
);
410 S
.Diag(Loc
, diag::note_coroutine_promise_call_implicitly_required
)
411 << AwaitReady
->getDirectCallee() << E
->getSourceRange();
412 Calls
.IsInvalid
= true;
414 Calls
.Results
[ACT::ACT_Ready
] = S
.MaybeCreateExprWithCleanups(Conv
.get());
417 ExprResult CoroHandleRes
=
418 buildCoroutineHandle(S
, CoroPromise
->getType(), Loc
);
419 if (CoroHandleRes
.isInvalid()) {
420 Calls
.IsInvalid
= true;
423 Expr
*CoroHandle
= CoroHandleRes
.get();
424 CallExpr
*AwaitSuspend
= cast_or_null
<CallExpr
>(
425 BuildSubExpr(ACT::ACT_Suspend
, "await_suspend", CoroHandle
));
428 if (!AwaitSuspend
->getType()->isDependentType()) {
429 // [expr.await]p3 [...]
430 // - await-suspend is the expression e.await_suspend(h), which shall be
431 // a prvalue of type void, bool, or std::coroutine_handle<Z> for some
433 QualType RetType
= AwaitSuspend
->getCallReturnType(S
.Context
);
435 // Experimental support for coroutine_handle returning await_suspend.
436 if (Expr
*TailCallSuspend
=
437 maybeTailCall(S
, RetType
, AwaitSuspend
, Loc
))
438 // Note that we don't wrap the expression with ExprWithCleanups here
439 // because that might interfere with tailcall contract (e.g. inserting
440 // clean up instructions in-between tailcall and return). Instead
441 // ExprWithCleanups is wrapped within maybeTailCall() prior to the resume
443 Calls
.Results
[ACT::ACT_Suspend
] = TailCallSuspend
;
445 // non-class prvalues always have cv-unqualified types
446 if (RetType
->isReferenceType() ||
447 (!RetType
->isBooleanType() && !RetType
->isVoidType())) {
448 S
.Diag(AwaitSuspend
->getCalleeDecl()->getLocation(),
449 diag::err_await_suspend_invalid_return_type
)
451 S
.Diag(Loc
, diag::note_coroutine_promise_call_implicitly_required
)
452 << AwaitSuspend
->getDirectCallee();
453 Calls
.IsInvalid
= true;
455 Calls
.Results
[ACT::ACT_Suspend
] =
456 S
.MaybeCreateExprWithCleanups(AwaitSuspend
);
460 BuildSubExpr(ACT::ACT_Resume
, "await_resume", None
);
462 // Make sure the awaiter object gets a chance to be cleaned up.
463 S
.Cleanup
.setExprNeedsCleanups(true);
468 static ExprResult
buildPromiseCall(Sema
&S
, VarDecl
*Promise
,
469 SourceLocation Loc
, StringRef Name
,
472 // Form a reference to the promise.
473 ExprResult PromiseRef
= S
.BuildDeclRefExpr(
474 Promise
, Promise
->getType().getNonReferenceType(), VK_LValue
, Loc
);
475 if (PromiseRef
.isInvalid())
478 return buildMemberCall(S
, PromiseRef
.get(), Loc
, Name
, Args
);
481 VarDecl
*Sema::buildCoroutinePromise(SourceLocation Loc
) {
482 assert(isa
<FunctionDecl
>(CurContext
) && "not in a function scope");
483 auto *FD
= cast
<FunctionDecl
>(CurContext
);
484 bool IsThisDependentType
= [&] {
485 if (auto *MD
= dyn_cast_or_null
<CXXMethodDecl
>(FD
))
486 return MD
->isInstance() && MD
->getThisType()->isDependentType();
491 QualType T
= FD
->getType()->isDependentType() || IsThisDependentType
492 ? Context
.DependentTy
493 : lookupPromiseType(*this, FD
, Loc
);
497 auto *VD
= VarDecl::Create(Context
, FD
, FD
->getLocation(), FD
->getLocation(),
498 &PP
.getIdentifierTable().get("__promise"), T
,
499 Context
.getTrivialTypeSourceInfo(T
, Loc
), SC_None
);
501 CheckVariableDeclarationType(VD
);
502 if (VD
->isInvalidDecl())
505 auto *ScopeInfo
= getCurFunction();
507 // Build a list of arguments, based on the coroutine function's arguments,
508 // that if present will be passed to the promise type's constructor.
509 llvm::SmallVector
<Expr
*, 4> CtorArgExprs
;
511 // Add implicit object parameter.
512 if (auto *MD
= dyn_cast
<CXXMethodDecl
>(FD
)) {
513 if (MD
->isInstance() && !isLambdaCallOperator(MD
)) {
514 ExprResult ThisExpr
= ActOnCXXThis(Loc
);
515 if (ThisExpr
.isInvalid())
517 ThisExpr
= CreateBuiltinUnaryOp(Loc
, UO_Deref
, ThisExpr
.get());
518 if (ThisExpr
.isInvalid())
520 CtorArgExprs
.push_back(ThisExpr
.get());
524 // Add the coroutine function's parameters.
525 auto &Moves
= ScopeInfo
->CoroutineParameterMoves
;
526 for (auto *PD
: FD
->parameters()) {
527 if (PD
->getType()->isDependentType())
530 auto RefExpr
= ExprEmpty();
531 auto Move
= Moves
.find(PD
);
532 assert(Move
!= Moves
.end() &&
533 "Coroutine function parameter not inserted into move map");
534 // If a reference to the function parameter exists in the coroutine
535 // frame, use that reference.
537 cast
<VarDecl
>(cast
<DeclStmt
>(Move
->second
)->getSingleDecl());
539 BuildDeclRefExpr(MoveDecl
, MoveDecl
->getType().getNonReferenceType(),
540 ExprValueKind::VK_LValue
, FD
->getLocation());
541 if (RefExpr
.isInvalid())
543 CtorArgExprs
.push_back(RefExpr
.get());
546 // If we have a non-zero number of constructor arguments, try to use them.
547 // Otherwise, fall back to the promise type's default constructor.
548 if (!CtorArgExprs
.empty()) {
549 // Create an initialization sequence for the promise type using the
550 // constructor arguments, wrapped in a parenthesized list expression.
551 Expr
*PLE
= ParenListExpr::Create(Context
, FD
->getLocation(),
552 CtorArgExprs
, FD
->getLocation());
553 InitializedEntity Entity
= InitializedEntity::InitializeVariable(VD
);
554 InitializationKind Kind
= InitializationKind::CreateForInit(
555 VD
->getLocation(), /*DirectInit=*/true, PLE
);
556 InitializationSequence
InitSeq(*this, Entity
, Kind
, CtorArgExprs
,
557 /*TopLevelOfInitList=*/false,
558 /*TreatUnavailableAsInvalid=*/false);
560 // [dcl.fct.def.coroutine]5.7
561 // promise-constructor-arguments is determined as follows: overload
562 // resolution is performed on a promise constructor call created by
563 // assembling an argument list q_1 ... q_n . If a viable constructor is
564 // found ([over.match.viable]), then promise-constructor-arguments is ( q_1
565 // , ..., q_n ), otherwise promise-constructor-arguments is empty.
567 ExprResult Result
= InitSeq
.Perform(*this, Entity
, Kind
, CtorArgExprs
);
568 if (Result
.isInvalid()) {
569 VD
->setInvalidDecl();
570 } else if (Result
.get()) {
571 VD
->setInit(MaybeCreateExprWithCleanups(Result
.get()));
572 VD
->setInitStyle(VarDecl::CallInit
);
573 CheckCompleteVariableDeclaration(VD
);
576 ActOnUninitializedDecl(VD
);
578 ActOnUninitializedDecl(VD
);
584 /// Check that this is a context in which a coroutine suspension can appear.
585 static FunctionScopeInfo
*checkCoroutineContext(Sema
&S
, SourceLocation Loc
,
587 bool IsImplicit
= false) {
588 if (!isValidCoroutineContext(S
, Loc
, Keyword
))
591 assert(isa
<FunctionDecl
>(S
.CurContext
) && "not in a function scope");
593 auto *ScopeInfo
= S
.getCurFunction();
594 assert(ScopeInfo
&& "missing function scope for function");
596 if (ScopeInfo
->FirstCoroutineStmtLoc
.isInvalid() && !IsImplicit
)
597 ScopeInfo
->setFirstCoroutineStmt(Loc
, Keyword
);
599 if (ScopeInfo
->CoroutinePromise
)
602 if (!S
.buildCoroutineParameterMoves(Loc
))
605 ScopeInfo
->CoroutinePromise
= S
.buildCoroutinePromise(Loc
);
606 if (!ScopeInfo
->CoroutinePromise
)
612 /// Recursively check \p E and all its children to see if any call target
613 /// (including constructor call) is declared noexcept. Also any value returned
614 /// from the call has a noexcept destructor.
615 static void checkNoThrow(Sema
&S
, const Stmt
*E
,
616 llvm::SmallPtrSetImpl
<const Decl
*> &ThrowingDecls
) {
617 auto checkDeclNoexcept
= [&](const Decl
*D
, bool IsDtor
= false) {
618 // In the case of dtor, the call to dtor is implicit and hence we should
619 // pass nullptr to canCalleeThrow.
620 if (Sema::canCalleeThrow(S
, IsDtor
? nullptr : cast
<Expr
>(E
), D
)) {
621 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
622 // co_await promise.final_suspend() could end up calling
623 // __builtin_coro_resume for symmetric transfer if await_suspend()
624 // returns a handle. In that case, even __builtin_coro_resume is not
625 // declared as noexcept and may throw, it does not throw _into_ the
626 // coroutine that just suspended, but rather throws back out from
627 // whoever called coroutine_handle::resume(), hence we claim that
628 // logically it does not throw.
629 if (FD
->getBuiltinID() == Builtin::BI__builtin_coro_resume
)
632 if (ThrowingDecls
.empty()) {
633 // [dcl.fct.def.coroutine]p15
634 // The expression co_await promise.final_suspend() shall not be
635 // potentially-throwing ([except.spec]).
637 // First time seeing an error, emit the error message.
638 S
.Diag(cast
<FunctionDecl
>(S
.CurContext
)->getLocation(),
639 diag::err_coroutine_promise_final_suspend_requires_nothrow
);
641 ThrowingDecls
.insert(D
);
645 if (auto *CE
= dyn_cast
<CXXConstructExpr
>(E
)) {
646 CXXConstructorDecl
*Ctor
= CE
->getConstructor();
647 checkDeclNoexcept(Ctor
);
648 // Check the corresponding destructor of the constructor.
649 checkDeclNoexcept(Ctor
->getParent()->getDestructor(), /*IsDtor=*/true);
650 } else if (auto *CE
= dyn_cast
<CallExpr
>(E
)) {
651 if (CE
->isTypeDependent())
654 checkDeclNoexcept(CE
->getCalleeDecl());
655 QualType ReturnType
= CE
->getCallReturnType(S
.getASTContext());
656 // Check the destructor of the call return type, if any.
657 if (ReturnType
.isDestructedType() ==
658 QualType::DestructionKind::DK_cxx_destructor
) {
660 cast
<RecordType
>(ReturnType
.getCanonicalType().getTypePtr());
661 checkDeclNoexcept(cast
<CXXRecordDecl
>(T
->getDecl())->getDestructor(),
665 for (const auto *Child
: E
->children()) {
668 checkNoThrow(S
, Child
, ThrowingDecls
);
672 bool Sema::checkFinalSuspendNoThrow(const Stmt
*FinalSuspend
) {
673 llvm::SmallPtrSet
<const Decl
*, 4> ThrowingDecls
;
674 // We first collect all declarations that should not throw but not declared
675 // with noexcept. We then sort them based on the location before printing.
676 // This is to avoid emitting the same note multiple times on the same
677 // declaration, and also provide a deterministic order for the messages.
678 checkNoThrow(*this, FinalSuspend
, ThrowingDecls
);
679 auto SortedDecls
= llvm::SmallVector
<const Decl
*, 4>{ThrowingDecls
.begin(),
680 ThrowingDecls
.end()};
681 sort(SortedDecls
, [](const Decl
*A
, const Decl
*B
) {
682 return A
->getEndLoc() < B
->getEndLoc();
684 for (const auto *D
: SortedDecls
) {
685 Diag(D
->getEndLoc(), diag::note_coroutine_function_declare_noexcept
);
687 return ThrowingDecls
.empty();
690 bool Sema::ActOnCoroutineBodyStart(Scope
*SC
, SourceLocation KWLoc
,
692 if (!checkCoroutineContext(*this, KWLoc
, Keyword
))
694 auto *ScopeInfo
= getCurFunction();
695 assert(ScopeInfo
->CoroutinePromise
);
697 // If we have existing coroutine statements then we have already built
698 // the initial and final suspend points.
699 if (!ScopeInfo
->NeedsCoroutineSuspends
)
702 ScopeInfo
->setNeedsCoroutineSuspends(false);
704 auto *Fn
= cast
<FunctionDecl
>(CurContext
);
705 SourceLocation Loc
= Fn
->getLocation();
706 // Build the initial suspend point
707 auto buildSuspends
= [&](StringRef Name
) mutable -> StmtResult
{
709 buildPromiseCall(*this, ScopeInfo
->CoroutinePromise
, Loc
, Name
, None
);
710 if (Operand
.isInvalid())
713 buildOperatorCoawaitCall(*this, SC
, Loc
, Operand
.get());
714 if (Suspend
.isInvalid())
716 Suspend
= BuildResolvedCoawaitExpr(Loc
, Operand
.get(), Suspend
.get(),
717 /*IsImplicit*/ true);
718 Suspend
= ActOnFinishFullExpr(Suspend
.get(), /*DiscardedValue*/ false);
719 if (Suspend
.isInvalid()) {
720 Diag(Loc
, diag::note_coroutine_promise_suspend_implicitly_required
)
721 << ((Name
== "initial_suspend") ? 0 : 1);
722 Diag(KWLoc
, diag::note_declared_coroutine_here
) << Keyword
;
725 return cast
<Stmt
>(Suspend
.get());
728 StmtResult InitSuspend
= buildSuspends("initial_suspend");
729 if (InitSuspend
.isInvalid())
732 StmtResult FinalSuspend
= buildSuspends("final_suspend");
733 if (FinalSuspend
.isInvalid() || !checkFinalSuspendNoThrow(FinalSuspend
.get()))
736 ScopeInfo
->setCoroutineSuspends(InitSuspend
.get(), FinalSuspend
.get());
741 // Recursively walks up the scope hierarchy until either a 'catch' or a function
742 // scope is found, whichever comes first.
743 static bool isWithinCatchScope(Scope
*S
) {
744 // 'co_await' and 'co_yield' keywords are disallowed within catch blocks, but
745 // lambdas that use 'co_await' are allowed. The loop below ends when a
746 // function scope is found in order to ensure the following behavior:
748 // void foo() { // <- function scope
750 // co_await x; // <- 'co_await' is OK within a function scope
751 // } catch { // <- catch scope
752 // co_await x; // <- 'co_await' is not OK within a catch scope
753 // []() { // <- function scope
754 // co_await x; // <- 'co_await' is OK within a function scope
758 while (S
&& !S
->isFunctionScope()) {
759 if (S
->isCatchScope())
766 // [expr.await]p2, emphasis added: "An await-expression shall appear only in
767 // a *potentially evaluated* expression within the compound-statement of a
768 // function-body *outside of a handler* [...] A context within a function
769 // where an await-expression can appear is called a suspension context of the
771 static void checkSuspensionContext(Sema
&S
, SourceLocation Loc
,
773 // First emphasis of [expr.await]p2: must be a potentially evaluated context.
774 // That is, 'co_await' and 'co_yield' cannot appear in subexpressions of
776 if (S
.isUnevaluatedContext())
777 S
.Diag(Loc
, diag::err_coroutine_unevaluated_context
) << Keyword
;
779 // Second emphasis of [expr.await]p2: must be outside of an exception handler.
780 if (isWithinCatchScope(S
.getCurScope()))
781 S
.Diag(Loc
, diag::err_coroutine_within_handler
) << Keyword
;
784 ExprResult
Sema::ActOnCoawaitExpr(Scope
*S
, SourceLocation Loc
, Expr
*E
) {
785 if (!ActOnCoroutineBodyStart(S
, Loc
, "co_await")) {
786 CorrectDelayedTyposInExpr(E
);
790 checkSuspensionContext(*this, Loc
, "co_await");
792 if (E
->hasPlaceholderType()) {
793 ExprResult R
= CheckPlaceholderExpr(E
);
794 if (R
.isInvalid()) return ExprError();
797 ExprResult Lookup
= BuildOperatorCoawaitLookupExpr(S
, Loc
);
798 if (Lookup
.isInvalid())
800 return BuildUnresolvedCoawaitExpr(Loc
, E
,
801 cast
<UnresolvedLookupExpr
>(Lookup
.get()));
804 ExprResult
Sema::BuildOperatorCoawaitLookupExpr(Scope
*S
, SourceLocation Loc
) {
805 DeclarationName OpName
=
806 Context
.DeclarationNames
.getCXXOperatorName(OO_Coawait
);
807 LookupResult
Operators(*this, OpName
, SourceLocation(),
808 Sema::LookupOperatorName
);
809 LookupName(Operators
, S
);
811 assert(!Operators
.isAmbiguous() && "Operator lookup cannot be ambiguous");
812 const auto &Functions
= Operators
.asUnresolvedSet();
814 Functions
.size() > 1 ||
815 (Functions
.size() == 1 && isa
<FunctionTemplateDecl
>(*Functions
.begin()));
816 Expr
*CoawaitOp
= UnresolvedLookupExpr::Create(
817 Context
, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(),
818 DeclarationNameInfo(OpName
, Loc
), /*RequiresADL*/ true, IsOverloaded
,
819 Functions
.begin(), Functions
.end());
824 // Attempts to resolve and build a CoawaitExpr from "raw" inputs, bailing out to
825 // DependentCoawaitExpr if needed.
826 ExprResult
Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc
, Expr
*Operand
,
827 UnresolvedLookupExpr
*Lookup
) {
828 auto *FSI
= checkCoroutineContext(*this, Loc
, "co_await");
832 if (Operand
->hasPlaceholderType()) {
833 ExprResult R
= CheckPlaceholderExpr(Operand
);
839 auto *Promise
= FSI
->CoroutinePromise
;
840 if (Promise
->getType()->isDependentType()) {
841 Expr
*Res
= new (Context
)
842 DependentCoawaitExpr(Loc
, Context
.DependentTy
, Operand
, Lookup
);
846 auto *RD
= Promise
->getType()->getAsCXXRecordDecl();
847 auto *Transformed
= Operand
;
848 if (lookupMember(*this, "await_transform", RD
, Loc
)) {
850 buildPromiseCall(*this, Promise
, Loc
, "await_transform", Operand
);
853 diag::note_coroutine_promise_implicit_await_transform_required_here
)
854 << Operand
->getSourceRange();
857 Transformed
= R
.get();
859 ExprResult Awaiter
= BuildOperatorCoawaitCall(Loc
, Transformed
, Lookup
);
860 if (Awaiter
.isInvalid())
863 return BuildResolvedCoawaitExpr(Loc
, Operand
, Awaiter
.get());
866 ExprResult
Sema::BuildResolvedCoawaitExpr(SourceLocation Loc
, Expr
*Operand
,
867 Expr
*Awaiter
, bool IsImplicit
) {
868 auto *Coroutine
= checkCoroutineContext(*this, Loc
, "co_await", IsImplicit
);
872 if (Awaiter
->hasPlaceholderType()) {
873 ExprResult R
= CheckPlaceholderExpr(Awaiter
);
874 if (R
.isInvalid()) return ExprError();
878 if (Awaiter
->getType()->isDependentType()) {
879 Expr
*Res
= new (Context
)
880 CoawaitExpr(Loc
, Context
.DependentTy
, Operand
, Awaiter
, IsImplicit
);
884 // If the expression is a temporary, materialize it as an lvalue so that we
885 // can use it multiple times.
886 if (Awaiter
->isPRValue())
887 Awaiter
= CreateMaterializeTemporaryExpr(Awaiter
->getType(), Awaiter
, true);
889 // The location of the `co_await` token cannot be used when constructing
890 // the member call expressions since it's before the location of `Expr`, which
891 // is used as the start of the member call expression.
892 SourceLocation CallLoc
= Awaiter
->getExprLoc();
894 // Build the await_ready, await_suspend, await_resume calls.
895 ReadySuspendResumeResult RSS
=
896 buildCoawaitCalls(*this, Coroutine
->CoroutinePromise
, CallLoc
, Awaiter
);
900 Expr
*Res
= new (Context
)
901 CoawaitExpr(Loc
, Operand
, Awaiter
, RSS
.Results
[0], RSS
.Results
[1],
902 RSS
.Results
[2], RSS
.OpaqueValue
, IsImplicit
);
907 ExprResult
Sema::ActOnCoyieldExpr(Scope
*S
, SourceLocation Loc
, Expr
*E
) {
908 if (!ActOnCoroutineBodyStart(S
, Loc
, "co_yield")) {
909 CorrectDelayedTyposInExpr(E
);
913 checkSuspensionContext(*this, Loc
, "co_yield");
915 // Build yield_value call.
916 ExprResult Awaitable
= buildPromiseCall(
917 *this, getCurFunction()->CoroutinePromise
, Loc
, "yield_value", E
);
918 if (Awaitable
.isInvalid())
921 // Build 'operator co_await' call.
922 Awaitable
= buildOperatorCoawaitCall(*this, S
, Loc
, Awaitable
.get());
923 if (Awaitable
.isInvalid())
926 return BuildCoyieldExpr(Loc
, Awaitable
.get());
928 ExprResult
Sema::BuildCoyieldExpr(SourceLocation Loc
, Expr
*E
) {
929 auto *Coroutine
= checkCoroutineContext(*this, Loc
, "co_yield");
933 if (E
->hasPlaceholderType()) {
934 ExprResult R
= CheckPlaceholderExpr(E
);
935 if (R
.isInvalid()) return ExprError();
941 if (E
->getType()->isDependentType()) {
942 Expr
*Res
= new (Context
) CoyieldExpr(Loc
, Context
.DependentTy
, Operand
, E
);
946 // If the expression is a temporary, materialize it as an lvalue so that we
947 // can use it multiple times.
949 E
= CreateMaterializeTemporaryExpr(E
->getType(), E
, true);
951 // Build the await_ready, await_suspend, await_resume calls.
952 ReadySuspendResumeResult RSS
= buildCoawaitCalls(
953 *this, Coroutine
->CoroutinePromise
, Loc
, E
);
958 new (Context
) CoyieldExpr(Loc
, Operand
, E
, RSS
.Results
[0], RSS
.Results
[1],
959 RSS
.Results
[2], RSS
.OpaqueValue
);
964 StmtResult
Sema::ActOnCoreturnStmt(Scope
*S
, SourceLocation Loc
, Expr
*E
) {
965 if (!ActOnCoroutineBodyStart(S
, Loc
, "co_return")) {
966 CorrectDelayedTyposInExpr(E
);
969 return BuildCoreturnStmt(Loc
, E
);
972 StmtResult
Sema::BuildCoreturnStmt(SourceLocation Loc
, Expr
*E
,
974 auto *FSI
= checkCoroutineContext(*this, Loc
, "co_return", IsImplicit
);
978 if (E
&& E
->hasPlaceholderType() &&
979 !E
->hasPlaceholderType(BuiltinType::Overload
)) {
980 ExprResult R
= CheckPlaceholderExpr(E
);
981 if (R
.isInvalid()) return StmtError();
985 VarDecl
*Promise
= FSI
->CoroutinePromise
;
987 if (E
&& (isa
<InitListExpr
>(E
) || !E
->getType()->isVoidType())) {
988 getNamedReturnInfo(E
, SimplerImplicitMoveMode::ForceOn
);
989 PC
= buildPromiseCall(*this, Promise
, Loc
, "return_value", E
);
991 E
= MakeFullDiscardedValueExpr(E
).get();
992 PC
= buildPromiseCall(*this, Promise
, Loc
, "return_void", None
);
997 Expr
*PCE
= ActOnFinishFullExpr(PC
.get(), /*DiscardedValue*/ false).get();
999 Stmt
*Res
= new (Context
) CoreturnStmt(Loc
, E
, PCE
, IsImplicit
);
1003 /// Look up the std::nothrow object.
1004 static Expr
*buildStdNoThrowDeclRef(Sema
&S
, SourceLocation Loc
) {
1005 NamespaceDecl
*Std
= S
.getStdNamespace();
1006 assert(Std
&& "Should already be diagnosed");
1008 LookupResult
Result(S
, &S
.PP
.getIdentifierTable().get("nothrow"), Loc
,
1009 Sema::LookupOrdinaryName
);
1010 if (!S
.LookupQualifiedName(Result
, Std
)) {
1011 // <coroutine> is not requred to include <new>, so we couldn't omit
1013 S
.Diag(Loc
, diag::err_implicit_coroutine_std_nothrow_type_not_found
);
1017 auto *VD
= Result
.getAsSingle
<VarDecl
>();
1019 Result
.suppressDiagnostics();
1020 // We found something weird. Complain about the first thing we found.
1021 NamedDecl
*Found
= *Result
.begin();
1022 S
.Diag(Found
->getLocation(), diag::err_malformed_std_nothrow
);
1026 ExprResult DR
= S
.BuildDeclRefExpr(VD
, VD
->getType(), VK_LValue
, Loc
);
1033 // Find an appropriate delete for the promise.
1034 static bool findDeleteForPromise(Sema
&S
, SourceLocation Loc
, QualType PromiseType
,
1035 FunctionDecl
*&OperatorDelete
) {
1036 DeclarationName DeleteName
=
1037 S
.Context
.DeclarationNames
.getCXXOperatorName(OO_Delete
);
1039 auto *PointeeRD
= PromiseType
->getAsCXXRecordDecl();
1040 assert(PointeeRD
&& "PromiseType must be a CxxRecordDecl type");
1042 // [dcl.fct.def.coroutine]p12
1043 // The deallocation function's name is looked up by searching for it in the
1044 // scope of the promise type. If nothing is found, a search is performed in
1045 // the global scope.
1046 if (S
.FindDeallocationFunction(Loc
, PointeeRD
, DeleteName
, OperatorDelete
,
1047 /*Diagnose*/ true, /*WantSize*/ true))
1050 // [dcl.fct.def.coroutine]p12
1051 // If both a usual deallocation function with only a pointer parameter and a
1052 // usual deallocation function with both a pointer parameter and a size
1053 // parameter are found, then the selected deallocation function shall be the
1054 // one with two parameters. Otherwise, the selected deallocation function
1055 // shall be the function with one parameter.
1056 if (!OperatorDelete
) {
1057 // Look for a global declaration.
1058 // Coroutines can always provide their required size.
1059 const bool CanProvideSize
= true;
1060 const bool Overaligned
= false;
1061 // Sema::FindUsualDeallocationFunction will try to find the one with two
1062 // parameters first. It will return the deallocation function with one
1063 // parameter if failed.
1064 OperatorDelete
= S
.FindUsualDeallocationFunction(Loc
, CanProvideSize
,
1065 Overaligned
, DeleteName
);
1067 if (!OperatorDelete
)
1071 S
.MarkFunctionReferenced(Loc
, OperatorDelete
);
1076 void Sema::CheckCompletedCoroutineBody(FunctionDecl
*FD
, Stmt
*&Body
) {
1077 FunctionScopeInfo
*Fn
= getCurFunction();
1078 assert(Fn
&& Fn
->isCoroutine() && "not a coroutine");
1080 assert(FD
->isInvalidDecl() &&
1081 "a null body is only allowed for invalid declarations");
1084 // We have a function that uses coroutine keywords, but we failed to build
1085 // the promise type.
1086 if (!Fn
->CoroutinePromise
)
1087 return FD
->setInvalidDecl();
1089 if (isa
<CoroutineBodyStmt
>(Body
)) {
1090 // Nothing todo. the body is already a transformed coroutine body statement.
1094 // The always_inline attribute doesn't reliably apply to a coroutine,
1095 // because the coroutine will be split into pieces and some pieces
1096 // might be called indirectly, as in a virtual call. Even the ramp
1097 // function cannot be inlined at -O0, due to pipeline ordering
1098 // problems (see https://llvm.org/PR53413). Tell the user about it.
1099 if (FD
->hasAttr
<AlwaysInlineAttr
>())
1100 Diag(FD
->getLocation(), diag::warn_always_inline_coroutine
);
1102 // [stmt.return.coroutine]p1:
1103 // A coroutine shall not enclose a return statement ([stmt.return]).
1104 if (Fn
->FirstReturnLoc
.isValid()) {
1105 assert(Fn
->FirstCoroutineStmtLoc
.isValid() &&
1106 "first coroutine location not set");
1107 Diag(Fn
->FirstReturnLoc
, diag::err_return_in_coroutine
);
1108 Diag(Fn
->FirstCoroutineStmtLoc
, diag::note_declared_coroutine_here
)
1109 << Fn
->getFirstCoroutineStmtKeyword();
1111 CoroutineStmtBuilder
Builder(*this, *FD
, *Fn
, Body
);
1112 if (Builder
.isInvalid() || !Builder
.buildStatements())
1113 return FD
->setInvalidDecl();
1115 // Build body for the coroutine wrapper statement.
1116 Body
= CoroutineBodyStmt::Create(Context
, Builder
);
1119 CoroutineStmtBuilder::CoroutineStmtBuilder(Sema
&S
, FunctionDecl
&FD
,
1120 sema::FunctionScopeInfo
&Fn
,
1122 : S(S
), FD(FD
), Fn(Fn
), Loc(FD
.getLocation()),
1123 IsPromiseDependentType(
1124 !Fn
.CoroutinePromise
||
1125 Fn
.CoroutinePromise
->getType()->isDependentType()) {
1128 for (auto KV
: Fn
.CoroutineParameterMoves
)
1129 this->ParamMovesVector
.push_back(KV
.second
);
1130 this->ParamMoves
= this->ParamMovesVector
;
1132 if (!IsPromiseDependentType
) {
1133 PromiseRecordDecl
= Fn
.CoroutinePromise
->getType()->getAsCXXRecordDecl();
1134 assert(PromiseRecordDecl
&& "Type should have already been checked");
1136 this->IsValid
= makePromiseStmt() && makeInitialAndFinalSuspend();
1139 bool CoroutineStmtBuilder::buildStatements() {
1140 assert(this->IsValid
&& "coroutine already invalid");
1141 this->IsValid
= makeReturnObject();
1142 if (this->IsValid
&& !IsPromiseDependentType
)
1143 buildDependentStatements();
1144 return this->IsValid
;
1147 bool CoroutineStmtBuilder::buildDependentStatements() {
1148 assert(this->IsValid
&& "coroutine already invalid");
1149 assert(!this->IsPromiseDependentType
&&
1150 "coroutine cannot have a dependent promise type");
1151 this->IsValid
= makeOnException() && makeOnFallthrough() &&
1152 makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() &&
1153 makeNewAndDeleteExpr();
1154 return this->IsValid
;
1157 bool CoroutineStmtBuilder::makePromiseStmt() {
1158 // Form a declaration statement for the promise declaration, so that AST
1159 // visitors can more easily find it.
1160 StmtResult PromiseStmt
=
1161 S
.ActOnDeclStmt(S
.ConvertDeclToDeclGroup(Fn
.CoroutinePromise
), Loc
, Loc
);
1162 if (PromiseStmt
.isInvalid())
1165 this->Promise
= PromiseStmt
.get();
1169 bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() {
1170 if (Fn
.hasInvalidCoroutineSuspends())
1172 this->InitialSuspend
= cast
<Expr
>(Fn
.CoroutineSuspends
.first
);
1173 this->FinalSuspend
= cast
<Expr
>(Fn
.CoroutineSuspends
.second
);
1177 static bool diagReturnOnAllocFailure(Sema
&S
, Expr
*E
,
1178 CXXRecordDecl
*PromiseRecordDecl
,
1179 FunctionScopeInfo
&Fn
) {
1180 auto Loc
= E
->getExprLoc();
1181 if (auto *DeclRef
= dyn_cast_or_null
<DeclRefExpr
>(E
)) {
1182 auto *Decl
= DeclRef
->getDecl();
1183 if (CXXMethodDecl
*Method
= dyn_cast_or_null
<CXXMethodDecl
>(Decl
)) {
1184 if (Method
->isStatic())
1187 Loc
= Decl
->getLocation();
1193 diag::err_coroutine_promise_get_return_object_on_allocation_failure
)
1194 << PromiseRecordDecl
;
1195 S
.Diag(Fn
.FirstCoroutineStmtLoc
, diag::note_declared_coroutine_here
)
1196 << Fn
.getFirstCoroutineStmtKeyword();
1200 bool CoroutineStmtBuilder::makeReturnOnAllocFailure() {
1201 assert(!IsPromiseDependentType
&&
1202 "cannot make statement while the promise type is dependent");
1204 // [dcl.fct.def.coroutine]p10
1205 // If a search for the name get_return_object_on_allocation_failure in
1206 // the scope of the promise type ([class.member.lookup]) finds any
1207 // declarations, then the result of a call to an allocation function used to
1208 // obtain storage for the coroutine state is assumed to return nullptr if it
1209 // fails to obtain storage, ... If the allocation function returns nullptr,
1210 // ... and the return value is obtained by a call to
1211 // T::get_return_object_on_allocation_failure(), where T is the
1213 DeclarationName DN
=
1214 S
.PP
.getIdentifierInfo("get_return_object_on_allocation_failure");
1215 LookupResult
Found(S
, DN
, Loc
, Sema::LookupMemberName
);
1216 if (!S
.LookupQualifiedName(Found
, PromiseRecordDecl
))
1220 ExprResult DeclNameExpr
=
1221 S
.BuildDeclarationNameExpr(SS
, Found
, /*NeedsADL=*/false);
1222 if (DeclNameExpr
.isInvalid())
1225 if (!diagReturnOnAllocFailure(S
, DeclNameExpr
.get(), PromiseRecordDecl
, Fn
))
1228 ExprResult ReturnObjectOnAllocationFailure
=
1229 S
.BuildCallExpr(nullptr, DeclNameExpr
.get(), Loc
, {}, Loc
);
1230 if (ReturnObjectOnAllocationFailure
.isInvalid())
1233 StmtResult ReturnStmt
=
1234 S
.BuildReturnStmt(Loc
, ReturnObjectOnAllocationFailure
.get());
1235 if (ReturnStmt
.isInvalid()) {
1236 S
.Diag(Found
.getFoundDecl()->getLocation(), diag::note_member_declared_here
)
1238 S
.Diag(Fn
.FirstCoroutineStmtLoc
, diag::note_declared_coroutine_here
)
1239 << Fn
.getFirstCoroutineStmtKeyword();
1243 this->ReturnStmtOnAllocFailure
= ReturnStmt
.get();
1247 // Collect placement arguments for allocation function of coroutine FD.
1248 // Return true if we collect placement arguments succesfully. Return false,
1250 static bool collectPlacementArgs(Sema
&S
, FunctionDecl
&FD
, SourceLocation Loc
,
1251 SmallVectorImpl
<Expr
*> &PlacementArgs
) {
1252 if (auto *MD
= dyn_cast
<CXXMethodDecl
>(&FD
)) {
1253 if (MD
->isInstance() && !isLambdaCallOperator(MD
)) {
1254 ExprResult ThisExpr
= S
.ActOnCXXThis(Loc
);
1255 if (ThisExpr
.isInvalid())
1257 ThisExpr
= S
.CreateBuiltinUnaryOp(Loc
, UO_Deref
, ThisExpr
.get());
1258 if (ThisExpr
.isInvalid())
1260 PlacementArgs
.push_back(ThisExpr
.get());
1264 for (auto *PD
: FD
.parameters()) {
1265 if (PD
->getType()->isDependentType())
1268 // Build a reference to the parameter.
1269 auto PDLoc
= PD
->getLocation();
1270 ExprResult PDRefExpr
=
1271 S
.BuildDeclRefExpr(PD
, PD
->getOriginalType().getNonReferenceType(),
1272 ExprValueKind::VK_LValue
, PDLoc
);
1273 if (PDRefExpr
.isInvalid())
1276 PlacementArgs
.push_back(PDRefExpr
.get());
1282 bool CoroutineStmtBuilder::makeNewAndDeleteExpr() {
1283 // Form and check allocation and deallocation calls.
1284 assert(!IsPromiseDependentType
&&
1285 "cannot make statement while the promise type is dependent");
1286 QualType PromiseType
= Fn
.CoroutinePromise
->getType();
1288 if (S
.RequireCompleteType(Loc
, PromiseType
, diag::err_incomplete_type
))
1291 const bool RequiresNoThrowAlloc
= ReturnStmtOnAllocFailure
!= nullptr;
1293 // According to [dcl.fct.def.coroutine]p9, Lookup allocation functions using a
1294 // parameter list composed of the requested size of the coroutine state being
1295 // allocated, followed by the coroutine function's arguments. If a matching
1296 // allocation function exists, use it. Otherwise, use an allocation function
1297 // that just takes the requested size.
1299 // [dcl.fct.def.coroutine]p9
1300 // An implementation may need to allocate additional storage for a
1302 // This storage is known as the coroutine state and is obtained by calling a
1303 // non-array allocation function ([basic.stc.dynamic.allocation]). The
1304 // allocation function's name is looked up by searching for it in the scope of
1305 // the promise type.
1306 // - If any declarations are found, overload resolution is performed on a
1307 // function call created by assembling an argument list. The first argument is
1308 // the amount of space requested, and has type std::size_t. The
1309 // lvalues p1 ... pn are the succeeding arguments.
1311 // ...where "p1 ... pn" are defined earlier as:
1313 // [dcl.fct.def.coroutine]p3
1314 // The promise type of a coroutine is `std::coroutine_traits<R, P1, ...,
1316 // , where R is the return type of the function, and `P1, ..., Pn` are the
1317 // sequence of types of the non-object function parameters, preceded by the
1318 // type of the object parameter ([dcl.fct]) if the coroutine is a non-static
1319 // member function. [dcl.fct.def.coroutine]p4 In the following, p_i is an
1320 // lvalue of type P_i, where p1 denotes the object parameter and p_i+1 denotes
1321 // the i-th non-object function parameter for a non-static member function,
1322 // and p_i denotes the i-th function parameter otherwise. For a non-static
1323 // member function, q_1 is an lvalue that denotes *this; any other q_i is an
1324 // lvalue that denotes the parameter copy corresponding to p_i.
1326 FunctionDecl
*OperatorNew
= nullptr;
1327 bool PassAlignment
= false;
1328 SmallVector
<Expr
*, 1> PlacementArgs
;
1330 const bool PromiseContainsNew
= [this, &PromiseType
]() -> bool {
1331 DeclarationName NewName
=
1332 S
.getASTContext().DeclarationNames
.getCXXOperatorName(OO_New
);
1333 LookupResult
R(S
, NewName
, Loc
, Sema::LookupOrdinaryName
);
1335 if (PromiseType
->isRecordType())
1336 S
.LookupQualifiedName(R
, PromiseType
->getAsCXXRecordDecl());
1338 return !R
.empty() && !R
.isAmbiguous();
1341 auto LookupAllocationFunction
= [&](Sema::AllocationFunctionScope NewScope
=
1343 // [dcl.fct.def.coroutine]p9
1344 // The allocation function's name is looked up by searching for it in the
1345 // scope of the promise type.
1346 // - If any declarations are found, ...
1347 // - If no declarations are found in the scope of the promise type, a search
1348 // is performed in the global scope.
1349 if (NewScope
== Sema::AFS_Both
)
1350 NewScope
= PromiseContainsNew
? Sema::AFS_Class
: Sema::AFS_Global
;
1352 FunctionDecl
*UnusedResult
= nullptr;
1353 S
.FindAllocationFunctions(Loc
, SourceRange(), NewScope
,
1354 /*DeleteScope*/ Sema::AFS_Both
, PromiseType
,
1355 /*isArray*/ false, PassAlignment
, PlacementArgs
,
1356 OperatorNew
, UnusedResult
, /*Diagnose*/ false);
1359 // We don't expect to call to global operator new with (size, p0, …, pn).
1360 // So if we choose to lookup the allocation function in global scope, we
1361 // shouldn't lookup placement arguments.
1362 if (PromiseContainsNew
&& !collectPlacementArgs(S
, FD
, Loc
, PlacementArgs
))
1365 LookupAllocationFunction();
1367 // [dcl.fct.def.coroutine]p9
1368 // If no viable function is found ([over.match.viable]), overload resolution
1369 // is performed again on a function call created by passing just the amount of
1370 // space required as an argument of type std::size_t.
1371 if (!OperatorNew
&& !PlacementArgs
.empty() && PromiseContainsNew
) {
1372 PlacementArgs
.clear();
1373 LookupAllocationFunction();
1376 bool IsGlobalOverload
=
1377 OperatorNew
&& !isa
<CXXRecordDecl
>(OperatorNew
->getDeclContext());
1378 // If we didn't find a class-local new declaration and non-throwing new
1379 // was is required then we need to lookup the non-throwing global operator
1381 if (RequiresNoThrowAlloc
&& (!OperatorNew
|| IsGlobalOverload
)) {
1382 auto *StdNoThrow
= buildStdNoThrowDeclRef(S
, Loc
);
1385 PlacementArgs
= {StdNoThrow
};
1386 OperatorNew
= nullptr;
1387 LookupAllocationFunction(Sema::AFS_Global
);
1391 if (PromiseContainsNew
)
1392 S
.Diag(Loc
, diag::err_coroutine_unusable_new
) << PromiseType
<< &FD
;
1393 else if (RequiresNoThrowAlloc
)
1394 S
.Diag(Loc
, diag::err_coroutine_unfound_nothrow_new
) << &FD
;
1399 if (RequiresNoThrowAlloc
) {
1400 const auto *FT
= OperatorNew
->getType()->castAs
<FunctionProtoType
>();
1401 if (!FT
->isNothrow(/*ResultIfDependent*/ false)) {
1402 S
.Diag(OperatorNew
->getLocation(),
1403 diag::err_coroutine_promise_new_requires_nothrow
)
1405 S
.Diag(Loc
, diag::note_coroutine_promise_call_implicitly_required
)
1411 FunctionDecl
*OperatorDelete
= nullptr;
1412 if (!findDeleteForPromise(S
, Loc
, PromiseType
, OperatorDelete
)) {
1413 // FIXME: We should add an error here. According to:
1414 // [dcl.fct.def.coroutine]p12
1415 // If no usual deallocation function is found, the program is ill-formed.
1420 S
.BuildBuiltinCallExpr(Loc
, Builtin::BI__builtin_coro_frame
, {});
1423 S
.BuildBuiltinCallExpr(Loc
, Builtin::BI__builtin_coro_size
, {});
1428 S
.BuildDeclRefExpr(OperatorNew
, OperatorNew
->getType(), VK_LValue
, Loc
);
1429 if (NewRef
.isInvalid())
1432 SmallVector
<Expr
*, 2> NewArgs(1, FrameSize
);
1433 llvm::append_range(NewArgs
, PlacementArgs
);
1435 ExprResult NewExpr
=
1436 S
.BuildCallExpr(S
.getCurScope(), NewRef
.get(), Loc
, NewArgs
, Loc
);
1437 NewExpr
= S
.ActOnFinishFullExpr(NewExpr
.get(), /*DiscardedValue*/ false);
1438 if (NewExpr
.isInvalid())
1441 // Make delete call.
1443 QualType OpDeleteQualType
= OperatorDelete
->getType();
1445 ExprResult DeleteRef
=
1446 S
.BuildDeclRefExpr(OperatorDelete
, OpDeleteQualType
, VK_LValue
, Loc
);
1447 if (DeleteRef
.isInvalid())
1451 S
.BuildBuiltinCallExpr(Loc
, Builtin::BI__builtin_coro_free
, {FramePtr
});
1453 SmallVector
<Expr
*, 2> DeleteArgs
{CoroFree
};
1455 // [dcl.fct.def.coroutine]p12
1456 // The selected deallocation function shall be called with the address of
1457 // the block of storage to be reclaimed as its first argument. If a
1458 // deallocation function with a parameter of type std::size_t is
1459 // used, the size of the block is passed as the corresponding argument.
1460 const auto *OpDeleteType
=
1461 OpDeleteQualType
.getTypePtr()->castAs
<FunctionProtoType
>();
1462 if (OpDeleteType
->getNumParams() > 1)
1463 DeleteArgs
.push_back(FrameSize
);
1465 ExprResult DeleteExpr
=
1466 S
.BuildCallExpr(S
.getCurScope(), DeleteRef
.get(), Loc
, DeleteArgs
, Loc
);
1468 S
.ActOnFinishFullExpr(DeleteExpr
.get(), /*DiscardedValue*/ false);
1469 if (DeleteExpr
.isInvalid())
1472 this->Allocate
= NewExpr
.get();
1473 this->Deallocate
= DeleteExpr
.get();
1478 bool CoroutineStmtBuilder::makeOnFallthrough() {
1479 assert(!IsPromiseDependentType
&&
1480 "cannot make statement while the promise type is dependent");
1482 // [dcl.fct.def.coroutine]/p6
1483 // If searches for the names return_void and return_value in the scope of
1484 // the promise type each find any declarations, the program is ill-formed.
1485 // [Note 1: If return_void is found, flowing off the end of a coroutine is
1486 // equivalent to a co_return with no operand. Otherwise, flowing off the end
1487 // of a coroutine results in undefined behavior ([stmt.return.coroutine]). —
1489 bool HasRVoid
, HasRValue
;
1490 LookupResult LRVoid
=
1491 lookupMember(S
, "return_void", PromiseRecordDecl
, Loc
, HasRVoid
);
1492 LookupResult LRValue
=
1493 lookupMember(S
, "return_value", PromiseRecordDecl
, Loc
, HasRValue
);
1495 StmtResult Fallthrough
;
1496 if (HasRVoid
&& HasRValue
) {
1497 // FIXME Improve this diagnostic
1498 S
.Diag(FD
.getLocation(),
1499 diag::err_coroutine_promise_incompatible_return_functions
)
1500 << PromiseRecordDecl
;
1501 S
.Diag(LRVoid
.getRepresentativeDecl()->getLocation(),
1502 diag::note_member_first_declared_here
)
1503 << LRVoid
.getLookupName();
1504 S
.Diag(LRValue
.getRepresentativeDecl()->getLocation(),
1505 diag::note_member_first_declared_here
)
1506 << LRValue
.getLookupName();
1508 } else if (!HasRVoid
&& !HasRValue
) {
1509 // We need to set 'Fallthrough'. Otherwise the other analysis part might
1510 // think the coroutine has defined a return_value method. So it might emit
1511 // **false** positive warning. e.g.,
1513 // promise_without_return_func foo() {
1514 // co_await something();
1517 // Then AnalysisBasedWarning would emit a warning about `foo()` lacking a
1518 // co_return statements, which isn't correct.
1519 Fallthrough
= S
.ActOnNullStmt(PromiseRecordDecl
->getLocation());
1520 if (Fallthrough
.isInvalid())
1522 } else if (HasRVoid
) {
1523 Fallthrough
= S
.BuildCoreturnStmt(FD
.getLocation(), nullptr,
1524 /*IsImplicit*/false);
1525 Fallthrough
= S
.ActOnFinishFullStmt(Fallthrough
.get());
1526 if (Fallthrough
.isInvalid())
1530 this->OnFallthrough
= Fallthrough
.get();
1534 bool CoroutineStmtBuilder::makeOnException() {
1535 // Try to form 'p.unhandled_exception();'
1536 assert(!IsPromiseDependentType
&&
1537 "cannot make statement while the promise type is dependent");
1539 const bool RequireUnhandledException
= S
.getLangOpts().CXXExceptions
;
1541 if (!lookupMember(S
, "unhandled_exception", PromiseRecordDecl
, Loc
)) {
1543 RequireUnhandledException
1544 ? diag::err_coroutine_promise_unhandled_exception_required
1546 warn_coroutine_promise_unhandled_exception_required_with_exceptions
;
1547 S
.Diag(Loc
, DiagID
) << PromiseRecordDecl
;
1548 S
.Diag(PromiseRecordDecl
->getLocation(), diag::note_defined_here
)
1549 << PromiseRecordDecl
;
1550 return !RequireUnhandledException
;
1553 // If exceptions are disabled, don't try to build OnException.
1554 if (!S
.getLangOpts().CXXExceptions
)
1557 ExprResult UnhandledException
= buildPromiseCall(S
, Fn
.CoroutinePromise
, Loc
,
1558 "unhandled_exception", None
);
1559 UnhandledException
= S
.ActOnFinishFullExpr(UnhandledException
.get(), Loc
,
1560 /*DiscardedValue*/ false);
1561 if (UnhandledException
.isInvalid())
1564 // Since the body of the coroutine will be wrapped in try-catch, it will
1565 // be incompatible with SEH __try if present in a function.
1566 if (!S
.getLangOpts().Borland
&& Fn
.FirstSEHTryLoc
.isValid()) {
1567 S
.Diag(Fn
.FirstSEHTryLoc
, diag::err_seh_in_a_coroutine_with_cxx_exceptions
);
1568 S
.Diag(Fn
.FirstCoroutineStmtLoc
, diag::note_declared_coroutine_here
)
1569 << Fn
.getFirstCoroutineStmtKeyword();
1573 this->OnException
= UnhandledException
.get();
1577 bool CoroutineStmtBuilder::makeReturnObject() {
1578 // [dcl.fct.def.coroutine]p7
1579 // The expression promise.get_return_object() is used to initialize the
1580 // returned reference or prvalue result object of a call to a coroutine.
1581 ExprResult ReturnObject
=
1582 buildPromiseCall(S
, Fn
.CoroutinePromise
, Loc
, "get_return_object", None
);
1583 if (ReturnObject
.isInvalid())
1586 this->ReturnValue
= ReturnObject
.get();
1590 static void noteMemberDeclaredHere(Sema
&S
, Expr
*E
, FunctionScopeInfo
&Fn
) {
1591 if (auto *MbrRef
= dyn_cast
<CXXMemberCallExpr
>(E
)) {
1592 auto *MethodDecl
= MbrRef
->getMethodDecl();
1593 S
.Diag(MethodDecl
->getLocation(), diag::note_member_declared_here
)
1596 S
.Diag(Fn
.FirstCoroutineStmtLoc
, diag::note_declared_coroutine_here
)
1597 << Fn
.getFirstCoroutineStmtKeyword();
1600 bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
1601 assert(!IsPromiseDependentType
&&
1602 "cannot make statement while the promise type is dependent");
1603 assert(this->ReturnValue
&& "ReturnValue must be already formed");
1605 QualType
const GroType
= this->ReturnValue
->getType();
1606 assert(!GroType
->isDependentType() &&
1607 "get_return_object type must no longer be dependent");
1609 QualType
const FnRetType
= FD
.getReturnType();
1610 assert(!FnRetType
->isDependentType() &&
1611 "get_return_object type must no longer be dependent");
1613 if (FnRetType
->isVoidType()) {
1615 S
.ActOnFinishFullExpr(this->ReturnValue
, Loc
, /*DiscardedValue*/ false);
1616 if (Res
.isInvalid())
1622 if (GroType
->isVoidType()) {
1623 // Trigger a nice error message.
1624 InitializedEntity Entity
=
1625 InitializedEntity::InitializeResult(Loc
, FnRetType
);
1626 S
.PerformCopyInitialization(Entity
, SourceLocation(), ReturnValue
);
1627 noteMemberDeclaredHere(S
, ReturnValue
, Fn
);
1631 StmtResult ReturnStmt
= S
.BuildReturnStmt(Loc
, ReturnValue
);
1632 if (ReturnStmt
.isInvalid()) {
1633 noteMemberDeclaredHere(S
, ReturnValue
, Fn
);
1637 this->ReturnStmt
= ReturnStmt
.get();
1641 // Create a static_cast\<T&&>(expr).
1642 static Expr
*castForMoving(Sema
&S
, Expr
*E
, QualType T
= QualType()) {
1645 QualType TargetType
= S
.BuildReferenceType(
1646 T
, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName());
1647 SourceLocation ExprLoc
= E
->getBeginLoc();
1648 TypeSourceInfo
*TargetLoc
=
1649 S
.Context
.getTrivialTypeSourceInfo(TargetType
, ExprLoc
);
1652 .BuildCXXNamedCast(ExprLoc
, tok::kw_static_cast
, TargetLoc
, E
,
1653 SourceRange(ExprLoc
, ExprLoc
), E
->getSourceRange())
1657 /// Build a variable declaration for move parameter.
1658 static VarDecl
*buildVarDecl(Sema
&S
, SourceLocation Loc
, QualType Type
,
1659 IdentifierInfo
*II
) {
1660 TypeSourceInfo
*TInfo
= S
.Context
.getTrivialTypeSourceInfo(Type
, Loc
);
1661 VarDecl
*Decl
= VarDecl::Create(S
.Context
, S
.CurContext
, Loc
, Loc
, II
, Type
,
1663 Decl
->setImplicit();
1667 // Build statements that move coroutine function parameters to the coroutine
1668 // frame, and store them on the function scope info.
1669 bool Sema::buildCoroutineParameterMoves(SourceLocation Loc
) {
1670 assert(isa
<FunctionDecl
>(CurContext
) && "not in a function scope");
1671 auto *FD
= cast
<FunctionDecl
>(CurContext
);
1673 auto *ScopeInfo
= getCurFunction();
1674 if (!ScopeInfo
->CoroutineParameterMoves
.empty())
1677 // [dcl.fct.def.coroutine]p13
1678 // When a coroutine is invoked, after initializing its parameters
1679 // ([expr.call]), a copy is created for each coroutine parameter. For a
1680 // parameter of type cv T, the copy is a variable of type cv T with
1681 // automatic storage duration that is direct-initialized from an xvalue of
1682 // type T referring to the parameter.
1683 for (auto *PD
: FD
->parameters()) {
1684 if (PD
->getType()->isDependentType())
1687 ExprResult PDRefExpr
=
1688 BuildDeclRefExpr(PD
, PD
->getType().getNonReferenceType(),
1689 ExprValueKind::VK_LValue
, Loc
); // FIXME: scope?
1690 if (PDRefExpr
.isInvalid())
1693 Expr
*CExpr
= nullptr;
1694 if (PD
->getType()->getAsCXXRecordDecl() ||
1695 PD
->getType()->isRValueReferenceType())
1696 CExpr
= castForMoving(*this, PDRefExpr
.get());
1698 CExpr
= PDRefExpr
.get();
1699 // [dcl.fct.def.coroutine]p13
1700 // The initialization and destruction of each parameter copy occurs in the
1701 // context of the called coroutine.
1702 auto *D
= buildVarDecl(*this, Loc
, PD
->getType(), PD
->getIdentifier());
1703 AddInitializerToDecl(D
, CExpr
, /*DirectInit=*/true);
1705 // Convert decl to a statement.
1706 StmtResult Stmt
= ActOnDeclStmt(ConvertDeclToDeclGroup(D
), Loc
, Loc
);
1707 if (Stmt
.isInvalid())
1710 ScopeInfo
->CoroutineParameterMoves
.insert(std::make_pair(PD
, Stmt
.get()));
1715 StmtResult
Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args
) {
1716 CoroutineBodyStmt
*Res
= CoroutineBodyStmt::Create(Context
, Args
);
1722 ClassTemplateDecl
*Sema::lookupCoroutineTraits(SourceLocation KwLoc
,
1723 SourceLocation FuncLoc
,
1724 NamespaceDecl
*&Namespace
) {
1725 if (!StdCoroutineTraitsCache
) {
1726 // Because coroutines moved from std::experimental in the TS to std in
1727 // C++20, we look in both places to give users time to transition their
1728 // TS-specific code to C++20. Diagnostics are given when the TS usage is
1730 // TODO: Become stricter when <experimental/coroutine> is removed.
1732 IdentifierInfo
const &TraitIdent
=
1733 PP
.getIdentifierTable().get("coroutine_traits");
1735 NamespaceDecl
*StdSpace
= getStdNamespace();
1736 LookupResult
ResStd(*this, &TraitIdent
, FuncLoc
, LookupOrdinaryName
);
1737 bool InStd
= StdSpace
&& LookupQualifiedName(ResStd
, StdSpace
);
1739 NamespaceDecl
*ExpSpace
= lookupStdExperimentalNamespace();
1740 LookupResult
ResExp(*this, &TraitIdent
, FuncLoc
, LookupOrdinaryName
);
1741 bool InExp
= ExpSpace
&& LookupQualifiedName(ResExp
, ExpSpace
);
1743 if (!InStd
&& !InExp
) {
1744 // The goggles, they found nothing!
1745 Diag(KwLoc
, diag::err_implied_coroutine_type_not_found
)
1746 << "std::coroutine_traits";
1750 // Prefer ::std to std::experimental.
1751 LookupResult
&Result
= InStd
? ResStd
: ResExp
;
1752 CoroTraitsNamespaceCache
= InStd
? StdSpace
: ExpSpace
;
1754 // coroutine_traits is required to be a class template.
1755 StdCoroutineTraitsCache
= Result
.getAsSingle
<ClassTemplateDecl
>();
1756 if (!StdCoroutineTraitsCache
) {
1757 Result
.suppressDiagnostics();
1758 NamedDecl
*Found
= *Result
.begin();
1759 Diag(Found
->getLocation(), diag::err_malformed_std_coroutine_traits
);
1764 // Found in std::experimental
1765 Diag(KwLoc
, diag::warn_deprecated_coroutine_namespace
)
1766 << "coroutine_traits";
1767 ResExp
.suppressDiagnostics();
1768 NamedDecl
*Found
= *ResExp
.begin();
1769 Diag(Found
->getLocation(), diag::note_entity_declared_at
) << Found
;
1772 StdCoroutineTraitsCache
!= ResExp
.getAsSingle
<ClassTemplateDecl
>()) {
1773 // Also found something different in std
1775 diag::err_mixed_use_std_and_experimental_namespace_for_coroutine
);
1776 Diag(StdCoroutineTraitsCache
->getLocation(),
1777 diag::note_entity_declared_at
)
1778 << StdCoroutineTraitsCache
;
1784 Namespace
= CoroTraitsNamespaceCache
;
1785 return StdCoroutineTraitsCache
;