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