blob: e6b640f878c2bb4bbd3dd1f1f067d3c2bc1479df [file] [log] [blame]
Richard Smithcfd53b42015-10-22 06:13:50 +00001//===--- SemaCoroutines.cpp - Semantic Analysis for Coroutines ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for C++ Coroutines.
11//
12//===----------------------------------------------------------------------===//
13
Eric Fiselierbee782b2017-04-03 19:21:00 +000014#include "CoroutineStmtBuilder.h"
Richard Smith9f690bd2015-10-27 06:02:45 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/StmtCXX.h"
18#include "clang/Lex/Preprocessor.h"
Richard Smith2af65c42015-11-24 02:34:39 +000019#include "clang/Sema/Initialization.h"
Richard Smith9f690bd2015-10-27 06:02:45 +000020#include "clang/Sema/Overload.h"
Eric Fiselierbee782b2017-04-03 19:21:00 +000021#include "clang/Sema/SemaInternal.h"
22
Richard Smithcfd53b42015-10-22 06:13:50 +000023using namespace clang;
24using namespace sema;
25
Eric Fiselierfc50f622017-05-25 14:59:39 +000026static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
27 SourceLocation Loc, bool &Res) {
Eric Fiselier20f25cb2017-03-06 23:38:15 +000028 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();
Eric Fiselierfc50f622017-05-25 14:59:39 +000033 Res = S.LookupQualifiedName(LR, RD);
34 return LR;
35}
36
37static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
38 SourceLocation Loc) {
39 bool Res;
40 lookupMember(S, Name, RD, Loc, Res);
41 return Res;
Eric Fiselier20f25cb2017-03-06 23:38:15 +000042}
43
Richard Smith9f690bd2015-10-27 06:02:45 +000044/// Look up the std::coroutine_traits<...>::promise_type for the given
45/// function type.
Eric Fiselier166c6e62017-07-10 01:27:22 +000046static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD,
47 SourceLocation KwLoc) {
48 const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>();
49 const SourceLocation FuncLoc = FD->getLocation();
Richard Smith9f690bd2015-10-27 06:02:45 +000050 // FIXME: Cache std::coroutine_traits once we've found it.
Gor Nishanov3e048bb2016-10-04 00:31:16 +000051 NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace();
52 if (!StdExp) {
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +000053 S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
54 << "std::experimental::coroutine_traits";
Richard Smith9f690bd2015-10-27 06:02:45 +000055 return QualType();
56 }
57
58 LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_traits"),
Eric Fiselier89bf0e72017-03-06 22:52:28 +000059 FuncLoc, Sema::LookupOrdinaryName);
Gor Nishanov3e048bb2016-10-04 00:31:16 +000060 if (!S.LookupQualifiedName(Result, StdExp)) {
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +000061 S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
62 << "std::experimental::coroutine_traits";
Richard Smith9f690bd2015-10-27 06:02:45 +000063 return QualType();
64 }
65
66 ClassTemplateDecl *CoroTraits = Result.getAsSingle<ClassTemplateDecl>();
67 if (!CoroTraits) {
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);
72 return QualType();
73 }
74
Eric Fiselier166c6e62017-07-10 01:27:22 +000075 // Form template argument list for coroutine_traits<R, P1, P2, ...> according
76 // to [dcl.fct.def.coroutine]3
Eric Fiselier89bf0e72017-03-06 22:52:28 +000077 TemplateArgumentListInfo Args(KwLoc, KwLoc);
Eric Fiselier166c6e62017-07-10 01:27:22 +000078 auto AddArg = [&](QualType T) {
Richard Smith9f690bd2015-10-27 06:02:45 +000079 Args.addArgument(TemplateArgumentLoc(
Eric Fiselier89bf0e72017-03-06 22:52:28 +000080 TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc)));
Eric Fiselier166c6e62017-07-10 01:27:22 +000081 };
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
89 // parameter is
Eric Fiselierbf166ce2017-07-10 02:52:34 +000090 // -- "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 &&
93 // ref-qualifier
Eric Fiselier166c6e62017-07-10 01:27:22 +000094 QualType T =
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);
99 AddArg(T);
100 }
101 }
102 for (QualType T : FnType->getParamTypes())
103 AddArg(T);
Richard Smith9f690bd2015-10-27 06:02:45 +0000104
105 // Build the template-id.
106 QualType CoroTrait =
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000107 S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args);
Richard Smith9f690bd2015-10-27 06:02:45 +0000108 if (CoroTrait.isNull())
109 return QualType();
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000110 if (S.RequireCompleteType(KwLoc, CoroTrait,
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000111 diag::err_coroutine_type_missing_specialization))
Richard Smith9f690bd2015-10-27 06:02:45 +0000112 return QualType();
113
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000114 auto *RD = CoroTrait->getAsCXXRecordDecl();
Richard Smith9f690bd2015-10-27 06:02:45 +0000115 assert(RD && "specialization of class template is not a class?");
116
117 // Look up the ::promise_type member.
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000118 LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc,
Richard Smith9f690bd2015-10-27 06:02:45 +0000119 Sema::LookupOrdinaryName);
120 S.LookupQualifiedName(R, RD);
121 auto *Promise = R.getAsSingle<TypeDecl>();
122 if (!Promise) {
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000123 S.Diag(FuncLoc,
124 diag::err_implied_std_coroutine_traits_promise_type_not_found)
Gor Nishanov8df64e92016-10-27 16:28:31 +0000125 << RD;
Richard Smith9f690bd2015-10-27 06:02:45 +0000126 return QualType();
127 }
Richard Smith9f690bd2015-10-27 06:02:45 +0000128 // The promise type is required to be a class type.
129 QualType PromiseType = S.Context.getTypeDeclType(Promise);
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000130
131 auto buildElaboratedType = [&]() {
Gor Nishanov3e048bb2016-10-04 00:31:16 +0000132 auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, StdExp);
Richard Smith9b2f53e2015-11-19 02:36:35 +0000133 NNS = NestedNameSpecifier::Create(S.Context, NNS, false,
134 CoroTrait.getTypePtr());
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000135 return S.Context.getElaboratedType(ETK_None, NNS, PromiseType);
136 };
Richard Smith9b2f53e2015-11-19 02:36:35 +0000137
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000138 if (!PromiseType->getAsCXXRecordDecl()) {
139 S.Diag(FuncLoc,
140 diag::err_implied_std_coroutine_traits_promise_type_not_class)
141 << buildElaboratedType();
Richard Smith9f690bd2015-10-27 06:02:45 +0000142 return QualType();
143 }
Eric Fiselier89bf0e72017-03-06 22:52:28 +0000144 if (S.RequireCompleteType(FuncLoc, buildElaboratedType(),
145 diag::err_coroutine_promise_type_incomplete))
146 return QualType();
Richard Smith9f690bd2015-10-27 06:02:45 +0000147
148 return PromiseType;
149}
150
Gor Nishanov29ff6382017-05-24 14:34:19 +0000151/// Look up the std::experimental::coroutine_handle<PromiseType>.
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000152static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType,
153 SourceLocation Loc) {
154 if (PromiseType.isNull())
155 return QualType();
156
157 NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace();
158 assert(StdExp && "Should already be diagnosed");
159
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";
165 return QualType();
166 }
167
168 ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>();
169 if (!CoroHandle) {
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);
174 return QualType();
175 }
176
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)));
182
183 // Build the template-id.
184 QualType CoroHandleType =
185 S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args);
186 if (CoroHandleType.isNull())
187 return QualType();
188 if (S.RequireCompleteType(Loc, CoroHandleType,
189 diag::err_coroutine_type_missing_specialization))
190 return QualType();
191
192 return CoroHandleType;
193}
194
Eric Fiselierc8efda72016-10-27 18:43:28 +0000195static bool isValidCoroutineContext(Sema &S, SourceLocation Loc,
196 StringRef Keyword) {
Richard Smith744b2242015-11-20 02:54:01 +0000197 // '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;
Eric Fiselierc8efda72016-10-27 18:43:28 +0000200 return false;
Richard Smith744b2242015-11-20 02:54:01 +0000201 }
Richard Smithcfd53b42015-10-22 06:13:50 +0000202
203 // Any other usage must be within a function.
204 auto *FD = dyn_cast<FunctionDecl>(S.CurContext);
205 if (!FD) {
206 S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext)
207 ? diag::err_coroutine_objc_method
208 : diag::err_coroutine_outside_function) << Keyword;
Eric Fiselierc8efda72016-10-27 18:43:28 +0000209 return false;
Richard Smithcfd53b42015-10-22 06:13:50 +0000210 }
211
Eric Fiselierc8efda72016-10-27 18:43:28 +0000212 // An enumeration for mapping the diagnostic type to the correct diagnostic
213 // selection index.
214 enum InvalidFuncDiag {
215 DiagCtor = 0,
216 DiagDtor,
217 DiagCopyAssign,
218 DiagMoveAssign,
219 DiagMain,
220 DiagConstexpr,
221 DiagAutoRet,
222 DiagVarargs,
223 };
224 bool Diagnosed = false;
225 auto DiagInvalid = [&](InvalidFuncDiag ID) {
226 S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword;
227 Diagnosed = true;
228 return false;
229 };
230
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);
244
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);
252
253 return !Diagnosed;
254}
255
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000256static 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);
Eric Fiselierc8efda72016-10-27 18:43:28 +0000263
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000264 assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
265 const auto &Functions = Operators.asUnresolvedSet();
266 bool IsOverloaded =
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());
273 assert(CoawaitOp);
274 return CoawaitOp;
275}
Eric Fiselierc8efda72016-10-27 18:43:28 +0000276
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000277/// Build a call to 'operator co_await' if there is a suitable operator for
278/// the given expression.
279static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, SourceLocation Loc,
280 Expr *E,
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);
285}
Eric Fiselierc8efda72016-10-27 18:43:28 +0000286
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000287static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S,
288 SourceLocation Loc, Expr *E) {
289 ExprResult R = buildOperatorCoawaitLookupExpr(SemaRef, S, Loc);
290 if (R.isInvalid())
291 return ExprError();
292 return buildOperatorCoawaitCall(SemaRef, Loc, E,
293 cast<UnresolvedLookupExpr>(R.get()));
Richard Smithcfd53b42015-10-22 06:13:50 +0000294}
295
Gor Nishanov8df64e92016-10-27 16:28:31 +0000296static Expr *buildBuiltinCall(Sema &S, SourceLocation Loc, Builtin::ID Id,
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000297 MultiExprArg CallArgs) {
Gor Nishanov8df64e92016-10-27 16:28:31 +0000298 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);
301
302 auto *BuiltInDecl = R.getAsSingle<FunctionDecl>();
303 assert(BuiltInDecl && "failed to find builtin declaration");
304
305 ExprResult DeclRef =
306 S.BuildDeclRefExpr(BuiltInDecl, BuiltInDecl->getType(), VK_LValue, Loc);
307 assert(DeclRef.isUsable() && "Builtin reference cannot fail");
308
309 ExprResult Call =
310 S.ActOnCallExpr(/*Scope=*/nullptr, DeclRef.get(), Loc, CallArgs, Loc);
311
312 assert(!Call.isInvalid() && "Call to builtin cannot fail!");
313 return Call.get();
314}
315
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000316static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType,
317 SourceLocation Loc) {
318 QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc);
319 if (CoroHandleType.isNull())
320 return ExprError();
321
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)
327 << "from_address";
328 return ExprError();
329 }
330
331 Expr *FramePtr =
332 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
333
334 CXXScopeSpec SS;
335 ExprResult FromAddr =
336 S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
337 if (FromAddr.isInvalid())
338 return ExprError();
339
340 return S.ActOnCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc);
341}
Richard Smithcfd53b42015-10-22 06:13:50 +0000342
Richard Smith9f690bd2015-10-27 06:02:45 +0000343struct ReadySuspendResumeResult {
Eric Fiselierd978e532017-05-28 18:21:12 +0000344 enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume };
Richard Smith9f690bd2015-10-27 06:02:45 +0000345 Expr *Results[3];
Gor Nishanovce43bd22017-03-11 01:30:17 +0000346 OpaqueValueExpr *OpaqueValue;
347 bool IsInvalid;
Richard Smith9f690bd2015-10-27 06:02:45 +0000348};
349
Richard Smith23da82c2015-11-20 22:40:06 +0000350static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc,
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000351 StringRef Name, MultiExprArg Args) {
Richard Smith23da82c2015-11-20 22:40:06 +0000352 DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc);
353
354 // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&.
355 CXXScopeSpec SS;
356 ExprResult Result = S.BuildMemberReferenceExpr(
357 Base, Base->getType(), Loc, /*IsPtr=*/false, SS,
358 SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr,
359 /*Scope=*/nullptr);
360 if (Result.isInvalid())
361 return ExprError();
362
363 return S.ActOnCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr);
364}
365
Gor Nishanov0f333002017-08-25 04:46:54 +0000366// 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
369// coroutine.
370static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E,
371 SourceLocation Loc) {
372 if (RetType->isReferenceType())
373 return nullptr;
374 Type const *T = RetType.getTypePtr();
375 if (!T->isClassType() && !T->isStructureType())
376 return nullptr;
377
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
381
382 ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", None);
383 if (AddressExpr.isInvalid())
384 return nullptr;
385
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,
389 JustAddress);
390}
391
Richard Smith9f690bd2015-10-27 06:02:45 +0000392/// Build calls to await_ready, await_suspend, and await_resume for a co_await
393/// expression.
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000394static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise,
395 SourceLocation Loc, Expr *E) {
Gor Nishanovce43bd22017-03-11 01:30:17 +0000396 OpaqueValueExpr *Operand = new (S.Context)
397 OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E);
398
Richard Smith9f690bd2015-10-27 06:02:45 +0000399 // Assume invalid until we see otherwise.
Gor Nishanovce43bd22017-03-11 01:30:17 +0000400 ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/true};
Richard Smith9f690bd2015-10-27 06:02:45 +0000401
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000402 ExprResult CoroHandleRes = buildCoroutineHandle(S, CoroPromise->getType(), Loc);
403 if (CoroHandleRes.isInvalid())
404 return Calls;
405 Expr *CoroHandle = CoroHandleRes.get();
406
Richard Smith9f690bd2015-10-27 06:02:45 +0000407 const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"};
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000408 MultiExprArg Args[] = {None, CoroHandle, None};
Richard Smith9f690bd2015-10-27 06:02:45 +0000409 for (size_t I = 0, N = llvm::array_lengthof(Funcs); I != N; ++I) {
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000410 ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], Args[I]);
Richard Smith9f690bd2015-10-27 06:02:45 +0000411 if (Result.isInvalid())
412 return Calls;
413 Calls.Results[I] = Result.get();
414 }
415
Eric Fiselierd978e532017-05-28 18:21:12 +0000416 // Assume the calls are valid; all further checking should make them invalid.
Richard Smith9f690bd2015-10-27 06:02:45 +0000417 Calls.IsInvalid = false;
Eric Fiselierd978e532017-05-28 18:21:12 +0000418
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
424 // to bool.
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;
432 }
433 Calls.Results[ACT::ACT_Ready] = Conv.get();
434 }
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.
Eric Fiselier84ee7ff2017-05-31 23:41:11 +0000440 QualType RetType = AwaitSuspend->getCallReturnType(S.Context);
Gor Nishanovdb419a62017-09-05 19:31:52 +0000441
Gor Nishanov0f333002017-08-25 04:46:54 +0000442 // Experimental support for coroutine_handle returning await_suspend.
443 if (Expr *TailCallSuspend = maybeTailCall(S, RetType, AwaitSuspend, Loc))
444 Calls.Results[ACT::ACT_Suspend] = TailCallSuspend;
445 else {
446 // non-class prvalues always have cv-unqualified types
Gor Nishanov0f333002017-08-25 04:46:54 +0000447 if (RetType->isReferenceType() ||
Gor Nishanovdb419a62017-09-05 19:31:52 +0000448 (!RetType->isBooleanType() && !RetType->isVoidType())) {
Gor Nishanov0f333002017-08-25 04:46:54 +0000449 S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(),
450 diag::err_await_suspend_invalid_return_type)
451 << RetType;
452 S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
453 << AwaitSuspend->getDirectCallee();
454 Calls.IsInvalid = true;
455 }
Eric Fiselierd978e532017-05-28 18:21:12 +0000456 }
457 }
458
Richard Smith9f690bd2015-10-27 06:02:45 +0000459 return Calls;
460}
461
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000462static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise,
463 SourceLocation Loc, StringRef Name,
464 MultiExprArg Args) {
465
466 // Form a reference to the promise.
467 ExprResult PromiseRef = S.BuildDeclRefExpr(
468 Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc);
469 if (PromiseRef.isInvalid())
470 return ExprError();
471
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000472 return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args);
473}
474
475VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) {
476 assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
477 auto *FD = cast<FunctionDecl>(CurContext);
Eric Fiselier166c6e62017-07-10 01:27:22 +0000478 bool IsThisDependentType = [&] {
479 if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(FD))
480 return MD->isInstance() && MD->getThisType(Context)->isDependentType();
481 else
482 return false;
483 }();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000484
Eric Fiselier166c6e62017-07-10 01:27:22 +0000485 QualType T = FD->getType()->isDependentType() || IsThisDependentType
486 ? Context.DependentTy
487 : lookupPromiseType(*this, FD, Loc);
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000488 if (T.isNull())
489 return nullptr;
490
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())
496 return nullptr;
497 ActOnUninitializedDecl(VD);
Eric Fiselier37b8a372017-05-31 19:36:59 +0000498 FD->addDecl(VD);
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000499 assert(!VD->isInvalidDecl());
500 return VD;
501}
502
503/// Check that this is a context in which a coroutine suspension can appear.
504static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc,
Eric Fiseliercac0a592017-03-11 02:35:37 +0000505 StringRef Keyword,
506 bool IsImplicit = false) {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000507 if (!isValidCoroutineContext(S, Loc, Keyword))
508 return nullptr;
509
510 assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope");
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000511
512 auto *ScopeInfo = S.getCurFunction();
513 assert(ScopeInfo && "missing function scope for function");
514
Eric Fiseliercac0a592017-03-11 02:35:37 +0000515 if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit)
516 ScopeInfo->setFirstCoroutineStmt(Loc, Keyword);
517
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000518 if (ScopeInfo->CoroutinePromise)
519 return ScopeInfo;
520
521 ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc);
522 if (!ScopeInfo->CoroutinePromise)
523 return nullptr;
524
525 return ScopeInfo;
526}
527
Eric Fiselierb936a392017-06-14 03:24:55 +0000528bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc,
529 StringRef Keyword) {
530 if (!checkCoroutineContext(*this, KWLoc, Keyword))
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000531 return false;
Eric Fiselierb936a392017-06-14 03:24:55 +0000532 auto *ScopeInfo = getCurFunction();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000533 assert(ScopeInfo->CoroutinePromise);
534
535 // If we have existing coroutine statements then we have already built
536 // the initial and final suspend points.
537 if (!ScopeInfo->NeedsCoroutineSuspends)
538 return true;
539
540 ScopeInfo->setNeedsCoroutineSuspends(false);
541
Eric Fiselierb936a392017-06-14 03:24:55 +0000542 auto *Fn = cast<FunctionDecl>(CurContext);
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000543 SourceLocation Loc = Fn->getLocation();
544 // Build the initial suspend point
545 auto buildSuspends = [&](StringRef Name) mutable -> StmtResult {
546 ExprResult Suspend =
Eric Fiselierb936a392017-06-14 03:24:55 +0000547 buildPromiseCall(*this, ScopeInfo->CoroutinePromise, Loc, Name, None);
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000548 if (Suspend.isInvalid())
549 return StmtError();
Eric Fiselierb936a392017-06-14 03:24:55 +0000550 Suspend = buildOperatorCoawaitCall(*this, SC, Loc, Suspend.get());
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000551 if (Suspend.isInvalid())
552 return StmtError();
Eric Fiselierb936a392017-06-14 03:24:55 +0000553 Suspend = BuildResolvedCoawaitExpr(Loc, Suspend.get(),
554 /*IsImplicit*/ true);
555 Suspend = ActOnFinishFullExpr(Suspend.get());
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000556 if (Suspend.isInvalid()) {
Eric Fiselierb936a392017-06-14 03:24:55 +0000557 Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required)
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000558 << ((Name == "initial_suspend") ? 0 : 1);
Eric Fiselierb936a392017-06-14 03:24:55 +0000559 Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword;
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000560 return StmtError();
561 }
562 return cast<Stmt>(Suspend.get());
563 };
564
565 StmtResult InitSuspend = buildSuspends("initial_suspend");
566 if (InitSuspend.isInvalid())
567 return true;
568
569 StmtResult FinalSuspend = buildSuspends("final_suspend");
570 if (FinalSuspend.isInvalid())
571 return true;
572
573 ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get());
574
575 return true;
576}
577
Richard Smith9f690bd2015-10-27 06:02:45 +0000578ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) {
Eric Fiselierb936a392017-06-14 03:24:55 +0000579 if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) {
Eric Fiseliera5465282016-09-29 21:47:39 +0000580 CorrectDelayedTyposInExpr(E);
581 return ExprError();
582 }
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000583
Richard Smith10610f72015-11-20 22:57:24 +0000584 if (E->getType()->isPlaceholderType()) {
585 ExprResult R = CheckPlaceholderExpr(E);
586 if (R.isInvalid()) return ExprError();
587 E = R.get();
588 }
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000589 ExprResult Lookup = buildOperatorCoawaitLookupExpr(*this, S, Loc);
590 if (Lookup.isInvalid())
591 return ExprError();
592 return BuildUnresolvedCoawaitExpr(Loc, E,
593 cast<UnresolvedLookupExpr>(Lookup.get()));
594}
Richard Smith10610f72015-11-20 22:57:24 +0000595
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000596ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *E,
Eric Fiseliercac0a592017-03-11 02:35:37 +0000597 UnresolvedLookupExpr *Lookup) {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000598 auto *FSI = checkCoroutineContext(*this, Loc, "co_await");
599 if (!FSI)
600 return ExprError();
601
602 if (E->getType()->isPlaceholderType()) {
603 ExprResult R = CheckPlaceholderExpr(E);
604 if (R.isInvalid())
605 return ExprError();
606 E = R.get();
607 }
608
609 auto *Promise = FSI->CoroutinePromise;
610 if (Promise->getType()->isDependentType()) {
611 Expr *Res =
612 new (Context) DependentCoawaitExpr(Loc, Context.DependentTy, E, Lookup);
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000613 return Res;
614 }
615
616 auto *RD = Promise->getType()->getAsCXXRecordDecl();
617 if (lookupMember(*this, "await_transform", RD, Loc)) {
618 ExprResult R = buildPromiseCall(*this, Promise, Loc, "await_transform", E);
619 if (R.isInvalid()) {
620 Diag(Loc,
621 diag::note_coroutine_promise_implicit_await_transform_required_here)
622 << E->getSourceRange();
623 return ExprError();
624 }
625 E = R.get();
626 }
627 ExprResult Awaitable = buildOperatorCoawaitCall(*this, Loc, E, Lookup);
Richard Smith9f690bd2015-10-27 06:02:45 +0000628 if (Awaitable.isInvalid())
629 return ExprError();
Eric Fiseliera5465282016-09-29 21:47:39 +0000630
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000631 return BuildResolvedCoawaitExpr(Loc, Awaitable.get());
Richard Smith9f690bd2015-10-27 06:02:45 +0000632}
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000633
634ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *E,
635 bool IsImplicit) {
Eric Fiseliercac0a592017-03-11 02:35:37 +0000636 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit);
Richard Smith744b2242015-11-20 02:54:01 +0000637 if (!Coroutine)
638 return ExprError();
Richard Smith9f690bd2015-10-27 06:02:45 +0000639
Richard Smith9f690bd2015-10-27 06:02:45 +0000640 if (E->getType()->isPlaceholderType()) {
641 ExprResult R = CheckPlaceholderExpr(E);
642 if (R.isInvalid()) return ExprError();
643 E = R.get();
644 }
645
Richard Smith10610f72015-11-20 22:57:24 +0000646 if (E->getType()->isDependentType()) {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000647 Expr *Res = new (Context)
648 CoawaitExpr(Loc, Context.DependentTy, E, IsImplicit);
Richard Smith10610f72015-11-20 22:57:24 +0000649 return Res;
650 }
651
Richard Smith1f38edd2015-11-22 03:13:02 +0000652 // 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)
Tim Shen4a05bb82016-06-21 20:29:17 +0000655 E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
Richard Smith9f690bd2015-10-27 06:02:45 +0000656
657 // Build the await_ready, await_suspend, await_resume calls.
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000658 ReadySuspendResumeResult RSS =
659 buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E);
Richard Smith9f690bd2015-10-27 06:02:45 +0000660 if (RSS.IsInvalid)
661 return ExprError();
662
Gor Nishanovce43bd22017-03-11 01:30:17 +0000663 Expr *Res =
664 new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1],
665 RSS.Results[2], RSS.OpaqueValue, IsImplicit);
Eric Fiseliercac0a592017-03-11 02:35:37 +0000666
Richard Smithcfd53b42015-10-22 06:13:50 +0000667 return Res;
668}
669
Richard Smith9f690bd2015-10-27 06:02:45 +0000670ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) {
Eric Fiselierb936a392017-06-14 03:24:55 +0000671 if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) {
Eric Fiseliera5465282016-09-29 21:47:39 +0000672 CorrectDelayedTyposInExpr(E);
Richard Smith23da82c2015-11-20 22:40:06 +0000673 return ExprError();
Eric Fiseliera5465282016-09-29 21:47:39 +0000674 }
Richard Smith23da82c2015-11-20 22:40:06 +0000675
676 // Build yield_value call.
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000677 ExprResult Awaitable = buildPromiseCall(
678 *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E);
Richard Smith9f690bd2015-10-27 06:02:45 +0000679 if (Awaitable.isInvalid())
680 return ExprError();
Richard Smith23da82c2015-11-20 22:40:06 +0000681
682 // Build 'operator co_await' call.
683 Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get());
684 if (Awaitable.isInvalid())
685 return ExprError();
686
Richard Smith9f690bd2015-10-27 06:02:45 +0000687 return BuildCoyieldExpr(Loc, Awaitable.get());
688}
689ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) {
690 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield");
Richard Smith744b2242015-11-20 02:54:01 +0000691 if (!Coroutine)
692 return ExprError();
Richard Smithcfd53b42015-10-22 06:13:50 +0000693
Richard Smith10610f72015-11-20 22:57:24 +0000694 if (E->getType()->isPlaceholderType()) {
695 ExprResult R = CheckPlaceholderExpr(E);
696 if (R.isInvalid()) return ExprError();
697 E = R.get();
698 }
699
Richard Smithd7bed4d2015-11-22 02:57:17 +0000700 if (E->getType()->isDependentType()) {
701 Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, E);
Richard Smithd7bed4d2015-11-22 02:57:17 +0000702 return Res;
703 }
704
Richard Smith1f38edd2015-11-22 03:13:02 +0000705 // 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)
Tim Shen4a05bb82016-06-21 20:29:17 +0000708 E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
Richard Smithd7bed4d2015-11-22 02:57:17 +0000709
710 // Build the await_ready, await_suspend, await_resume calls.
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000711 ReadySuspendResumeResult RSS =
712 buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E);
Richard Smithd7bed4d2015-11-22 02:57:17 +0000713 if (RSS.IsInvalid)
714 return ExprError();
715
Eric Fiselierb936a392017-06-14 03:24:55 +0000716 Expr *Res =
717 new (Context) CoyieldExpr(Loc, E, RSS.Results[0], RSS.Results[1],
718 RSS.Results[2], RSS.OpaqueValue);
Eric Fiseliercac0a592017-03-11 02:35:37 +0000719
Richard Smithcfd53b42015-10-22 06:13:50 +0000720 return Res;
721}
722
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000723StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) {
Eric Fiselierb936a392017-06-14 03:24:55 +0000724 if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) {
Eric Fiseliera5465282016-09-29 21:47:39 +0000725 CorrectDelayedTyposInExpr(E);
726 return StmtError();
727 }
Richard Smith9f690bd2015-10-27 06:02:45 +0000728 return BuildCoreturnStmt(Loc, E);
729}
Gor Nishanov3e048bb2016-10-04 00:31:16 +0000730
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000731StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E,
732 bool IsImplicit) {
Eric Fiseliercac0a592017-03-11 02:35:37 +0000733 auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit);
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000734 if (!FSI)
Richard Smith71d403e2015-11-22 07:33:28 +0000735 return StmtError();
736
737 if (E && E->getType()->isPlaceholderType() &&
738 !E->getType()->isSpecificPlaceholderType(BuiltinType::Overload)) {
Richard Smith10610f72015-11-20 22:57:24 +0000739 ExprResult R = CheckPlaceholderExpr(E);
740 if (R.isInvalid()) return StmtError();
741 E = R.get();
742 }
743
Richard Smith4ba66602015-11-22 07:05:16 +0000744 // FIXME: If the operand is a reference to a variable that's about to go out
Richard Smith2af65c42015-11-24 02:34:39 +0000745 // of scope, we should treat the operand as an xvalue for this overload
Richard Smith4ba66602015-11-22 07:05:16 +0000746 // resolution.
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000747 VarDecl *Promise = FSI->CoroutinePromise;
Richard Smith4ba66602015-11-22 07:05:16 +0000748 ExprResult PC;
Eric Fiselier98131312016-10-06 21:23:38 +0000749 if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000750 PC = buildPromiseCall(*this, Promise, Loc, "return_value", E);
Richard Smith4ba66602015-11-22 07:05:16 +0000751 } else {
752 E = MakeFullDiscardedValueExpr(E).get();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000753 PC = buildPromiseCall(*this, Promise, Loc, "return_void", None);
Richard Smith4ba66602015-11-22 07:05:16 +0000754 }
755 if (PC.isInvalid())
756 return StmtError();
757
758 Expr *PCE = ActOnFinishFullExpr(PC.get()).get();
759
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000760 Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit);
Richard Smithcfd53b42015-10-22 06:13:50 +0000761 return Res;
762}
763
Eric Fiselierf692e7d2017-04-18 03:12:48 +0000764/// Look up the std::nothrow object.
765static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) {
766 NamespaceDecl *Std = S.getStdNamespace();
767 assert(Std && "Should already be diagnosed");
768
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
774 // needed.
775 S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found);
776 return nullptr;
777 }
778
Eric Fiselierf692e7d2017-04-18 03:12:48 +0000779 auto *VD = Result.getAsSingle<VarDecl>();
780 if (!VD) {
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);
785 return nullptr;
786 }
787
788 ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc);
789 if (DR.isInvalid())
790 return nullptr;
791
792 return DR.get();
793}
794
Gor Nishanov8df64e92016-10-27 16:28:31 +0000795// Find an appropriate delete for the promise.
796static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc,
797 QualType PromiseType) {
798 FunctionDecl *OperatorDelete = nullptr;
799
800 DeclarationName DeleteName =
801 S.Context.DeclarationNames.getCXXOperatorName(OO_Delete);
802
803 auto *PointeeRD = PromiseType->getAsCXXRecordDecl();
804 assert(PointeeRD && "PromiseType must be a CxxRecordDecl type");
805
806 if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete))
807 return nullptr;
808
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);
815 }
816 S.MarkFunctionReferenced(Loc, OperatorDelete);
817 return OperatorDelete;
818}
819
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000820
821void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) {
822 FunctionScopeInfo *Fn = getCurFunction();
Eric Fiselierda8f9b52017-05-25 02:16:53 +0000823 assert(Fn && Fn->isCoroutine() && "not a coroutine");
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000824 if (!Body) {
825 assert(FD->isInvalidDecl() &&
826 "a null body is only allowed for invalid declarations");
827 return;
828 }
Eric Fiselierda8f9b52017-05-25 02:16:53 +0000829 // We have a function that uses coroutine keywords, but we failed to build
830 // the promise type.
831 if (!Fn->CoroutinePromise)
832 return FD->setInvalidDecl();
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000833
834 if (isa<CoroutineBodyStmt>(Body)) {
Gor Nishanov29ff6382017-05-24 14:34:19 +0000835 // Nothing todo. the body is already a transformed coroutine body statement.
Gor Nishanov6dcb0eb2017-03-09 03:09:43 +0000836 return;
837 }
838
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000839 // Coroutines [stmt.return]p1:
840 // A return statement shall not appear in a coroutine.
841 if (Fn->FirstReturnLoc.isValid()) {
Eric Fiseliercac0a592017-03-11 02:35:37 +0000842 assert(Fn->FirstCoroutineStmtLoc.isValid() &&
843 "first coroutine location not set");
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000844 Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine);
Eric Fiseliercac0a592017-03-11 02:35:37 +0000845 Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
846 << Fn->getFirstCoroutineStmtKeyword();
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000847 }
Eric Fiselierbee782b2017-04-03 19:21:00 +0000848 CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body);
849 if (Builder.isInvalid() || !Builder.buildStatements())
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000850 return FD->setInvalidDecl();
851
852 // Build body for the coroutine wrapper statement.
853 Body = CoroutineBodyStmt::Create(Context, Builder);
854}
855
Eric Fiselierbee782b2017-04-03 19:21:00 +0000856CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD,
857 sema::FunctionScopeInfo &Fn,
858 Stmt *Body)
859 : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()),
860 IsPromiseDependentType(
861 !Fn.CoroutinePromise ||
862 Fn.CoroutinePromise->getType()->isDependentType()) {
863 this->Body = Body;
864 if (!IsPromiseDependentType) {
865 PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl();
866 assert(PromiseRecordDecl && "Type should have already been checked");
867 }
868 this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend();
869}
870
871bool 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;
877}
878
879bool 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() &&
Gor Nishanov6a470682017-05-22 20:22:23 +0000884 makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() &&
885 makeNewAndDeleteExpr();
Eric Fiselierbee782b2017-04-03 19:21:00 +0000886 return this->IsValid;
887}
888
Eric Fiselierde7943b2017-06-03 00:22:18 +0000889bool CoroutineStmtBuilder::buildParameterMoves() {
890 assert(this->IsValid && "coroutine already invalid");
891 assert(this->ParamMoves.empty() && "param moves already built");
892 return this->IsValid = makeParamMoves();
893}
894
Eric Fiselierbee782b2017-04-03 19:21:00 +0000895bool CoroutineStmtBuilder::makePromiseStmt() {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000896 // 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())
901 return false;
902
903 this->Promise = PromiseStmt.get();
904 return true;
905}
906
Eric Fiselierbee782b2017-04-03 19:21:00 +0000907bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000908 if (Fn.hasInvalidCoroutineSuspends())
909 return false;
910 this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first);
911 this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second);
912 return true;
913}
914
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000915static 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())
923 return true;
924 else
925 Loc = Decl->getLocation();
926 }
927 }
928
929 S.Diag(
930 Loc,
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();
935 return false;
936}
937
Eric Fiselierbee782b2017-04-03 19:21:00 +0000938bool CoroutineStmtBuilder::makeReturnOnAllocFailure() {
939 assert(!IsPromiseDependentType &&
940 "cannot make statement while the promise type is dependent");
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000941
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().
947
948 DeclarationName DN =
949 S.PP.getIdentifierInfo("get_return_object_on_allocation_failure");
950 LookupResult Found(S, DN, Loc, Sema::LookupMemberName);
Eric Fiselierbee782b2017-04-03 19:21:00 +0000951 if (!S.LookupQualifiedName(Found, PromiseRecordDecl))
952 return true;
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000953
954 CXXScopeSpec SS;
955 ExprResult DeclNameExpr =
956 S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
Eric Fiselierbee782b2017-04-03 19:21:00 +0000957 if (DeclNameExpr.isInvalid())
958 return false;
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000959
960 if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn))
961 return false;
962
963 ExprResult ReturnObjectOnAllocationFailure =
964 S.ActOnCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc);
Eric Fiselierbee782b2017-04-03 19:21:00 +0000965 if (ReturnObjectOnAllocationFailure.isInvalid())
966 return false;
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000967
Gor Nishanovc4a19082017-03-28 02:51:45 +0000968 StmtResult ReturnStmt =
969 S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get());
Gor Nishanov6a470682017-05-22 20:22:23 +0000970 if (ReturnStmt.isInvalid()) {
Eric Fiselierfc50f622017-05-25 14:59:39 +0000971 S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here)
972 << DN;
Gor Nishanov6a470682017-05-22 20:22:23 +0000973 S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
974 << Fn.getFirstCoroutineStmtKeyword();
Eric Fiselierbee782b2017-04-03 19:21:00 +0000975 return false;
Gor Nishanov6a470682017-05-22 20:22:23 +0000976 }
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000977
978 this->ReturnStmtOnAllocFailure = ReturnStmt.get();
979 return true;
980}
981
Eric Fiselierbee782b2017-04-03 19:21:00 +0000982bool CoroutineStmtBuilder::makeNewAndDeleteExpr() {
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000983 // Form and check allocation and deallocation calls.
Eric Fiselierbee782b2017-04-03 19:21:00 +0000984 assert(!IsPromiseDependentType &&
985 "cannot make statement while the promise type is dependent");
Eric Fiselier20f25cb2017-03-06 23:38:15 +0000986 QualType PromiseType = Fn.CoroutinePromise->getType();
Gor Nishanov8df64e92016-10-27 16:28:31 +0000987
988 if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type))
989 return false;
990
Eric Fiselierf692e7d2017-04-18 03:12:48 +0000991 const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr;
992
Gor Nishanov8df64e92016-10-27 16:28:31 +0000993 // FIXME: Add support for stateful allocators.
994
995 FunctionDecl *OperatorNew = nullptr;
996 FunctionDecl *OperatorDelete = nullptr;
997 FunctionDecl *UnusedResult = nullptr;
998 bool PassAlignment = false;
Eric Fiselierf747f532017-04-18 05:08:08 +0000999 SmallVector<Expr *, 1> PlacementArgs;
Gor Nishanov8df64e92016-10-27 16:28:31 +00001000
1001 S.FindAllocationFunctions(Loc, SourceRange(),
1002 /*UseGlobal*/ false, PromiseType,
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001003 /*isArray*/ false, PassAlignment, PlacementArgs,
1004 OperatorNew, UnusedResult);
Gor Nishanov8df64e92016-10-27 16:28:31 +00001005
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001006 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
1010 // instead.
1011 if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) {
1012 auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc);
1013 if (!StdNoThrow)
1014 return false;
Eric Fiselierf747f532017-04-18 05:08:08 +00001015 PlacementArgs = {StdNoThrow};
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001016 OperatorNew = nullptr;
1017 S.FindAllocationFunctions(Loc, SourceRange(),
1018 /*UseGlobal*/ true, PromiseType,
1019 /*isArray*/ false, PassAlignment, PlacementArgs,
1020 OperatorNew, UnusedResult);
1021 }
Gor Nishanov8df64e92016-10-27 16:28:31 +00001022
Eric Fiselierc5128752017-04-18 05:30:39 +00001023 assert(OperatorNew && "expected definition of operator new to be found");
1024
1025 if (RequiresNoThrowAlloc) {
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001026 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)
1030 << OperatorNew;
1031 S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
1032 << OperatorNew;
1033 return false;
1034 }
1035 }
1036
1037 if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr)
Gor Nishanov8df64e92016-10-27 16:28:31 +00001038 return false;
1039
1040 Expr *FramePtr =
1041 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
1042
1043 Expr *FrameSize =
1044 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_size, {});
1045
1046 // Make new call.
1047
1048 ExprResult NewRef =
1049 S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc);
1050 if (NewRef.isInvalid())
1051 return false;
1052
Eric Fiselierf747f532017-04-18 05:08:08 +00001053 SmallVector<Expr *, 2> NewArgs(1, FrameSize);
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001054 for (auto Arg : PlacementArgs)
1055 NewArgs.push_back(Arg);
1056
Gor Nishanov8df64e92016-10-27 16:28:31 +00001057 ExprResult NewExpr =
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001058 S.ActOnCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc);
1059 NewExpr = S.ActOnFinishFullExpr(NewExpr.get());
Gor Nishanov8df64e92016-10-27 16:28:31 +00001060 if (NewExpr.isInvalid())
1061 return false;
1062
Gor Nishanov8df64e92016-10-27 16:28:31 +00001063 // Make delete call.
1064
1065 QualType OpDeleteQualType = OperatorDelete->getType();
1066
1067 ExprResult DeleteRef =
1068 S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc);
1069 if (DeleteRef.isInvalid())
1070 return false;
1071
1072 Expr *CoroFree =
1073 buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_free, {FramePtr});
1074
1075 SmallVector<Expr *, 2> DeleteArgs{CoroFree};
1076
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);
1082
1083 ExprResult DeleteExpr =
1084 S.ActOnCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc);
Eric Fiselierf692e7d2017-04-18 03:12:48 +00001085 DeleteExpr = S.ActOnFinishFullExpr(DeleteExpr.get());
Gor Nishanov8df64e92016-10-27 16:28:31 +00001086 if (DeleteExpr.isInvalid())
1087 return false;
1088
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001089 this->Allocate = NewExpr.get();
1090 this->Deallocate = DeleteExpr.get();
Gor Nishanov8df64e92016-10-27 16:28:31 +00001091
1092 return true;
1093}
1094
Eric Fiselierbee782b2017-04-03 19:21:00 +00001095bool CoroutineStmtBuilder::makeOnFallthrough() {
1096 assert(!IsPromiseDependentType &&
1097 "cannot make statement while the promise type is dependent");
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001098
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.
Eric Fiselierfc50f622017-05-25 14:59:39 +00001102 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);
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001107
Eric Fiselier709d1b32016-10-27 07:30:31 +00001108 StmtResult Fallthrough;
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001109 if (HasRVoid && HasRValue) {
1110 // FIXME Improve this diagnostic
Eric Fiselierfc50f622017-05-25 14:59:39 +00001111 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();
1120 return false;
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)
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001128 << PromiseRecordDecl;
1129 return false;
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.
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001134 Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr,
1135 /*IsImplicit*/false);
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001136 Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get());
1137 if (Fallthrough.isInvalid())
1138 return false;
Eric Fiselier709d1b32016-10-27 07:30:31 +00001139 }
Richard Smith2af65c42015-11-24 02:34:39 +00001140
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001141 this->OnFallthrough = Fallthrough.get();
1142 return true;
1143}
1144
Eric Fiselierbee782b2017-04-03 19:21:00 +00001145bool CoroutineStmtBuilder::makeOnException() {
Eric Fiseliera9fdb342017-03-23 00:33:33 +00001146 // Try to form 'p.unhandled_exception();'
Eric Fiselierbee782b2017-04-03 19:21:00 +00001147 assert(!IsPromiseDependentType &&
1148 "cannot make statement while the promise type is dependent");
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001149
Eric Fiseliera9fdb342017-03-23 00:33:33 +00001150 const bool RequireUnhandledException = S.getLangOpts().CXXExceptions;
1151
1152 if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) {
1153 auto DiagID =
1154 RequireUnhandledException
1155 ? diag::err_coroutine_promise_unhandled_exception_required
1156 : diag::
1157 warn_coroutine_promise_unhandled_exception_required_with_exceptions;
1158 S.Diag(Loc, DiagID) << PromiseRecordDecl;
Gor Nishanov29ff6382017-05-24 14:34:19 +00001159 S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
1160 << PromiseRecordDecl;
Eric Fiseliera9fdb342017-03-23 00:33:33 +00001161 return !RequireUnhandledException;
1162 }
1163
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001164 // If exceptions are disabled, don't try to build OnException.
1165 if (!S.getLangOpts().CXXExceptions)
1166 return true;
1167
Eric Fiseliera9fdb342017-03-23 00:33:33 +00001168 ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc,
1169 "unhandled_exception", None);
1170 UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc);
1171 if (UnhandledException.isInvalid())
1172 return false;
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001173
Gor Nishanov5b050e42017-05-22 22:33:17 +00001174 // 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();
1180 return false;
1181 }
1182
Eric Fiseliera9fdb342017-03-23 00:33:33 +00001183 this->OnException = UnhandledException.get();
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001184 return true;
1185}
1186
Eric Fiselierbee782b2017-04-03 19:21:00 +00001187bool CoroutineStmtBuilder::makeReturnObject() {
Richard Smith2af65c42015-11-24 02:34:39 +00001188 // Build implicit 'p.get_return_object()' expression and form initialization
1189 // of return type from it.
1190 ExprResult ReturnObject =
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001191 buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None);
Richard Smith2af65c42015-11-24 02:34:39 +00001192 if (ReturnObject.isInvalid())
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001193 return false;
Richard Smith2af65c42015-11-24 02:34:39 +00001194
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001195 this->ReturnValue = ReturnObject.get();
1196 return true;
1197}
1198
Gor Nishanov6a470682017-05-22 20:22:23 +00001199static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) {
1200 if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) {
1201 auto *MethodDecl = MbrRef->getMethodDecl();
Eric Fiselierfc50f622017-05-25 14:59:39 +00001202 S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here)
1203 << MethodDecl;
Gor Nishanov6a470682017-05-22 20:22:23 +00001204 }
1205 S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
1206 << Fn.getFirstCoroutineStmtKeyword();
1207}
1208
1209bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
1210 assert(!IsPromiseDependentType &&
1211 "cannot make statement while the promise type is dependent");
1212 assert(this->ReturnValue && "ReturnValue must be already formed");
1213
1214 QualType const GroType = this->ReturnValue->getType();
1215 assert(!GroType->isDependentType() &&
1216 "get_return_object type must no longer be dependent");
1217
1218 QualType const FnRetType = FD.getReturnType();
1219 assert(!FnRetType->isDependentType() &&
1220 "get_return_object type must no longer be dependent");
1221
1222 if (FnRetType->isVoidType()) {
1223 ExprResult Res = S.ActOnFinishFullExpr(this->ReturnValue, Loc);
1224 if (Res.isInvalid())
1225 return false;
1226
1227 this->ResultDecl = Res.get();
1228 return true;
1229 }
1230
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);
1237 return false;
1238 }
1239
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);
1244
1245 S.CheckVariableDeclarationType(GroDecl);
1246 if (GroDecl->isInvalidDecl())
1247 return false;
1248
1249 InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl);
1250 ExprResult Res = S.PerformMoveOrCopyInitialization(Entity, nullptr, GroType,
1251 this->ReturnValue);
1252 if (Res.isInvalid())
1253 return false;
1254
1255 Res = S.ActOnFinishFullExpr(Res.get());
1256 if (Res.isInvalid())
1257 return false;
1258
1259 if (GroType == FnRetType) {
1260 GroDecl->setNRVOVariable(true);
1261 }
1262
1263 S.AddInitializerToDecl(GroDecl, Res.get(),
1264 /*DirectInit=*/false);
1265
1266 S.FinalizeDeclaration(GroDecl);
1267
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())
1273 return false;
1274
1275 this->ResultDecl = GroDeclStmt.get();
1276
1277 ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc);
1278 if (declRef.isInvalid())
1279 return false;
1280
1281 StmtResult ReturnStmt = S.BuildReturnStmt(Loc, declRef.get());
1282 if (ReturnStmt.isInvalid()) {
1283 noteMemberDeclaredHere(S, ReturnValue, Fn);
1284 return false;
1285 }
1286
1287 this->ReturnStmt = ReturnStmt.get();
1288 return true;
1289}
1290
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001291// Create a static_cast\<T&&>(expr).
1292static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) {
1293 if (T.isNull())
1294 T = E->getType();
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);
1300
1301 return S
1302 .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E,
1303 SourceRange(ExprLoc, ExprLoc), E->getSourceRange())
1304 .get();
1305}
1306
Eric Fiselierde7943b2017-06-03 00:22:18 +00001307
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001308/// \brief Build a variable declaration for move parameter.
1309static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type,
Eric Fiselierde7943b2017-06-03 00:22:18 +00001310 IdentifierInfo *II) {
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001311 TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc);
1312 VarDecl *Decl =
Eric Fiselierde7943b2017-06-03 00:22:18 +00001313 VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type, TInfo, SC_None);
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001314 Decl->setImplicit();
1315 return Decl;
1316}
1317
Eric Fiselierbee782b2017-04-03 19:21:00 +00001318bool CoroutineStmtBuilder::makeParamMoves() {
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001319 for (auto *paramDecl : FD.parameters()) {
1320 auto Ty = paramDecl->getType();
1321 if (Ty->isDependentType())
1322 continue;
1323
1324 // No need to copy scalars, llvm will take care of them.
1325 if (Ty->getAsCXXRecordDecl()) {
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001326 ExprResult ParamRef =
1327 S.BuildDeclRefExpr(paramDecl, paramDecl->getType(),
1328 ExprValueKind::VK_LValue, Loc); // FIXME: scope?
1329 if (ParamRef.isInvalid())
1330 return false;
1331
1332 Expr *RCast = castForMoving(S, ParamRef.get());
1333
Eric Fiselierde7943b2017-06-03 00:22:18 +00001334 auto D = buildVarDecl(S, Loc, Ty, paramDecl->getIdentifier());
Gor Nishanov33d5fd22017-05-24 20:09:14 +00001335 S.AddInitializerToDecl(D, RCast, /*DirectInit=*/true);
1336
1337 // Convert decl to a statement.
1338 StmtResult Stmt = S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(D), Loc, Loc);
1339 if (Stmt.isInvalid())
1340 return false;
1341
1342 ParamMovesVector.push_back(Stmt.get());
1343 }
1344 }
1345
1346 // Convert to ArrayRef in CtorArgs structure that builder inherits from.
1347 ParamMoves = ParamMovesVector;
Gor Nishanovbbe1c072017-02-13 05:05:02 +00001348 return true;
Richard Smithcfd53b42015-10-22 06:13:50 +00001349}
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001350
1351StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) {
1352 CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args);
1353 if (!Res)
1354 return StmtError();
1355 return Res;
1356}