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