Fangrui Song | 524b3c1 | 2019-03-01 06:49:51 +0000 | [diff] [blame] | 1 | //===-- SemaCoroutine.cpp - Semantic Analysis for Coroutines --------------===// |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements semantic analysis for C++ Coroutines. |
| 10 | // |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 11 | // This file contains references to sections of the Coroutines TS, which |
| 12 | // can be found at http://wg21.link/coroutines. |
| 13 | // |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 16 | #include "CoroutineStmtBuilder.h" |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTLambda.h" |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/ExprCXX.h" |
| 20 | #include "clang/AST/StmtCXX.h" |
| 21 | #include "clang/Lex/Preprocessor.h" |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Initialization.h" |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Overload.h" |
Reid Kleckner | 04f9bca | 2018-03-07 22:48:35 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ScopeInfo.h" |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 25 | #include "clang/Sema/SemaInternal.h" |
| 26 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace sema; |
| 29 | |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 30 | static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, |
| 31 | SourceLocation Loc, bool &Res) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 32 | DeclarationName DN = S.PP.getIdentifierInfo(Name); |
| 33 | LookupResult LR(S, DN, Loc, Sema::LookupMemberName); |
| 34 | // Suppress diagnostics when a private member is selected. The same warnings |
| 35 | // will be produced again when building the call. |
| 36 | LR.suppressDiagnostics(); |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 37 | Res = S.LookupQualifiedName(LR, RD); |
| 38 | return LR; |
| 39 | } |
| 40 | |
| 41 | static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, |
| 42 | SourceLocation Loc) { |
| 43 | bool Res; |
| 44 | lookupMember(S, Name, RD, Loc, Res); |
| 45 | return Res; |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 48 | /// Look up the std::coroutine_traits<...>::promise_type for the given |
| 49 | /// function type. |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 50 | static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD, |
| 51 | SourceLocation KwLoc) { |
| 52 | const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>(); |
| 53 | const SourceLocation FuncLoc = FD->getLocation(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 54 | // FIXME: Cache std::coroutine_traits once we've found it. |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 55 | NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace(); |
| 56 | if (!StdExp) { |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 57 | S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found) |
| 58 | << "std::experimental::coroutine_traits"; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 59 | return QualType(); |
| 60 | } |
| 61 | |
Brian Gesiak | 3e65d9a | 2018-07-14 18:21:44 +0000 | [diff] [blame] | 62 | ClassTemplateDecl *CoroTraits = S.lookupCoroutineTraits(KwLoc, FuncLoc); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 63 | if (!CoroTraits) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 64 | return QualType(); |
| 65 | } |
| 66 | |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 67 | // Form template argument list for coroutine_traits<R, P1, P2, ...> according |
| 68 | // to [dcl.fct.def.coroutine]3 |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 69 | TemplateArgumentListInfo Args(KwLoc, KwLoc); |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 70 | auto AddArg = [&](QualType T) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 71 | Args.addArgument(TemplateArgumentLoc( |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 72 | TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc))); |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 73 | }; |
| 74 | AddArg(FnType->getReturnType()); |
| 75 | // If the function is a non-static member function, add the type |
| 76 | // of the implicit object parameter before the formal parameters. |
| 77 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 78 | if (MD->isInstance()) { |
| 79 | // [over.match.funcs]4 |
| 80 | // For non-static member functions, the type of the implicit object |
| 81 | // parameter is |
Eric Fiselier | bf166ce | 2017-07-10 02:52:34 +0000 | [diff] [blame] | 82 | // -- "lvalue reference to cv X" for functions declared without a |
| 83 | // ref-qualifier or with the & ref-qualifier |
| 84 | // -- "rvalue reference to cv X" for functions declared with the && |
| 85 | // ref-qualifier |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 86 | QualType T = MD->getThisType()->getAs<PointerType>()->getPointeeType(); |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 87 | T = FnType->getRefQualifier() == RQ_RValue |
| 88 | ? S.Context.getRValueReferenceType(T) |
| 89 | : S.Context.getLValueReferenceType(T, /*SpelledAsLValue*/ true); |
| 90 | AddArg(T); |
| 91 | } |
| 92 | } |
| 93 | for (QualType T : FnType->getParamTypes()) |
| 94 | AddArg(T); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 95 | |
| 96 | // Build the template-id. |
| 97 | QualType CoroTrait = |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 98 | S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 99 | if (CoroTrait.isNull()) |
| 100 | return QualType(); |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 101 | if (S.RequireCompleteType(KwLoc, CoroTrait, |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 102 | diag::err_coroutine_type_missing_specialization)) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 103 | return QualType(); |
| 104 | |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 105 | auto *RD = CoroTrait->getAsCXXRecordDecl(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 106 | assert(RD && "specialization of class template is not a class?"); |
| 107 | |
| 108 | // Look up the ::promise_type member. |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 109 | LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 110 | Sema::LookupOrdinaryName); |
| 111 | S.LookupQualifiedName(R, RD); |
| 112 | auto *Promise = R.getAsSingle<TypeDecl>(); |
| 113 | if (!Promise) { |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 114 | S.Diag(FuncLoc, |
| 115 | diag::err_implied_std_coroutine_traits_promise_type_not_found) |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 116 | << RD; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 117 | return QualType(); |
| 118 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 119 | // The promise type is required to be a class type. |
| 120 | QualType PromiseType = S.Context.getTypeDeclType(Promise); |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 121 | |
| 122 | auto buildElaboratedType = [&]() { |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 123 | auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, StdExp); |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 124 | NNS = NestedNameSpecifier::Create(S.Context, NNS, false, |
| 125 | CoroTrait.getTypePtr()); |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 126 | return S.Context.getElaboratedType(ETK_None, NNS, PromiseType); |
| 127 | }; |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 128 | |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 129 | if (!PromiseType->getAsCXXRecordDecl()) { |
| 130 | S.Diag(FuncLoc, |
| 131 | diag::err_implied_std_coroutine_traits_promise_type_not_class) |
| 132 | << buildElaboratedType(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 133 | return QualType(); |
| 134 | } |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 135 | if (S.RequireCompleteType(FuncLoc, buildElaboratedType(), |
| 136 | diag::err_coroutine_promise_type_incomplete)) |
| 137 | return QualType(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 138 | |
| 139 | return PromiseType; |
| 140 | } |
| 141 | |
Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 142 | /// Look up the std::experimental::coroutine_handle<PromiseType>. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 143 | static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType, |
| 144 | SourceLocation Loc) { |
| 145 | if (PromiseType.isNull()) |
| 146 | return QualType(); |
| 147 | |
| 148 | NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace(); |
| 149 | assert(StdExp && "Should already be diagnosed"); |
| 150 | |
| 151 | LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"), |
| 152 | Loc, Sema::LookupOrdinaryName); |
| 153 | if (!S.LookupQualifiedName(Result, StdExp)) { |
| 154 | S.Diag(Loc, diag::err_implied_coroutine_type_not_found) |
| 155 | << "std::experimental::coroutine_handle"; |
| 156 | return QualType(); |
| 157 | } |
| 158 | |
| 159 | ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>(); |
| 160 | if (!CoroHandle) { |
| 161 | Result.suppressDiagnostics(); |
| 162 | // We found something weird. Complain about the first thing we found. |
| 163 | NamedDecl *Found = *Result.begin(); |
| 164 | S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle); |
| 165 | return QualType(); |
| 166 | } |
| 167 | |
| 168 | // Form template argument list for coroutine_handle<Promise>. |
| 169 | TemplateArgumentListInfo Args(Loc, Loc); |
| 170 | Args.addArgument(TemplateArgumentLoc( |
| 171 | TemplateArgument(PromiseType), |
| 172 | S.Context.getTrivialTypeSourceInfo(PromiseType, Loc))); |
| 173 | |
| 174 | // Build the template-id. |
| 175 | QualType CoroHandleType = |
| 176 | S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args); |
| 177 | if (CoroHandleType.isNull()) |
| 178 | return QualType(); |
| 179 | if (S.RequireCompleteType(Loc, CoroHandleType, |
| 180 | diag::err_coroutine_type_missing_specialization)) |
| 181 | return QualType(); |
| 182 | |
| 183 | return CoroHandleType; |
| 184 | } |
| 185 | |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 186 | static bool isValidCoroutineContext(Sema &S, SourceLocation Loc, |
| 187 | StringRef Keyword) { |
Brian Gesiak | b15c35a | 2019-03-25 00:53:10 +0000 | [diff] [blame] | 188 | // [expr.await]p2 dictates that 'co_await' and 'co_yield' must be used within |
| 189 | // a function body. |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 190 | // FIXME: This also covers [expr.await]p2: "An await-expression shall not |
| 191 | // appear in a default argument." But the diagnostic QoI here could be |
| 192 | // improved to inform the user that default arguments specifically are not |
| 193 | // allowed. |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 194 | auto *FD = dyn_cast<FunctionDecl>(S.CurContext); |
| 195 | if (!FD) { |
| 196 | S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext) |
| 197 | ? diag::err_coroutine_objc_method |
| 198 | : diag::err_coroutine_outside_function) << Keyword; |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 199 | return false; |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 202 | // An enumeration for mapping the diagnostic type to the correct diagnostic |
| 203 | // selection index. |
| 204 | enum InvalidFuncDiag { |
| 205 | DiagCtor = 0, |
| 206 | DiagDtor, |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 207 | DiagMain, |
| 208 | DiagConstexpr, |
| 209 | DiagAutoRet, |
| 210 | DiagVarargs, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 211 | DiagConsteval, |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 212 | }; |
| 213 | bool Diagnosed = false; |
| 214 | auto DiagInvalid = [&](InvalidFuncDiag ID) { |
| 215 | S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword; |
| 216 | Diagnosed = true; |
| 217 | return false; |
| 218 | }; |
| 219 | |
Vivek Pandya | 3a0100a | 2019-06-19 14:12:19 +0000 | [diff] [blame^] | 220 | // Diagnose when a constructor, destructor |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 221 | // or the function 'main' are declared as a coroutine. |
| 222 | auto *MD = dyn_cast<CXXMethodDecl>(FD); |
Vivek Pandya | 3a0100a | 2019-06-19 14:12:19 +0000 | [diff] [blame^] | 223 | // [class.ctor]p11: "A constructor shall not be a coroutine." |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 224 | if (MD && isa<CXXConstructorDecl>(MD)) |
| 225 | return DiagInvalid(DiagCtor); |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 226 | // [class.dtor]p17: "A destructor shall not be a coroutine." |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 227 | else if (MD && isa<CXXDestructorDecl>(MD)) |
| 228 | return DiagInvalid(DiagDtor); |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 229 | // [basic.start.main]p3: "The function main shall not be a coroutine." |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 230 | else if (FD->isMain()) |
| 231 | return DiagInvalid(DiagMain); |
| 232 | |
| 233 | // Emit a diagnostics for each of the following conditions which is not met. |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 234 | // [expr.const]p2: "An expression e is a core constant expression unless the |
| 235 | // evaluation of e [...] would evaluate one of the following expressions: |
| 236 | // [...] an await-expression [...] a yield-expression." |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 237 | if (FD->isConstexpr()) |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 238 | DiagInvalid(FD->isConsteval() ? DiagConsteval : DiagConstexpr); |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 239 | // [dcl.spec.auto]p15: "A function declared with a return type that uses a |
| 240 | // placeholder type shall not be a coroutine." |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 241 | if (FD->getReturnType()->isUndeducedType()) |
| 242 | DiagInvalid(DiagAutoRet); |
Brian Gesiak | c1b173a | 2018-06-23 18:01:02 +0000 | [diff] [blame] | 243 | // [dcl.fct.def.coroutine]p1: "The parameter-declaration-clause of the |
| 244 | // coroutine shall not terminate with an ellipsis that is not part of a |
| 245 | // parameter-declaration." |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 246 | if (FD->isVariadic()) |
| 247 | DiagInvalid(DiagVarargs); |
| 248 | |
| 249 | return !Diagnosed; |
| 250 | } |
| 251 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 252 | static ExprResult buildOperatorCoawaitLookupExpr(Sema &SemaRef, Scope *S, |
| 253 | SourceLocation Loc) { |
| 254 | DeclarationName OpName = |
| 255 | SemaRef.Context.DeclarationNames.getCXXOperatorName(OO_Coawait); |
| 256 | LookupResult Operators(SemaRef, OpName, SourceLocation(), |
| 257 | Sema::LookupOperatorName); |
| 258 | SemaRef.LookupName(Operators, S); |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 259 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 260 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 261 | const auto &Functions = Operators.asUnresolvedSet(); |
| 262 | bool IsOverloaded = |
| 263 | Functions.size() > 1 || |
| 264 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 265 | Expr *CoawaitOp = UnresolvedLookupExpr::Create( |
| 266 | SemaRef.Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(), |
| 267 | DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, IsOverloaded, |
| 268 | Functions.begin(), Functions.end()); |
| 269 | assert(CoawaitOp); |
| 270 | return CoawaitOp; |
| 271 | } |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 272 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 273 | /// Build a call to 'operator co_await' if there is a suitable operator for |
| 274 | /// the given expression. |
| 275 | static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, SourceLocation Loc, |
| 276 | Expr *E, |
| 277 | UnresolvedLookupExpr *Lookup) { |
| 278 | UnresolvedSet<16> Functions; |
| 279 | Functions.append(Lookup->decls_begin(), Lookup->decls_end()); |
| 280 | return SemaRef.CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E); |
| 281 | } |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 282 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 283 | static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S, |
| 284 | SourceLocation Loc, Expr *E) { |
| 285 | ExprResult R = buildOperatorCoawaitLookupExpr(SemaRef, S, Loc); |
| 286 | if (R.isInvalid()) |
| 287 | return ExprError(); |
| 288 | return buildOperatorCoawaitCall(SemaRef, Loc, E, |
| 289 | cast<UnresolvedLookupExpr>(R.get())); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 292 | static Expr *buildBuiltinCall(Sema &S, SourceLocation Loc, Builtin::ID Id, |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 293 | MultiExprArg CallArgs) { |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 294 | StringRef Name = S.Context.BuiltinInfo.getName(Id); |
| 295 | LookupResult R(S, &S.Context.Idents.get(Name), Loc, Sema::LookupOrdinaryName); |
| 296 | S.LookupName(R, S.TUScope, /*AllowBuiltinCreation=*/true); |
| 297 | |
| 298 | auto *BuiltInDecl = R.getAsSingle<FunctionDecl>(); |
| 299 | assert(BuiltInDecl && "failed to find builtin declaration"); |
| 300 | |
| 301 | ExprResult DeclRef = |
| 302 | S.BuildDeclRefExpr(BuiltInDecl, BuiltInDecl->getType(), VK_LValue, Loc); |
| 303 | assert(DeclRef.isUsable() && "Builtin reference cannot fail"); |
| 304 | |
| 305 | ExprResult Call = |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 306 | S.BuildCallExpr(/*Scope=*/nullptr, DeclRef.get(), Loc, CallArgs, Loc); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 307 | |
| 308 | assert(!Call.isInvalid() && "Call to builtin cannot fail!"); |
| 309 | return Call.get(); |
| 310 | } |
| 311 | |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 312 | static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType, |
| 313 | SourceLocation Loc) { |
| 314 | QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc); |
| 315 | if (CoroHandleType.isNull()) |
| 316 | return ExprError(); |
| 317 | |
| 318 | DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType); |
| 319 | LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc, |
| 320 | Sema::LookupOrdinaryName); |
| 321 | if (!S.LookupQualifiedName(Found, LookupCtx)) { |
| 322 | S.Diag(Loc, diag::err_coroutine_handle_missing_member) |
| 323 | << "from_address"; |
| 324 | return ExprError(); |
| 325 | } |
| 326 | |
| 327 | Expr *FramePtr = |
| 328 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {}); |
| 329 | |
| 330 | CXXScopeSpec SS; |
| 331 | ExprResult FromAddr = |
| 332 | S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); |
| 333 | if (FromAddr.isInvalid()) |
| 334 | return ExprError(); |
| 335 | |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 336 | return S.BuildCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc); |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 337 | } |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 338 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 339 | struct ReadySuspendResumeResult { |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 340 | enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume }; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 341 | Expr *Results[3]; |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 342 | OpaqueValueExpr *OpaqueValue; |
| 343 | bool IsInvalid; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 344 | }; |
| 345 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 346 | static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc, |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 347 | StringRef Name, MultiExprArg Args) { |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 348 | DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc); |
| 349 | |
| 350 | // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&. |
| 351 | CXXScopeSpec SS; |
| 352 | ExprResult Result = S.BuildMemberReferenceExpr( |
| 353 | Base, Base->getType(), Loc, /*IsPtr=*/false, SS, |
| 354 | SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr, |
| 355 | /*Scope=*/nullptr); |
| 356 | if (Result.isInvalid()) |
| 357 | return ExprError(); |
| 358 | |
Gor Nishanov | d450726 | 2018-03-27 20:38:19 +0000 | [diff] [blame] | 359 | // We meant exactly what we asked for. No need for typo correction. |
| 360 | if (auto *TE = dyn_cast<TypoExpr>(Result.get())) { |
| 361 | S.clearDelayedTypo(TE); |
| 362 | S.Diag(Loc, diag::err_no_member) |
| 363 | << NameInfo.getName() << Base->getType()->getAsCXXRecordDecl() |
| 364 | << Base->getSourceRange(); |
| 365 | return ExprError(); |
| 366 | } |
| 367 | |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 368 | return S.BuildCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr); |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Gor Nishanov | 0f33300 | 2017-08-25 04:46:54 +0000 | [diff] [blame] | 371 | // See if return type is coroutine-handle and if so, invoke builtin coro-resume |
| 372 | // on its address. This is to enable experimental support for coroutine-handle |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 373 | // returning await_suspend that results in a guaranteed tail call to the target |
Gor Nishanov | 0f33300 | 2017-08-25 04:46:54 +0000 | [diff] [blame] | 374 | // coroutine. |
| 375 | static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E, |
| 376 | SourceLocation Loc) { |
| 377 | if (RetType->isReferenceType()) |
| 378 | return nullptr; |
| 379 | Type const *T = RetType.getTypePtr(); |
| 380 | if (!T->isClassType() && !T->isStructureType()) |
| 381 | return nullptr; |
| 382 | |
| 383 | // FIXME: Add convertability check to coroutine_handle<>. Possibly via |
| 384 | // EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment |
| 385 | // a private function in SemaExprCXX.cpp |
| 386 | |
| 387 | ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", None); |
| 388 | if (AddressExpr.isInvalid()) |
| 389 | return nullptr; |
| 390 | |
| 391 | Expr *JustAddress = AddressExpr.get(); |
| 392 | // FIXME: Check that the type of AddressExpr is void* |
| 393 | return buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_resume, |
| 394 | JustAddress); |
| 395 | } |
| 396 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 397 | /// Build calls to await_ready, await_suspend, and await_resume for a co_await |
| 398 | /// expression. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 399 | static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise, |
| 400 | SourceLocation Loc, Expr *E) { |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 401 | OpaqueValueExpr *Operand = new (S.Context) |
| 402 | OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E); |
| 403 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 404 | // Assume invalid until we see otherwise. |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 405 | ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/true}; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 406 | |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 407 | ExprResult CoroHandleRes = buildCoroutineHandle(S, CoroPromise->getType(), Loc); |
| 408 | if (CoroHandleRes.isInvalid()) |
| 409 | return Calls; |
| 410 | Expr *CoroHandle = CoroHandleRes.get(); |
| 411 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 412 | const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"}; |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 413 | MultiExprArg Args[] = {None, CoroHandle, None}; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 414 | for (size_t I = 0, N = llvm::array_lengthof(Funcs); I != N; ++I) { |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 415 | ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], Args[I]); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 416 | if (Result.isInvalid()) |
| 417 | return Calls; |
| 418 | Calls.Results[I] = Result.get(); |
| 419 | } |
| 420 | |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 421 | // Assume the calls are valid; all further checking should make them invalid. |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 422 | Calls.IsInvalid = false; |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 423 | |
| 424 | using ACT = ReadySuspendResumeResult::AwaitCallType; |
| 425 | CallExpr *AwaitReady = cast<CallExpr>(Calls.Results[ACT::ACT_Ready]); |
| 426 | if (!AwaitReady->getType()->isDependentType()) { |
| 427 | // [expr.await]p3 [...] |
| 428 | // — await-ready is the expression e.await_ready(), contextually converted |
| 429 | // to bool. |
| 430 | ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady); |
| 431 | if (Conv.isInvalid()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 432 | S.Diag(AwaitReady->getDirectCallee()->getBeginLoc(), |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 433 | diag::note_await_ready_no_bool_conversion); |
| 434 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
| 435 | << AwaitReady->getDirectCallee() << E->getSourceRange(); |
| 436 | Calls.IsInvalid = true; |
| 437 | } |
| 438 | Calls.Results[ACT::ACT_Ready] = Conv.get(); |
| 439 | } |
| 440 | CallExpr *AwaitSuspend = cast<CallExpr>(Calls.Results[ACT::ACT_Suspend]); |
| 441 | if (!AwaitSuspend->getType()->isDependentType()) { |
| 442 | // [expr.await]p3 [...] |
| 443 | // - await-suspend is the expression e.await_suspend(h), which shall be |
| 444 | // a prvalue of type void or bool. |
Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame] | 445 | QualType RetType = AwaitSuspend->getCallReturnType(S.Context); |
Gor Nishanov | db419a6 | 2017-09-05 19:31:52 +0000 | [diff] [blame] | 446 | |
Gor Nishanov | 0f33300 | 2017-08-25 04:46:54 +0000 | [diff] [blame] | 447 | // Experimental support for coroutine_handle returning await_suspend. |
| 448 | if (Expr *TailCallSuspend = maybeTailCall(S, RetType, AwaitSuspend, Loc)) |
| 449 | Calls.Results[ACT::ACT_Suspend] = TailCallSuspend; |
| 450 | else { |
| 451 | // non-class prvalues always have cv-unqualified types |
Gor Nishanov | 0f33300 | 2017-08-25 04:46:54 +0000 | [diff] [blame] | 452 | if (RetType->isReferenceType() || |
Gor Nishanov | db419a6 | 2017-09-05 19:31:52 +0000 | [diff] [blame] | 453 | (!RetType->isBooleanType() && !RetType->isVoidType())) { |
Gor Nishanov | 0f33300 | 2017-08-25 04:46:54 +0000 | [diff] [blame] | 454 | S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(), |
| 455 | diag::err_await_suspend_invalid_return_type) |
| 456 | << RetType; |
| 457 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
| 458 | << AwaitSuspend->getDirectCallee(); |
| 459 | Calls.IsInvalid = true; |
| 460 | } |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 464 | return Calls; |
| 465 | } |
| 466 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 467 | static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise, |
| 468 | SourceLocation Loc, StringRef Name, |
| 469 | MultiExprArg Args) { |
| 470 | |
| 471 | // Form a reference to the promise. |
| 472 | ExprResult PromiseRef = S.BuildDeclRefExpr( |
| 473 | Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc); |
| 474 | if (PromiseRef.isInvalid()) |
| 475 | return ExprError(); |
| 476 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 477 | return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args); |
| 478 | } |
| 479 | |
| 480 | VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) { |
| 481 | assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); |
| 482 | auto *FD = cast<FunctionDecl>(CurContext); |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 483 | bool IsThisDependentType = [&] { |
| 484 | if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(FD)) |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 485 | return MD->isInstance() && MD->getThisType()->isDependentType(); |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 486 | else |
| 487 | return false; |
| 488 | }(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 489 | |
Eric Fiselier | 166c6e6 | 2017-07-10 01:27:22 +0000 | [diff] [blame] | 490 | QualType T = FD->getType()->isDependentType() || IsThisDependentType |
| 491 | ? Context.DependentTy |
| 492 | : lookupPromiseType(*this, FD, Loc); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 493 | if (T.isNull()) |
| 494 | return nullptr; |
| 495 | |
| 496 | auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(), |
| 497 | &PP.getIdentifierTable().get("__promise"), T, |
| 498 | Context.getTrivialTypeSourceInfo(T, Loc), SC_None); |
| 499 | CheckVariableDeclarationType(VD); |
| 500 | if (VD->isInvalidDecl()) |
| 501 | return nullptr; |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 502 | |
| 503 | auto *ScopeInfo = getCurFunction(); |
| 504 | // Build a list of arguments, based on the coroutine functions arguments, |
| 505 | // that will be passed to the promise type's constructor. |
| 506 | llvm::SmallVector<Expr *, 4> CtorArgExprs; |
Gor Nishanov | 07ac63f | 2018-05-28 18:08:47 +0000 | [diff] [blame] | 507 | |
| 508 | // Add implicit object parameter. |
| 509 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 510 | if (MD->isInstance() && !isLambdaCallOperator(MD)) { |
| 511 | ExprResult ThisExpr = ActOnCXXThis(Loc); |
| 512 | if (ThisExpr.isInvalid()) |
| 513 | return nullptr; |
| 514 | ThisExpr = CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get()); |
| 515 | if (ThisExpr.isInvalid()) |
| 516 | return nullptr; |
| 517 | CtorArgExprs.push_back(ThisExpr.get()); |
| 518 | } |
| 519 | } |
| 520 | |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 521 | auto &Moves = ScopeInfo->CoroutineParameterMoves; |
| 522 | for (auto *PD : FD->parameters()) { |
| 523 | if (PD->getType()->isDependentType()) |
| 524 | continue; |
| 525 | |
| 526 | auto RefExpr = ExprEmpty(); |
| 527 | auto Move = Moves.find(PD); |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 528 | assert(Move != Moves.end() && |
| 529 | "Coroutine function parameter not inserted into move map"); |
| 530 | // If a reference to the function parameter exists in the coroutine |
| 531 | // frame, use that reference. |
| 532 | auto *MoveDecl = |
| 533 | cast<VarDecl>(cast<DeclStmt>(Move->second)->getSingleDecl()); |
| 534 | RefExpr = |
| 535 | BuildDeclRefExpr(MoveDecl, MoveDecl->getType().getNonReferenceType(), |
| 536 | ExprValueKind::VK_LValue, FD->getLocation()); |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 537 | if (RefExpr.isInvalid()) |
| 538 | return nullptr; |
| 539 | CtorArgExprs.push_back(RefExpr.get()); |
| 540 | } |
| 541 | |
| 542 | // Create an initialization sequence for the promise type using the |
| 543 | // constructor arguments, wrapped in a parenthesized list expression. |
Bruno Ricci | f49e1ca | 2018-11-20 16:20:40 +0000 | [diff] [blame] | 544 | Expr *PLE = ParenListExpr::Create(Context, FD->getLocation(), |
| 545 | CtorArgExprs, FD->getLocation()); |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 546 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VD); |
| 547 | InitializationKind Kind = InitializationKind::CreateForInit( |
| 548 | VD->getLocation(), /*DirectInit=*/true, PLE); |
| 549 | InitializationSequence InitSeq(*this, Entity, Kind, CtorArgExprs, |
| 550 | /*TopLevelOfInitList=*/false, |
| 551 | /*TreatUnavailableAsInvalid=*/false); |
| 552 | |
| 553 | // Attempt to initialize the promise type with the arguments. |
| 554 | // If that fails, fall back to the promise type's default constructor. |
| 555 | if (InitSeq) { |
| 556 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, CtorArgExprs); |
| 557 | if (Result.isInvalid()) { |
| 558 | VD->setInvalidDecl(); |
| 559 | } else if (Result.get()) { |
| 560 | VD->setInit(MaybeCreateExprWithCleanups(Result.get())); |
| 561 | VD->setInitStyle(VarDecl::CallInit); |
| 562 | CheckCompleteVariableDeclaration(VD); |
| 563 | } |
| 564 | } else |
| 565 | ActOnUninitializedDecl(VD); |
| 566 | |
Eric Fiselier | 37b8a37 | 2017-05-31 19:36:59 +0000 | [diff] [blame] | 567 | FD->addDecl(VD); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 568 | return VD; |
| 569 | } |
| 570 | |
| 571 | /// Check that this is a context in which a coroutine suspension can appear. |
| 572 | static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc, |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 573 | StringRef Keyword, |
| 574 | bool IsImplicit = false) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 575 | if (!isValidCoroutineContext(S, Loc, Keyword)) |
| 576 | return nullptr; |
| 577 | |
| 578 | assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope"); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 579 | |
| 580 | auto *ScopeInfo = S.getCurFunction(); |
| 581 | assert(ScopeInfo && "missing function scope for function"); |
| 582 | |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 583 | if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit) |
| 584 | ScopeInfo->setFirstCoroutineStmt(Loc, Keyword); |
| 585 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 586 | if (ScopeInfo->CoroutinePromise) |
| 587 | return ScopeInfo; |
| 588 | |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 589 | if (!S.buildCoroutineParameterMoves(Loc)) |
| 590 | return nullptr; |
| 591 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 592 | ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc); |
| 593 | if (!ScopeInfo->CoroutinePromise) |
| 594 | return nullptr; |
| 595 | |
| 596 | return ScopeInfo; |
| 597 | } |
| 598 | |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 599 | bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc, |
| 600 | StringRef Keyword) { |
| 601 | if (!checkCoroutineContext(*this, KWLoc, Keyword)) |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 602 | return false; |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 603 | auto *ScopeInfo = getCurFunction(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 604 | assert(ScopeInfo->CoroutinePromise); |
| 605 | |
| 606 | // If we have existing coroutine statements then we have already built |
| 607 | // the initial and final suspend points. |
| 608 | if (!ScopeInfo->NeedsCoroutineSuspends) |
| 609 | return true; |
| 610 | |
| 611 | ScopeInfo->setNeedsCoroutineSuspends(false); |
| 612 | |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 613 | auto *Fn = cast<FunctionDecl>(CurContext); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 614 | SourceLocation Loc = Fn->getLocation(); |
| 615 | // Build the initial suspend point |
| 616 | auto buildSuspends = [&](StringRef Name) mutable -> StmtResult { |
| 617 | ExprResult Suspend = |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 618 | buildPromiseCall(*this, ScopeInfo->CoroutinePromise, Loc, Name, None); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 619 | if (Suspend.isInvalid()) |
| 620 | return StmtError(); |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 621 | Suspend = buildOperatorCoawaitCall(*this, SC, Loc, Suspend.get()); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 622 | if (Suspend.isInvalid()) |
| 623 | return StmtError(); |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 624 | Suspend = BuildResolvedCoawaitExpr(Loc, Suspend.get(), |
| 625 | /*IsImplicit*/ true); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 626 | Suspend = ActOnFinishFullExpr(Suspend.get(), /*DiscardedValue*/ false); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 627 | if (Suspend.isInvalid()) { |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 628 | Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required) |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 629 | << ((Name == "initial_suspend") ? 0 : 1); |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 630 | Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword; |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 631 | return StmtError(); |
| 632 | } |
| 633 | return cast<Stmt>(Suspend.get()); |
| 634 | }; |
| 635 | |
| 636 | StmtResult InitSuspend = buildSuspends("initial_suspend"); |
| 637 | if (InitSuspend.isInvalid()) |
| 638 | return true; |
| 639 | |
| 640 | StmtResult FinalSuspend = buildSuspends("final_suspend"); |
| 641 | if (FinalSuspend.isInvalid()) |
| 642 | return true; |
| 643 | |
| 644 | ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get()); |
| 645 | |
| 646 | return true; |
| 647 | } |
| 648 | |
Brian Gesiak | b15c35a | 2019-03-25 00:53:10 +0000 | [diff] [blame] | 649 | // Recursively walks up the scope hierarchy until either a 'catch' or a function |
| 650 | // scope is found, whichever comes first. |
| 651 | static bool isWithinCatchScope(Scope *S) { |
| 652 | // 'co_await' and 'co_yield' keywords are disallowed within catch blocks, but |
| 653 | // lambdas that use 'co_await' are allowed. The loop below ends when a |
| 654 | // function scope is found in order to ensure the following behavior: |
| 655 | // |
| 656 | // void foo() { // <- function scope |
| 657 | // try { // |
| 658 | // co_await x; // <- 'co_await' is OK within a function scope |
| 659 | // } catch { // <- catch scope |
| 660 | // co_await x; // <- 'co_await' is not OK within a catch scope |
| 661 | // []() { // <- function scope |
| 662 | // co_await x; // <- 'co_await' is OK within a function scope |
| 663 | // }(); |
| 664 | // } |
| 665 | // } |
| 666 | while (S && !(S->getFlags() & Scope::FnScope)) { |
| 667 | if (S->getFlags() & Scope::CatchScope) |
| 668 | return true; |
| 669 | S = S->getParent(); |
| 670 | } |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | // [expr.await]p2, emphasis added: "An await-expression shall appear only in |
| 675 | // a *potentially evaluated* expression within the compound-statement of a |
| 676 | // function-body *outside of a handler* [...] A context within a function |
| 677 | // where an await-expression can appear is called a suspension context of the |
| 678 | // function." |
| 679 | static void checkSuspensionContext(Sema &S, SourceLocation Loc, |
| 680 | StringRef Keyword) { |
| 681 | // First emphasis of [expr.await]p2: must be a potentially evaluated context. |
| 682 | // That is, 'co_await' and 'co_yield' cannot appear in subexpressions of |
| 683 | // \c sizeof. |
| 684 | if (S.isUnevaluatedContext()) |
| 685 | S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword; |
| 686 | |
| 687 | // Second emphasis of [expr.await]p2: must be outside of an exception handler. |
| 688 | if (isWithinCatchScope(S.getCurScope())) |
| 689 | S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword; |
| 690 | } |
| 691 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 692 | ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) { |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 693 | if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) { |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 694 | CorrectDelayedTyposInExpr(E); |
| 695 | return ExprError(); |
| 696 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 697 | |
Brian Gesiak | b15c35a | 2019-03-25 00:53:10 +0000 | [diff] [blame] | 698 | checkSuspensionContext(*this, Loc, "co_await"); |
| 699 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 700 | if (E->getType()->isPlaceholderType()) { |
| 701 | ExprResult R = CheckPlaceholderExpr(E); |
| 702 | if (R.isInvalid()) return ExprError(); |
| 703 | E = R.get(); |
| 704 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 705 | ExprResult Lookup = buildOperatorCoawaitLookupExpr(*this, S, Loc); |
| 706 | if (Lookup.isInvalid()) |
| 707 | return ExprError(); |
| 708 | return BuildUnresolvedCoawaitExpr(Loc, E, |
| 709 | cast<UnresolvedLookupExpr>(Lookup.get())); |
| 710 | } |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 711 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 712 | ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *E, |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 713 | UnresolvedLookupExpr *Lookup) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 714 | auto *FSI = checkCoroutineContext(*this, Loc, "co_await"); |
| 715 | if (!FSI) |
| 716 | return ExprError(); |
| 717 | |
| 718 | if (E->getType()->isPlaceholderType()) { |
| 719 | ExprResult R = CheckPlaceholderExpr(E); |
| 720 | if (R.isInvalid()) |
| 721 | return ExprError(); |
| 722 | E = R.get(); |
| 723 | } |
| 724 | |
| 725 | auto *Promise = FSI->CoroutinePromise; |
| 726 | if (Promise->getType()->isDependentType()) { |
| 727 | Expr *Res = |
| 728 | new (Context) DependentCoawaitExpr(Loc, Context.DependentTy, E, Lookup); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 729 | return Res; |
| 730 | } |
| 731 | |
| 732 | auto *RD = Promise->getType()->getAsCXXRecordDecl(); |
| 733 | if (lookupMember(*this, "await_transform", RD, Loc)) { |
| 734 | ExprResult R = buildPromiseCall(*this, Promise, Loc, "await_transform", E); |
| 735 | if (R.isInvalid()) { |
| 736 | Diag(Loc, |
| 737 | diag::note_coroutine_promise_implicit_await_transform_required_here) |
| 738 | << E->getSourceRange(); |
| 739 | return ExprError(); |
| 740 | } |
| 741 | E = R.get(); |
| 742 | } |
| 743 | ExprResult Awaitable = buildOperatorCoawaitCall(*this, Loc, E, Lookup); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 744 | if (Awaitable.isInvalid()) |
| 745 | return ExprError(); |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 746 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 747 | return BuildResolvedCoawaitExpr(Loc, Awaitable.get()); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 748 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 749 | |
| 750 | ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *E, |
| 751 | bool IsImplicit) { |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 752 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 753 | if (!Coroutine) |
| 754 | return ExprError(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 755 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 756 | if (E->getType()->isPlaceholderType()) { |
| 757 | ExprResult R = CheckPlaceholderExpr(E); |
| 758 | if (R.isInvalid()) return ExprError(); |
| 759 | E = R.get(); |
| 760 | } |
| 761 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 762 | if (E->getType()->isDependentType()) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 763 | Expr *Res = new (Context) |
| 764 | CoawaitExpr(Loc, Context.DependentTy, E, IsImplicit); |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 765 | return Res; |
| 766 | } |
| 767 | |
Richard Smith | 1f38edd | 2015-11-22 03:13:02 +0000 | [diff] [blame] | 768 | // If the expression is a temporary, materialize it as an lvalue so that we |
| 769 | // can use it multiple times. |
| 770 | if (E->getValueKind() == VK_RValue) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 771 | E = CreateMaterializeTemporaryExpr(E->getType(), E, true); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 772 | |
Eric Fiselier | d2e30d3 | 2018-03-27 03:15:46 +0000 | [diff] [blame] | 773 | // The location of the `co_await` token cannot be used when constructing |
| 774 | // the member call expressions since it's before the location of `Expr`, which |
| 775 | // is used as the start of the member call expression. |
| 776 | SourceLocation CallLoc = E->getExprLoc(); |
| 777 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 778 | // Build the await_ready, await_suspend, await_resume calls. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 779 | ReadySuspendResumeResult RSS = |
Eric Fiselier | d2e30d3 | 2018-03-27 03:15:46 +0000 | [diff] [blame] | 780 | buildCoawaitCalls(*this, Coroutine->CoroutinePromise, CallLoc, E); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 781 | if (RSS.IsInvalid) |
| 782 | return ExprError(); |
| 783 | |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 784 | Expr *Res = |
| 785 | new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1], |
| 786 | RSS.Results[2], RSS.OpaqueValue, IsImplicit); |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 787 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 788 | return Res; |
| 789 | } |
| 790 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 791 | ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) { |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 792 | if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) { |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 793 | CorrectDelayedTyposInExpr(E); |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 794 | return ExprError(); |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 795 | } |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 796 | |
Brian Gesiak | b15c35a | 2019-03-25 00:53:10 +0000 | [diff] [blame] | 797 | checkSuspensionContext(*this, Loc, "co_yield"); |
| 798 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 799 | // Build yield_value call. |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 800 | ExprResult Awaitable = buildPromiseCall( |
| 801 | *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 802 | if (Awaitable.isInvalid()) |
| 803 | return ExprError(); |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 804 | |
| 805 | // Build 'operator co_await' call. |
| 806 | Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get()); |
| 807 | if (Awaitable.isInvalid()) |
| 808 | return ExprError(); |
| 809 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 810 | return BuildCoyieldExpr(Loc, Awaitable.get()); |
| 811 | } |
| 812 | ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) { |
| 813 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield"); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 814 | if (!Coroutine) |
| 815 | return ExprError(); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 816 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 817 | if (E->getType()->isPlaceholderType()) { |
| 818 | ExprResult R = CheckPlaceholderExpr(E); |
| 819 | if (R.isInvalid()) return ExprError(); |
| 820 | E = R.get(); |
| 821 | } |
| 822 | |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 823 | if (E->getType()->isDependentType()) { |
| 824 | Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, E); |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 825 | return Res; |
| 826 | } |
| 827 | |
Richard Smith | 1f38edd | 2015-11-22 03:13:02 +0000 | [diff] [blame] | 828 | // If the expression is a temporary, materialize it as an lvalue so that we |
| 829 | // can use it multiple times. |
| 830 | if (E->getValueKind() == VK_RValue) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 831 | E = CreateMaterializeTemporaryExpr(E->getType(), E, true); |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 832 | |
| 833 | // Build the await_ready, await_suspend, await_resume calls. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 834 | ReadySuspendResumeResult RSS = |
| 835 | buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E); |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 836 | if (RSS.IsInvalid) |
| 837 | return ExprError(); |
| 838 | |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 839 | Expr *Res = |
| 840 | new (Context) CoyieldExpr(Loc, E, RSS.Results[0], RSS.Results[1], |
| 841 | RSS.Results[2], RSS.OpaqueValue); |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 842 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 843 | return Res; |
| 844 | } |
| 845 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 846 | StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) { |
Eric Fiselier | b936a39 | 2017-06-14 03:24:55 +0000 | [diff] [blame] | 847 | if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) { |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 848 | CorrectDelayedTyposInExpr(E); |
| 849 | return StmtError(); |
| 850 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 851 | return BuildCoreturnStmt(Loc, E); |
| 852 | } |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 853 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 854 | StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E, |
| 855 | bool IsImplicit) { |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 856 | auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 857 | if (!FSI) |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 858 | return StmtError(); |
| 859 | |
| 860 | if (E && E->getType()->isPlaceholderType() && |
| 861 | !E->getType()->isSpecificPlaceholderType(BuiltinType::Overload)) { |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 862 | ExprResult R = CheckPlaceholderExpr(E); |
| 863 | if (R.isInvalid()) return StmtError(); |
| 864 | E = R.get(); |
| 865 | } |
| 866 | |
Brian Gesiak | 0b56830 | 2018-10-08 03:08:39 +0000 | [diff] [blame] | 867 | // Move the return value if we can |
| 868 | if (E) { |
| 869 | auto NRVOCandidate = this->getCopyElisionCandidate(E->getType(), E, CES_AsIfByStdMove); |
| 870 | if (NRVOCandidate) { |
| 871 | InitializedEntity Entity = |
| 872 | InitializedEntity::InitializeResult(Loc, E->getType(), NRVOCandidate); |
| 873 | ExprResult MoveResult = this->PerformMoveOrCopyInitialization( |
| 874 | Entity, NRVOCandidate, E->getType(), E); |
| 875 | if (MoveResult.get()) |
| 876 | E = MoveResult.get(); |
| 877 | } |
| 878 | } |
| 879 | |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 880 | // FIXME: If the operand is a reference to a variable that's about to go out |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 881 | // of scope, we should treat the operand as an xvalue for this overload |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 882 | // resolution. |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 883 | VarDecl *Promise = FSI->CoroutinePromise; |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 884 | ExprResult PC; |
Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 885 | if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 886 | PC = buildPromiseCall(*this, Promise, Loc, "return_value", E); |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 887 | } else { |
| 888 | E = MakeFullDiscardedValueExpr(E).get(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 889 | PC = buildPromiseCall(*this, Promise, Loc, "return_void", None); |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 890 | } |
| 891 | if (PC.isInvalid()) |
| 892 | return StmtError(); |
| 893 | |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 894 | Expr *PCE = ActOnFinishFullExpr(PC.get(), /*DiscardedValue*/ false).get(); |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 895 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 896 | Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 897 | return Res; |
| 898 | } |
| 899 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 900 | /// Look up the std::nothrow object. |
| 901 | static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) { |
| 902 | NamespaceDecl *Std = S.getStdNamespace(); |
| 903 | assert(Std && "Should already be diagnosed"); |
| 904 | |
| 905 | LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc, |
| 906 | Sema::LookupOrdinaryName); |
| 907 | if (!S.LookupQualifiedName(Result, Std)) { |
| 908 | // FIXME: <experimental/coroutine> should have been included already. |
| 909 | // If we require it to include <new> then this diagnostic is no longer |
| 910 | // needed. |
| 911 | S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found); |
| 912 | return nullptr; |
| 913 | } |
| 914 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 915 | auto *VD = Result.getAsSingle<VarDecl>(); |
| 916 | if (!VD) { |
| 917 | Result.suppressDiagnostics(); |
| 918 | // We found something weird. Complain about the first thing we found. |
| 919 | NamedDecl *Found = *Result.begin(); |
| 920 | S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow); |
| 921 | return nullptr; |
| 922 | } |
| 923 | |
| 924 | ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc); |
| 925 | if (DR.isInvalid()) |
| 926 | return nullptr; |
| 927 | |
| 928 | return DR.get(); |
| 929 | } |
| 930 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 931 | // Find an appropriate delete for the promise. |
| 932 | static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc, |
| 933 | QualType PromiseType) { |
| 934 | FunctionDecl *OperatorDelete = nullptr; |
| 935 | |
| 936 | DeclarationName DeleteName = |
| 937 | S.Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 938 | |
| 939 | auto *PointeeRD = PromiseType->getAsCXXRecordDecl(); |
| 940 | assert(PointeeRD && "PromiseType must be a CxxRecordDecl type"); |
| 941 | |
| 942 | if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete)) |
| 943 | return nullptr; |
| 944 | |
| 945 | if (!OperatorDelete) { |
| 946 | // Look for a global declaration. |
| 947 | const bool CanProvideSize = S.isCompleteType(Loc, PromiseType); |
| 948 | const bool Overaligned = false; |
| 949 | OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize, |
| 950 | Overaligned, DeleteName); |
| 951 | } |
| 952 | S.MarkFunctionReferenced(Loc, OperatorDelete); |
| 953 | return OperatorDelete; |
| 954 | } |
| 955 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 956 | |
| 957 | void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) { |
| 958 | FunctionScopeInfo *Fn = getCurFunction(); |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 959 | assert(Fn && Fn->isCoroutine() && "not a coroutine"); |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 960 | if (!Body) { |
| 961 | assert(FD->isInvalidDecl() && |
| 962 | "a null body is only allowed for invalid declarations"); |
| 963 | return; |
| 964 | } |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 965 | // We have a function that uses coroutine keywords, but we failed to build |
| 966 | // the promise type. |
| 967 | if (!Fn->CoroutinePromise) |
| 968 | return FD->setInvalidDecl(); |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 969 | |
| 970 | if (isa<CoroutineBodyStmt>(Body)) { |
Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 971 | // Nothing todo. the body is already a transformed coroutine body statement. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 972 | return; |
| 973 | } |
| 974 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 975 | // Coroutines [stmt.return]p1: |
| 976 | // A return statement shall not appear in a coroutine. |
| 977 | if (Fn->FirstReturnLoc.isValid()) { |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 978 | assert(Fn->FirstCoroutineStmtLoc.isValid() && |
| 979 | "first coroutine location not set"); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 980 | Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine); |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 981 | Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 982 | << Fn->getFirstCoroutineStmtKeyword(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 983 | } |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 984 | CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body); |
| 985 | if (Builder.isInvalid() || !Builder.buildStatements()) |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 986 | return FD->setInvalidDecl(); |
| 987 | |
| 988 | // Build body for the coroutine wrapper statement. |
| 989 | Body = CoroutineBodyStmt::Create(Context, Builder); |
| 990 | } |
| 991 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 992 | CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD, |
| 993 | sema::FunctionScopeInfo &Fn, |
| 994 | Stmt *Body) |
| 995 | : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()), |
| 996 | IsPromiseDependentType( |
| 997 | !Fn.CoroutinePromise || |
| 998 | Fn.CoroutinePromise->getType()->isDependentType()) { |
| 999 | this->Body = Body; |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1000 | |
| 1001 | for (auto KV : Fn.CoroutineParameterMoves) |
| 1002 | this->ParamMovesVector.push_back(KV.second); |
| 1003 | this->ParamMoves = this->ParamMovesVector; |
| 1004 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1005 | if (!IsPromiseDependentType) { |
| 1006 | PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl(); |
| 1007 | assert(PromiseRecordDecl && "Type should have already been checked"); |
| 1008 | } |
| 1009 | this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend(); |
| 1010 | } |
| 1011 | |
| 1012 | bool CoroutineStmtBuilder::buildStatements() { |
| 1013 | assert(this->IsValid && "coroutine already invalid"); |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1014 | this->IsValid = makeReturnObject(); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1015 | if (this->IsValid && !IsPromiseDependentType) |
| 1016 | buildDependentStatements(); |
| 1017 | return this->IsValid; |
| 1018 | } |
| 1019 | |
| 1020 | bool CoroutineStmtBuilder::buildDependentStatements() { |
| 1021 | assert(this->IsValid && "coroutine already invalid"); |
| 1022 | assert(!this->IsPromiseDependentType && |
| 1023 | "coroutine cannot have a dependent promise type"); |
| 1024 | this->IsValid = makeOnException() && makeOnFallthrough() && |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1025 | makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() && |
| 1026 | makeNewAndDeleteExpr(); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1027 | return this->IsValid; |
| 1028 | } |
| 1029 | |
| 1030 | bool CoroutineStmtBuilder::makePromiseStmt() { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1031 | // Form a declaration statement for the promise declaration, so that AST |
| 1032 | // visitors can more easily find it. |
| 1033 | StmtResult PromiseStmt = |
| 1034 | S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc); |
| 1035 | if (PromiseStmt.isInvalid()) |
| 1036 | return false; |
| 1037 | |
| 1038 | this->Promise = PromiseStmt.get(); |
| 1039 | return true; |
| 1040 | } |
| 1041 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1042 | bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1043 | if (Fn.hasInvalidCoroutineSuspends()) |
| 1044 | return false; |
| 1045 | this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first); |
| 1046 | this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second); |
| 1047 | return true; |
| 1048 | } |
| 1049 | |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 1050 | static bool diagReturnOnAllocFailure(Sema &S, Expr *E, |
| 1051 | CXXRecordDecl *PromiseRecordDecl, |
| 1052 | FunctionScopeInfo &Fn) { |
| 1053 | auto Loc = E->getExprLoc(); |
| 1054 | if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) { |
| 1055 | auto *Decl = DeclRef->getDecl(); |
| 1056 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) { |
| 1057 | if (Method->isStatic()) |
| 1058 | return true; |
| 1059 | else |
| 1060 | Loc = Decl->getLocation(); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | S.Diag( |
| 1065 | Loc, |
| 1066 | diag::err_coroutine_promise_get_return_object_on_allocation_failure) |
| 1067 | << PromiseRecordDecl; |
| 1068 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 1069 | << Fn.getFirstCoroutineStmtKeyword(); |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1073 | bool CoroutineStmtBuilder::makeReturnOnAllocFailure() { |
| 1074 | assert(!IsPromiseDependentType && |
| 1075 | "cannot make statement while the promise type is dependent"); |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 1076 | |
| 1077 | // [dcl.fct.def.coroutine]/8 |
| 1078 | // The unqualified-id get_return_object_on_allocation_failure is looked up in |
| 1079 | // the scope of class P by class member access lookup (3.4.5). ... |
| 1080 | // If an allocation function returns nullptr, ... the coroutine return value |
| 1081 | // is obtained by a call to ... get_return_object_on_allocation_failure(). |
| 1082 | |
| 1083 | DeclarationName DN = |
| 1084 | S.PP.getIdentifierInfo("get_return_object_on_allocation_failure"); |
| 1085 | LookupResult Found(S, DN, Loc, Sema::LookupMemberName); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1086 | if (!S.LookupQualifiedName(Found, PromiseRecordDecl)) |
| 1087 | return true; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 1088 | |
| 1089 | CXXScopeSpec SS; |
| 1090 | ExprResult DeclNameExpr = |
| 1091 | S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1092 | if (DeclNameExpr.isInvalid()) |
| 1093 | return false; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn)) |
| 1096 | return false; |
| 1097 | |
| 1098 | ExprResult ReturnObjectOnAllocationFailure = |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 1099 | S.BuildCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1100 | if (ReturnObjectOnAllocationFailure.isInvalid()) |
| 1101 | return false; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 1102 | |
Gor Nishanov | c4a1908 | 2017-03-28 02:51:45 +0000 | [diff] [blame] | 1103 | StmtResult ReturnStmt = |
| 1104 | S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get()); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1105 | if (ReturnStmt.isInvalid()) { |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 1106 | S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here) |
| 1107 | << DN; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1108 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 1109 | << Fn.getFirstCoroutineStmtKeyword(); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1110 | return false; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1111 | } |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 1112 | |
| 1113 | this->ReturnStmtOnAllocFailure = ReturnStmt.get(); |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1117 | bool CoroutineStmtBuilder::makeNewAndDeleteExpr() { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1118 | // Form and check allocation and deallocation calls. |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1119 | assert(!IsPromiseDependentType && |
| 1120 | "cannot make statement while the promise type is dependent"); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1121 | QualType PromiseType = Fn.CoroutinePromise->getType(); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1122 | |
| 1123 | if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type)) |
| 1124 | return false; |
| 1125 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1126 | const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr; |
| 1127 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1128 | // [dcl.fct.def.coroutine]/7 |
| 1129 | // Lookup allocation functions using a parameter list composed of the |
| 1130 | // requested size of the coroutine state being allocated, followed by |
| 1131 | // the coroutine function's arguments. If a matching allocation function |
| 1132 | // exists, use it. Otherwise, use an allocation function that just takes |
| 1133 | // the requested size. |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1134 | |
| 1135 | FunctionDecl *OperatorNew = nullptr; |
| 1136 | FunctionDecl *OperatorDelete = nullptr; |
| 1137 | FunctionDecl *UnusedResult = nullptr; |
| 1138 | bool PassAlignment = false; |
Eric Fiselier | f747f53 | 2017-04-18 05:08:08 +0000 | [diff] [blame] | 1139 | SmallVector<Expr *, 1> PlacementArgs; |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1140 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1141 | // [dcl.fct.def.coroutine]/7 |
| 1142 | // "The allocation function’s name is looked up in the scope of P. |
| 1143 | // [...] If the lookup finds an allocation function in the scope of P, |
| 1144 | // overload resolution is performed on a function call created by assembling |
| 1145 | // an argument list. The first argument is the amount of space requested, |
| 1146 | // and has type std::size_t. The lvalues p1 ... pn are the succeeding |
| 1147 | // arguments." |
| 1148 | // |
| 1149 | // ...where "p1 ... pn" are defined earlier as: |
| 1150 | // |
| 1151 | // [dcl.fct.def.coroutine]/3 |
| 1152 | // "For a coroutine f that is a non-static member function, let P1 denote the |
| 1153 | // type of the implicit object parameter (13.3.1) and P2 ... Pn be the types |
| 1154 | // of the function parameters; otherwise let P1 ... Pn be the types of the |
| 1155 | // function parameters. Let p1 ... pn be lvalues denoting those objects." |
| 1156 | if (auto *MD = dyn_cast<CXXMethodDecl>(&FD)) { |
| 1157 | if (MD->isInstance() && !isLambdaCallOperator(MD)) { |
| 1158 | ExprResult ThisExpr = S.ActOnCXXThis(Loc); |
| 1159 | if (ThisExpr.isInvalid()) |
| 1160 | return false; |
| 1161 | ThisExpr = S.CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get()); |
| 1162 | if (ThisExpr.isInvalid()) |
| 1163 | return false; |
| 1164 | PlacementArgs.push_back(ThisExpr.get()); |
| 1165 | } |
| 1166 | } |
| 1167 | for (auto *PD : FD.parameters()) { |
| 1168 | if (PD->getType()->isDependentType()) |
| 1169 | continue; |
| 1170 | |
| 1171 | // Build a reference to the parameter. |
| 1172 | auto PDLoc = PD->getLocation(); |
| 1173 | ExprResult PDRefExpr = |
| 1174 | S.BuildDeclRefExpr(PD, PD->getOriginalType().getNonReferenceType(), |
| 1175 | ExprValueKind::VK_LValue, PDLoc); |
| 1176 | if (PDRefExpr.isInvalid()) |
| 1177 | return false; |
| 1178 | |
| 1179 | PlacementArgs.push_back(PDRefExpr.get()); |
| 1180 | } |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 1181 | S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Class, |
| 1182 | /*DeleteScope*/ Sema::AFS_Both, PromiseType, |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1183 | /*isArray*/ false, PassAlignment, PlacementArgs, |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1184 | OperatorNew, UnusedResult, /*Diagnose*/ false); |
| 1185 | |
| 1186 | // [dcl.fct.def.coroutine]/7 |
| 1187 | // "If no matching function is found, overload resolution is performed again |
| 1188 | // on a function call created by passing just the amount of space required as |
| 1189 | // an argument of type std::size_t." |
| 1190 | if (!OperatorNew && !PlacementArgs.empty()) { |
| 1191 | PlacementArgs.clear(); |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 1192 | S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Class, |
| 1193 | /*DeleteScope*/ Sema::AFS_Both, PromiseType, |
| 1194 | /*isArray*/ false, PassAlignment, PlacementArgs, |
| 1195 | OperatorNew, UnusedResult, /*Diagnose*/ false); |
| 1196 | } |
| 1197 | |
| 1198 | // [dcl.fct.def.coroutine]/7 |
| 1199 | // "The allocation function’s name is looked up in the scope of P. If this |
| 1200 | // lookup fails, the allocation function’s name is looked up in the global |
| 1201 | // scope." |
| 1202 | if (!OperatorNew) { |
| 1203 | S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Global, |
| 1204 | /*DeleteScope*/ Sema::AFS_Both, PromiseType, |
| 1205 | /*isArray*/ false, PassAlignment, PlacementArgs, |
| 1206 | OperatorNew, UnusedResult); |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1207 | } |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1208 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1209 | bool IsGlobalOverload = |
| 1210 | OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext()); |
| 1211 | // If we didn't find a class-local new declaration and non-throwing new |
| 1212 | // was is required then we need to lookup the non-throwing global operator |
| 1213 | // instead. |
| 1214 | if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) { |
| 1215 | auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc); |
| 1216 | if (!StdNoThrow) |
| 1217 | return false; |
Eric Fiselier | f747f53 | 2017-04-18 05:08:08 +0000 | [diff] [blame] | 1218 | PlacementArgs = {StdNoThrow}; |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1219 | OperatorNew = nullptr; |
Brian Gesiak | cb02402 | 2018-04-01 22:59:22 +0000 | [diff] [blame] | 1220 | S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Both, |
| 1221 | /*DeleteScope*/ Sema::AFS_Both, PromiseType, |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1222 | /*isArray*/ false, PassAlignment, PlacementArgs, |
| 1223 | OperatorNew, UnusedResult); |
| 1224 | } |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1225 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1226 | if (!OperatorNew) |
| 1227 | return false; |
Eric Fiselier | c512875 | 2017-04-18 05:30:39 +0000 | [diff] [blame] | 1228 | |
| 1229 | if (RequiresNoThrowAlloc) { |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1230 | const auto *FT = OperatorNew->getType()->getAs<FunctionProtoType>(); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 1231 | if (!FT->isNothrow(/*ResultIfDependent*/ false)) { |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1232 | S.Diag(OperatorNew->getLocation(), |
| 1233 | diag::err_coroutine_promise_new_requires_nothrow) |
| 1234 | << OperatorNew; |
| 1235 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
| 1236 | << OperatorNew; |
| 1237 | return false; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr) |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1242 | return false; |
| 1243 | |
| 1244 | Expr *FramePtr = |
| 1245 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {}); |
| 1246 | |
| 1247 | Expr *FrameSize = |
| 1248 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_size, {}); |
| 1249 | |
| 1250 | // Make new call. |
| 1251 | |
| 1252 | ExprResult NewRef = |
| 1253 | S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc); |
| 1254 | if (NewRef.isInvalid()) |
| 1255 | return false; |
| 1256 | |
Eric Fiselier | f747f53 | 2017-04-18 05:08:08 +0000 | [diff] [blame] | 1257 | SmallVector<Expr *, 2> NewArgs(1, FrameSize); |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1258 | for (auto Arg : PlacementArgs) |
| 1259 | NewArgs.push_back(Arg); |
| 1260 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1261 | ExprResult NewExpr = |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 1262 | S.BuildCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1263 | NewExpr = S.ActOnFinishFullExpr(NewExpr.get(), /*DiscardedValue*/ false); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1264 | if (NewExpr.isInvalid()) |
| 1265 | return false; |
| 1266 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1267 | // Make delete call. |
| 1268 | |
| 1269 | QualType OpDeleteQualType = OperatorDelete->getType(); |
| 1270 | |
| 1271 | ExprResult DeleteRef = |
| 1272 | S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc); |
| 1273 | if (DeleteRef.isInvalid()) |
| 1274 | return false; |
| 1275 | |
| 1276 | Expr *CoroFree = |
| 1277 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_free, {FramePtr}); |
| 1278 | |
| 1279 | SmallVector<Expr *, 2> DeleteArgs{CoroFree}; |
| 1280 | |
| 1281 | // Check if we need to pass the size. |
| 1282 | const auto *OpDeleteType = |
| 1283 | OpDeleteQualType.getTypePtr()->getAs<FunctionProtoType>(); |
| 1284 | if (OpDeleteType->getNumParams() > 1) |
| 1285 | DeleteArgs.push_back(FrameSize); |
| 1286 | |
| 1287 | ExprResult DeleteExpr = |
Richard Smith | 255b85f | 2019-05-08 01:36:36 +0000 | [diff] [blame] | 1288 | S.BuildCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1289 | DeleteExpr = |
| 1290 | S.ActOnFinishFullExpr(DeleteExpr.get(), /*DiscardedValue*/ false); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1291 | if (DeleteExpr.isInvalid()) |
| 1292 | return false; |
| 1293 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1294 | this->Allocate = NewExpr.get(); |
| 1295 | this->Deallocate = DeleteExpr.get(); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1296 | |
| 1297 | return true; |
| 1298 | } |
| 1299 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1300 | bool CoroutineStmtBuilder::makeOnFallthrough() { |
| 1301 | assert(!IsPromiseDependentType && |
| 1302 | "cannot make statement while the promise type is dependent"); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1303 | |
| 1304 | // [dcl.fct.def.coroutine]/4 |
| 1305 | // The unqualified-ids 'return_void' and 'return_value' are looked up in |
| 1306 | // the scope of class P. If both are found, the program is ill-formed. |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 1307 | bool HasRVoid, HasRValue; |
| 1308 | LookupResult LRVoid = |
| 1309 | lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid); |
| 1310 | LookupResult LRValue = |
| 1311 | lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1312 | |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 1313 | StmtResult Fallthrough; |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1314 | if (HasRVoid && HasRValue) { |
| 1315 | // FIXME Improve this diagnostic |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 1316 | S.Diag(FD.getLocation(), |
| 1317 | diag::err_coroutine_promise_incompatible_return_functions) |
| 1318 | << PromiseRecordDecl; |
| 1319 | S.Diag(LRVoid.getRepresentativeDecl()->getLocation(), |
| 1320 | diag::note_member_first_declared_here) |
| 1321 | << LRVoid.getLookupName(); |
| 1322 | S.Diag(LRValue.getRepresentativeDecl()->getLocation(), |
| 1323 | diag::note_member_first_declared_here) |
| 1324 | << LRValue.getLookupName(); |
| 1325 | return false; |
| 1326 | } else if (!HasRVoid && !HasRValue) { |
| 1327 | // FIXME: The PDTS currently specifies this case as UB, not ill-formed. |
| 1328 | // However we still diagnose this as an error since until the PDTS is fixed. |
| 1329 | S.Diag(FD.getLocation(), |
| 1330 | diag::err_coroutine_promise_requires_return_function) |
| 1331 | << PromiseRecordDecl; |
| 1332 | S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here) |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1333 | << PromiseRecordDecl; |
| 1334 | return false; |
| 1335 | } else if (HasRVoid) { |
| 1336 | // If the unqualified-id return_void is found, flowing off the end of a |
| 1337 | // coroutine is equivalent to a co_return with no operand. Otherwise, |
| 1338 | // flowing off the end of a coroutine results in undefined behavior. |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1339 | Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr, |
| 1340 | /*IsImplicit*/false); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1341 | Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get()); |
| 1342 | if (Fallthrough.isInvalid()) |
| 1343 | return false; |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 1344 | } |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1345 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1346 | this->OnFallthrough = Fallthrough.get(); |
| 1347 | return true; |
| 1348 | } |
| 1349 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1350 | bool CoroutineStmtBuilder::makeOnException() { |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1351 | // Try to form 'p.unhandled_exception();' |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1352 | assert(!IsPromiseDependentType && |
| 1353 | "cannot make statement while the promise type is dependent"); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1354 | |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1355 | const bool RequireUnhandledException = S.getLangOpts().CXXExceptions; |
| 1356 | |
| 1357 | if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) { |
| 1358 | auto DiagID = |
| 1359 | RequireUnhandledException |
| 1360 | ? diag::err_coroutine_promise_unhandled_exception_required |
| 1361 | : diag:: |
| 1362 | warn_coroutine_promise_unhandled_exception_required_with_exceptions; |
| 1363 | S.Diag(Loc, DiagID) << PromiseRecordDecl; |
Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 1364 | S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here) |
| 1365 | << PromiseRecordDecl; |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1366 | return !RequireUnhandledException; |
| 1367 | } |
| 1368 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1369 | // If exceptions are disabled, don't try to build OnException. |
| 1370 | if (!S.getLangOpts().CXXExceptions) |
| 1371 | return true; |
| 1372 | |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1373 | ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc, |
| 1374 | "unhandled_exception", None); |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1375 | UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc, |
| 1376 | /*DiscardedValue*/ false); |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1377 | if (UnhandledException.isInvalid()) |
| 1378 | return false; |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1379 | |
Gor Nishanov | 5b050e4 | 2017-05-22 22:33:17 +0000 | [diff] [blame] | 1380 | // Since the body of the coroutine will be wrapped in try-catch, it will |
| 1381 | // be incompatible with SEH __try if present in a function. |
| 1382 | if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) { |
| 1383 | S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions); |
| 1384 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 1385 | << Fn.getFirstCoroutineStmtKeyword(); |
| 1386 | return false; |
| 1387 | } |
| 1388 | |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1389 | this->OnException = UnhandledException.get(); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1390 | return true; |
| 1391 | } |
| 1392 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1393 | bool CoroutineStmtBuilder::makeReturnObject() { |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1394 | // Build implicit 'p.get_return_object()' expression and form initialization |
| 1395 | // of return type from it. |
| 1396 | ExprResult ReturnObject = |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1397 | buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None); |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1398 | if (ReturnObject.isInvalid()) |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1399 | return false; |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1400 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1401 | this->ReturnValue = ReturnObject.get(); |
| 1402 | return true; |
| 1403 | } |
| 1404 | |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1405 | static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) { |
| 1406 | if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) { |
| 1407 | auto *MethodDecl = MbrRef->getMethodDecl(); |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 1408 | S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here) |
| 1409 | << MethodDecl; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1410 | } |
| 1411 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 1412 | << Fn.getFirstCoroutineStmtKeyword(); |
| 1413 | } |
| 1414 | |
| 1415 | bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() { |
| 1416 | assert(!IsPromiseDependentType && |
| 1417 | "cannot make statement while the promise type is dependent"); |
| 1418 | assert(this->ReturnValue && "ReturnValue must be already formed"); |
| 1419 | |
| 1420 | QualType const GroType = this->ReturnValue->getType(); |
| 1421 | assert(!GroType->isDependentType() && |
| 1422 | "get_return_object type must no longer be dependent"); |
| 1423 | |
| 1424 | QualType const FnRetType = FD.getReturnType(); |
| 1425 | assert(!FnRetType->isDependentType() && |
| 1426 | "get_return_object type must no longer be dependent"); |
| 1427 | |
| 1428 | if (FnRetType->isVoidType()) { |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1429 | ExprResult Res = |
| 1430 | S.ActOnFinishFullExpr(this->ReturnValue, Loc, /*DiscardedValue*/ false); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1431 | if (Res.isInvalid()) |
| 1432 | return false; |
| 1433 | |
| 1434 | this->ResultDecl = Res.get(); |
| 1435 | return true; |
| 1436 | } |
| 1437 | |
| 1438 | if (GroType->isVoidType()) { |
| 1439 | // Trigger a nice error message. |
| 1440 | InitializedEntity Entity = |
| 1441 | InitializedEntity::InitializeResult(Loc, FnRetType, false); |
| 1442 | S.PerformMoveOrCopyInitialization(Entity, nullptr, FnRetType, ReturnValue); |
| 1443 | noteMemberDeclaredHere(S, ReturnValue, Fn); |
| 1444 | return false; |
| 1445 | } |
| 1446 | |
| 1447 | auto *GroDecl = VarDecl::Create( |
| 1448 | S.Context, &FD, FD.getLocation(), FD.getLocation(), |
| 1449 | &S.PP.getIdentifierTable().get("__coro_gro"), GroType, |
| 1450 | S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None); |
| 1451 | |
| 1452 | S.CheckVariableDeclarationType(GroDecl); |
| 1453 | if (GroDecl->isInvalidDecl()) |
| 1454 | return false; |
| 1455 | |
| 1456 | InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl); |
| 1457 | ExprResult Res = S.PerformMoveOrCopyInitialization(Entity, nullptr, GroType, |
| 1458 | this->ReturnValue); |
| 1459 | if (Res.isInvalid()) |
| 1460 | return false; |
| 1461 | |
Aaron Ballman | fb6deeb | 2019-01-04 16:58:14 +0000 | [diff] [blame] | 1462 | Res = S.ActOnFinishFullExpr(Res.get(), /*DiscardedValue*/ false); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1463 | if (Res.isInvalid()) |
| 1464 | return false; |
| 1465 | |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1466 | S.AddInitializerToDecl(GroDecl, Res.get(), |
| 1467 | /*DirectInit=*/false); |
| 1468 | |
| 1469 | S.FinalizeDeclaration(GroDecl); |
| 1470 | |
| 1471 | // Form a declaration statement for the return declaration, so that AST |
| 1472 | // visitors can more easily find it. |
| 1473 | StmtResult GroDeclStmt = |
| 1474 | S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(GroDecl), Loc, Loc); |
| 1475 | if (GroDeclStmt.isInvalid()) |
| 1476 | return false; |
| 1477 | |
| 1478 | this->ResultDecl = GroDeclStmt.get(); |
| 1479 | |
| 1480 | ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc); |
| 1481 | if (declRef.isInvalid()) |
| 1482 | return false; |
| 1483 | |
| 1484 | StmtResult ReturnStmt = S.BuildReturnStmt(Loc, declRef.get()); |
| 1485 | if (ReturnStmt.isInvalid()) { |
| 1486 | noteMemberDeclaredHere(S, ReturnValue, Fn); |
| 1487 | return false; |
| 1488 | } |
Eric Fiselier | 8ed9727 | 2018-02-01 23:47:54 +0000 | [diff] [blame] | 1489 | if (cast<clang::ReturnStmt>(ReturnStmt.get())->getNRVOCandidate() == GroDecl) |
| 1490 | GroDecl->setNRVOVariable(true); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1491 | |
| 1492 | this->ReturnStmt = ReturnStmt.get(); |
| 1493 | return true; |
| 1494 | } |
| 1495 | |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1496 | // Create a static_cast\<T&&>(expr). |
| 1497 | static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) { |
| 1498 | if (T.isNull()) |
| 1499 | T = E->getType(); |
| 1500 | QualType TargetType = S.BuildReferenceType( |
| 1501 | T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1502 | SourceLocation ExprLoc = E->getBeginLoc(); |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1503 | TypeSourceInfo *TargetLoc = |
| 1504 | S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc); |
| 1505 | |
| 1506 | return S |
| 1507 | .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 1508 | SourceRange(ExprLoc, ExprLoc), E->getSourceRange()) |
| 1509 | .get(); |
| 1510 | } |
| 1511 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1512 | /// Build a variable declaration for move parameter. |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1513 | static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type, |
Eric Fiselier | de7943b | 2017-06-03 00:22:18 +0000 | [diff] [blame] | 1514 | IdentifierInfo *II) { |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1515 | TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc); |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1516 | VarDecl *Decl = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type, |
| 1517 | TInfo, SC_None); |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1518 | Decl->setImplicit(); |
| 1519 | return Decl; |
| 1520 | } |
| 1521 | |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 1522 | // Build statements that move coroutine function parameters to the coroutine |
| 1523 | // frame, and store them on the function scope info. |
| 1524 | bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) { |
| 1525 | assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); |
| 1526 | auto *FD = cast<FunctionDecl>(CurContext); |
| 1527 | |
| 1528 | auto *ScopeInfo = getCurFunction(); |
| 1529 | assert(ScopeInfo->CoroutineParameterMoves.empty() && |
| 1530 | "Should not build parameter moves twice"); |
| 1531 | |
| 1532 | for (auto *PD : FD->parameters()) { |
| 1533 | if (PD->getType()->isDependentType()) |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1534 | continue; |
| 1535 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1536 | ExprResult PDRefExpr = |
| 1537 | BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
| 1538 | ExprValueKind::VK_LValue, Loc); // FIXME: scope? |
| 1539 | if (PDRefExpr.isInvalid()) |
| 1540 | return false; |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1541 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1542 | Expr *CExpr = nullptr; |
| 1543 | if (PD->getType()->getAsCXXRecordDecl() || |
| 1544 | PD->getType()->isRValueReferenceType()) |
| 1545 | CExpr = castForMoving(*this, PDRefExpr.get()); |
| 1546 | else |
| 1547 | CExpr = PDRefExpr.get(); |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1548 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1549 | auto D = buildVarDecl(*this, Loc, PD->getType(), PD->getIdentifier()); |
| 1550 | AddInitializerToDecl(D, CExpr, /*DirectInit=*/true); |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1551 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1552 | // Convert decl to a statement. |
| 1553 | StmtResult Stmt = ActOnDeclStmt(ConvertDeclToDeclGroup(D), Loc, Loc); |
| 1554 | if (Stmt.isInvalid()) |
| 1555 | return false; |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1556 | |
Brian Gesiak | 9860622 | 2018-02-15 20:37:22 +0000 | [diff] [blame] | 1557 | ScopeInfo->CoroutineParameterMoves.insert(std::make_pair(PD, Stmt.get())); |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1558 | } |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1559 | return true; |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1560 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1561 | |
| 1562 | StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) { |
| 1563 | CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args); |
| 1564 | if (!Res) |
| 1565 | return StmtError(); |
| 1566 | return Res; |
| 1567 | } |
Brian Gesiak | 3e65d9a | 2018-07-14 18:21:44 +0000 | [diff] [blame] | 1568 | |
| 1569 | ClassTemplateDecl *Sema::lookupCoroutineTraits(SourceLocation KwLoc, |
| 1570 | SourceLocation FuncLoc) { |
| 1571 | if (!StdCoroutineTraitsCache) { |
| 1572 | if (auto StdExp = lookupStdExperimentalNamespace()) { |
| 1573 | LookupResult Result(*this, |
| 1574 | &PP.getIdentifierTable().get("coroutine_traits"), |
| 1575 | FuncLoc, LookupOrdinaryName); |
| 1576 | if (!LookupQualifiedName(Result, StdExp)) { |
| 1577 | Diag(KwLoc, diag::err_implied_coroutine_type_not_found) |
| 1578 | << "std::experimental::coroutine_traits"; |
| 1579 | return nullptr; |
| 1580 | } |
| 1581 | if (!(StdCoroutineTraitsCache = |
| 1582 | Result.getAsSingle<ClassTemplateDecl>())) { |
| 1583 | Result.suppressDiagnostics(); |
| 1584 | NamedDecl *Found = *Result.begin(); |
| 1585 | Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits); |
| 1586 | return nullptr; |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | return StdCoroutineTraitsCache; |
| 1591 | } |