Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1 | //===--- SemaCoroutines.cpp - Semantic Analysis for Coroutines ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for C++ Coroutines. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 14 | #include "CoroutineStmtBuilder.h" |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/StmtCXX.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Initialization.h" |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Overload.h" |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 21 | #include "clang/Sema/SemaInternal.h" |
| 22 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace sema; |
| 25 | |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 26 | static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, |
| 27 | SourceLocation Loc, bool &Res) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 28 | DeclarationName DN = S.PP.getIdentifierInfo(Name); |
| 29 | LookupResult LR(S, DN, Loc, Sema::LookupMemberName); |
| 30 | // Suppress diagnostics when a private member is selected. The same warnings |
| 31 | // will be produced again when building the call. |
| 32 | LR.suppressDiagnostics(); |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 33 | Res = S.LookupQualifiedName(LR, RD); |
| 34 | return LR; |
| 35 | } |
| 36 | |
| 37 | static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, |
| 38 | SourceLocation Loc) { |
| 39 | bool Res; |
| 40 | lookupMember(S, Name, RD, Loc, Res); |
| 41 | return Res; |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 44 | /// Look up the std::coroutine_traits<...>::promise_type for the given |
| 45 | /// function type. |
| 46 | static QualType lookupPromiseType(Sema &S, const FunctionProtoType *FnType, |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 47 | SourceLocation KwLoc, |
| 48 | SourceLocation FuncLoc) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 49 | // FIXME: Cache std::coroutine_traits once we've found it. |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 50 | NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace(); |
| 51 | if (!StdExp) { |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 52 | S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found) |
| 53 | << "std::experimental::coroutine_traits"; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 54 | return QualType(); |
| 55 | } |
| 56 | |
| 57 | LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_traits"), |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 58 | FuncLoc, Sema::LookupOrdinaryName); |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 59 | if (!S.LookupQualifiedName(Result, StdExp)) { |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 60 | S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found) |
| 61 | << "std::experimental::coroutine_traits"; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 62 | return QualType(); |
| 63 | } |
| 64 | |
| 65 | ClassTemplateDecl *CoroTraits = Result.getAsSingle<ClassTemplateDecl>(); |
| 66 | if (!CoroTraits) { |
| 67 | Result.suppressDiagnostics(); |
| 68 | // We found something weird. Complain about the first thing we found. |
| 69 | NamedDecl *Found = *Result.begin(); |
| 70 | S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits); |
| 71 | return QualType(); |
| 72 | } |
| 73 | |
| 74 | // Form template argument list for coroutine_traits<R, P1, P2, ...>. |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 75 | TemplateArgumentListInfo Args(KwLoc, KwLoc); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 76 | Args.addArgument(TemplateArgumentLoc( |
| 77 | TemplateArgument(FnType->getReturnType()), |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 78 | S.Context.getTrivialTypeSourceInfo(FnType->getReturnType(), KwLoc))); |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 79 | // FIXME: If the function is a non-static member function, add the type |
| 80 | // of the implicit object parameter before the formal parameters. |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 81 | for (QualType T : FnType->getParamTypes()) |
| 82 | Args.addArgument(TemplateArgumentLoc( |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 83 | TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc))); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 84 | |
| 85 | // Build the template-id. |
| 86 | QualType CoroTrait = |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 87 | S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 88 | if (CoroTrait.isNull()) |
| 89 | return QualType(); |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 90 | if (S.RequireCompleteType(KwLoc, CoroTrait, |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 91 | diag::err_coroutine_type_missing_specialization)) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 92 | return QualType(); |
| 93 | |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 94 | auto *RD = CoroTrait->getAsCXXRecordDecl(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 95 | assert(RD && "specialization of class template is not a class?"); |
| 96 | |
| 97 | // Look up the ::promise_type member. |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 98 | LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 99 | Sema::LookupOrdinaryName); |
| 100 | S.LookupQualifiedName(R, RD); |
| 101 | auto *Promise = R.getAsSingle<TypeDecl>(); |
| 102 | if (!Promise) { |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 103 | S.Diag(FuncLoc, |
| 104 | diag::err_implied_std_coroutine_traits_promise_type_not_found) |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 105 | << RD; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 106 | return QualType(); |
| 107 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 108 | // The promise type is required to be a class type. |
| 109 | QualType PromiseType = S.Context.getTypeDeclType(Promise); |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 110 | |
| 111 | auto buildElaboratedType = [&]() { |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 112 | auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, StdExp); |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 113 | NNS = NestedNameSpecifier::Create(S.Context, NNS, false, |
| 114 | CoroTrait.getTypePtr()); |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 115 | return S.Context.getElaboratedType(ETK_None, NNS, PromiseType); |
| 116 | }; |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 117 | |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 118 | if (!PromiseType->getAsCXXRecordDecl()) { |
| 119 | S.Diag(FuncLoc, |
| 120 | diag::err_implied_std_coroutine_traits_promise_type_not_class) |
| 121 | << buildElaboratedType(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 122 | return QualType(); |
| 123 | } |
Eric Fiselier | 89bf0e7 | 2017-03-06 22:52:28 +0000 | [diff] [blame] | 124 | if (S.RequireCompleteType(FuncLoc, buildElaboratedType(), |
| 125 | diag::err_coroutine_promise_type_incomplete)) |
| 126 | return QualType(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 127 | |
| 128 | return PromiseType; |
| 129 | } |
| 130 | |
Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 131 | /// Look up the std::experimental::coroutine_handle<PromiseType>. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 132 | static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType, |
| 133 | SourceLocation Loc) { |
| 134 | if (PromiseType.isNull()) |
| 135 | return QualType(); |
| 136 | |
| 137 | NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace(); |
| 138 | assert(StdExp && "Should already be diagnosed"); |
| 139 | |
| 140 | LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"), |
| 141 | Loc, Sema::LookupOrdinaryName); |
| 142 | if (!S.LookupQualifiedName(Result, StdExp)) { |
| 143 | S.Diag(Loc, diag::err_implied_coroutine_type_not_found) |
| 144 | << "std::experimental::coroutine_handle"; |
| 145 | return QualType(); |
| 146 | } |
| 147 | |
| 148 | ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>(); |
| 149 | if (!CoroHandle) { |
| 150 | Result.suppressDiagnostics(); |
| 151 | // We found something weird. Complain about the first thing we found. |
| 152 | NamedDecl *Found = *Result.begin(); |
| 153 | S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle); |
| 154 | return QualType(); |
| 155 | } |
| 156 | |
| 157 | // Form template argument list for coroutine_handle<Promise>. |
| 158 | TemplateArgumentListInfo Args(Loc, Loc); |
| 159 | Args.addArgument(TemplateArgumentLoc( |
| 160 | TemplateArgument(PromiseType), |
| 161 | S.Context.getTrivialTypeSourceInfo(PromiseType, Loc))); |
| 162 | |
| 163 | // Build the template-id. |
| 164 | QualType CoroHandleType = |
| 165 | S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args); |
| 166 | if (CoroHandleType.isNull()) |
| 167 | return QualType(); |
| 168 | if (S.RequireCompleteType(Loc, CoroHandleType, |
| 169 | diag::err_coroutine_type_missing_specialization)) |
| 170 | return QualType(); |
| 171 | |
| 172 | return CoroHandleType; |
| 173 | } |
| 174 | |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 175 | static bool isValidCoroutineContext(Sema &S, SourceLocation Loc, |
| 176 | StringRef Keyword) { |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 177 | // 'co_await' and 'co_yield' are not permitted in unevaluated operands. |
| 178 | if (S.isUnevaluatedContext()) { |
| 179 | S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword; |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 180 | return false; |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 181 | } |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 182 | |
| 183 | // Any other usage must be within a function. |
| 184 | auto *FD = dyn_cast<FunctionDecl>(S.CurContext); |
| 185 | if (!FD) { |
| 186 | S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext) |
| 187 | ? diag::err_coroutine_objc_method |
| 188 | : diag::err_coroutine_outside_function) << Keyword; |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 189 | return false; |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 192 | // An enumeration for mapping the diagnostic type to the correct diagnostic |
| 193 | // selection index. |
| 194 | enum InvalidFuncDiag { |
| 195 | DiagCtor = 0, |
| 196 | DiagDtor, |
| 197 | DiagCopyAssign, |
| 198 | DiagMoveAssign, |
| 199 | DiagMain, |
| 200 | DiagConstexpr, |
| 201 | DiagAutoRet, |
| 202 | DiagVarargs, |
| 203 | }; |
| 204 | bool Diagnosed = false; |
| 205 | auto DiagInvalid = [&](InvalidFuncDiag ID) { |
| 206 | S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword; |
| 207 | Diagnosed = true; |
| 208 | return false; |
| 209 | }; |
| 210 | |
| 211 | // Diagnose when a constructor, destructor, copy/move assignment operator, |
| 212 | // or the function 'main' are declared as a coroutine. |
| 213 | auto *MD = dyn_cast<CXXMethodDecl>(FD); |
| 214 | if (MD && isa<CXXConstructorDecl>(MD)) |
| 215 | return DiagInvalid(DiagCtor); |
| 216 | else if (MD && isa<CXXDestructorDecl>(MD)) |
| 217 | return DiagInvalid(DiagDtor); |
| 218 | else if (MD && MD->isCopyAssignmentOperator()) |
| 219 | return DiagInvalid(DiagCopyAssign); |
| 220 | else if (MD && MD->isMoveAssignmentOperator()) |
| 221 | return DiagInvalid(DiagMoveAssign); |
| 222 | else if (FD->isMain()) |
| 223 | return DiagInvalid(DiagMain); |
| 224 | |
| 225 | // Emit a diagnostics for each of the following conditions which is not met. |
| 226 | if (FD->isConstexpr()) |
| 227 | DiagInvalid(DiagConstexpr); |
| 228 | if (FD->getReturnType()->isUndeducedType()) |
| 229 | DiagInvalid(DiagAutoRet); |
| 230 | if (FD->isVariadic()) |
| 231 | DiagInvalid(DiagVarargs); |
| 232 | |
| 233 | return !Diagnosed; |
| 234 | } |
| 235 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 236 | static ExprResult buildOperatorCoawaitLookupExpr(Sema &SemaRef, Scope *S, |
| 237 | SourceLocation Loc) { |
| 238 | DeclarationName OpName = |
| 239 | SemaRef.Context.DeclarationNames.getCXXOperatorName(OO_Coawait); |
| 240 | LookupResult Operators(SemaRef, OpName, SourceLocation(), |
| 241 | Sema::LookupOperatorName); |
| 242 | SemaRef.LookupName(Operators, S); |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 243 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 244 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
| 245 | const auto &Functions = Operators.asUnresolvedSet(); |
| 246 | bool IsOverloaded = |
| 247 | Functions.size() > 1 || |
| 248 | (Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin())); |
| 249 | Expr *CoawaitOp = UnresolvedLookupExpr::Create( |
| 250 | SemaRef.Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(), |
| 251 | DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, IsOverloaded, |
| 252 | Functions.begin(), Functions.end()); |
| 253 | assert(CoawaitOp); |
| 254 | return CoawaitOp; |
| 255 | } |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 256 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 257 | /// Build a call to 'operator co_await' if there is a suitable operator for |
| 258 | /// the given expression. |
| 259 | static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, SourceLocation Loc, |
| 260 | Expr *E, |
| 261 | UnresolvedLookupExpr *Lookup) { |
| 262 | UnresolvedSet<16> Functions; |
| 263 | Functions.append(Lookup->decls_begin(), Lookup->decls_end()); |
| 264 | return SemaRef.CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E); |
| 265 | } |
Eric Fiselier | c8efda7 | 2016-10-27 18:43:28 +0000 | [diff] [blame] | 266 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 267 | static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S, |
| 268 | SourceLocation Loc, Expr *E) { |
| 269 | ExprResult R = buildOperatorCoawaitLookupExpr(SemaRef, S, Loc); |
| 270 | if (R.isInvalid()) |
| 271 | return ExprError(); |
| 272 | return buildOperatorCoawaitCall(SemaRef, Loc, E, |
| 273 | cast<UnresolvedLookupExpr>(R.get())); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 276 | static Expr *buildBuiltinCall(Sema &S, SourceLocation Loc, Builtin::ID Id, |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 277 | MultiExprArg CallArgs) { |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 278 | StringRef Name = S.Context.BuiltinInfo.getName(Id); |
| 279 | LookupResult R(S, &S.Context.Idents.get(Name), Loc, Sema::LookupOrdinaryName); |
| 280 | S.LookupName(R, S.TUScope, /*AllowBuiltinCreation=*/true); |
| 281 | |
| 282 | auto *BuiltInDecl = R.getAsSingle<FunctionDecl>(); |
| 283 | assert(BuiltInDecl && "failed to find builtin declaration"); |
| 284 | |
| 285 | ExprResult DeclRef = |
| 286 | S.BuildDeclRefExpr(BuiltInDecl, BuiltInDecl->getType(), VK_LValue, Loc); |
| 287 | assert(DeclRef.isUsable() && "Builtin reference cannot fail"); |
| 288 | |
| 289 | ExprResult Call = |
| 290 | S.ActOnCallExpr(/*Scope=*/nullptr, DeclRef.get(), Loc, CallArgs, Loc); |
| 291 | |
| 292 | assert(!Call.isInvalid() && "Call to builtin cannot fail!"); |
| 293 | return Call.get(); |
| 294 | } |
| 295 | |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 296 | static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType, |
| 297 | SourceLocation Loc) { |
| 298 | QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc); |
| 299 | if (CoroHandleType.isNull()) |
| 300 | return ExprError(); |
| 301 | |
| 302 | DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType); |
| 303 | LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc, |
| 304 | Sema::LookupOrdinaryName); |
| 305 | if (!S.LookupQualifiedName(Found, LookupCtx)) { |
| 306 | S.Diag(Loc, diag::err_coroutine_handle_missing_member) |
| 307 | << "from_address"; |
| 308 | return ExprError(); |
| 309 | } |
| 310 | |
| 311 | Expr *FramePtr = |
| 312 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {}); |
| 313 | |
| 314 | CXXScopeSpec SS; |
| 315 | ExprResult FromAddr = |
| 316 | S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); |
| 317 | if (FromAddr.isInvalid()) |
| 318 | return ExprError(); |
| 319 | |
| 320 | return S.ActOnCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc); |
| 321 | } |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 322 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 323 | struct ReadySuspendResumeResult { |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 324 | enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume }; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 325 | Expr *Results[3]; |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 326 | OpaqueValueExpr *OpaqueValue; |
| 327 | bool IsInvalid; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 328 | }; |
| 329 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 330 | static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc, |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 331 | StringRef Name, MultiExprArg Args) { |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 332 | DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc); |
| 333 | |
| 334 | // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&. |
| 335 | CXXScopeSpec SS; |
| 336 | ExprResult Result = S.BuildMemberReferenceExpr( |
| 337 | Base, Base->getType(), Loc, /*IsPtr=*/false, SS, |
| 338 | SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr, |
| 339 | /*Scope=*/nullptr); |
| 340 | if (Result.isInvalid()) |
| 341 | return ExprError(); |
| 342 | |
| 343 | return S.ActOnCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr); |
| 344 | } |
| 345 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 346 | /// Build calls to await_ready, await_suspend, and await_resume for a co_await |
| 347 | /// expression. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 348 | static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise, |
| 349 | SourceLocation Loc, Expr *E) { |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 350 | OpaqueValueExpr *Operand = new (S.Context) |
| 351 | OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E); |
| 352 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 353 | // Assume invalid until we see otherwise. |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 354 | ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/true}; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 355 | |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 356 | ExprResult CoroHandleRes = buildCoroutineHandle(S, CoroPromise->getType(), Loc); |
| 357 | if (CoroHandleRes.isInvalid()) |
| 358 | return Calls; |
| 359 | Expr *CoroHandle = CoroHandleRes.get(); |
| 360 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 361 | const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"}; |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 362 | MultiExprArg Args[] = {None, CoroHandle, None}; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 363 | 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] | 364 | ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], Args[I]); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 365 | if (Result.isInvalid()) |
| 366 | return Calls; |
| 367 | Calls.Results[I] = Result.get(); |
| 368 | } |
| 369 | |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 370 | // Assume the calls are valid; all further checking should make them invalid. |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 371 | Calls.IsInvalid = false; |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 372 | |
| 373 | using ACT = ReadySuspendResumeResult::AwaitCallType; |
| 374 | CallExpr *AwaitReady = cast<CallExpr>(Calls.Results[ACT::ACT_Ready]); |
| 375 | if (!AwaitReady->getType()->isDependentType()) { |
| 376 | // [expr.await]p3 [...] |
| 377 | // — await-ready is the expression e.await_ready(), contextually converted |
| 378 | // to bool. |
| 379 | ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady); |
| 380 | if (Conv.isInvalid()) { |
| 381 | S.Diag(AwaitReady->getDirectCallee()->getLocStart(), |
| 382 | diag::note_await_ready_no_bool_conversion); |
| 383 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
| 384 | << AwaitReady->getDirectCallee() << E->getSourceRange(); |
| 385 | Calls.IsInvalid = true; |
| 386 | } |
| 387 | Calls.Results[ACT::ACT_Ready] = Conv.get(); |
| 388 | } |
| 389 | CallExpr *AwaitSuspend = cast<CallExpr>(Calls.Results[ACT::ACT_Suspend]); |
| 390 | if (!AwaitSuspend->getType()->isDependentType()) { |
| 391 | // [expr.await]p3 [...] |
| 392 | // - await-suspend is the expression e.await_suspend(h), which shall be |
| 393 | // a prvalue of type void or bool. |
Eric Fiselier | 84ee7ff | 2017-05-31 23:41:11 +0000 | [diff] [blame^] | 394 | QualType RetType = AwaitSuspend->getCallReturnType(S.Context); |
| 395 | // non-class prvalues always have cv-unqualified types |
| 396 | QualType AdjRetType = RetType.getUnqualifiedType(); |
| 397 | if (RetType->isReferenceType() || |
| 398 | (AdjRetType != S.Context.BoolTy && AdjRetType != S.Context.VoidTy)) { |
Eric Fiselier | d978e53 | 2017-05-28 18:21:12 +0000 | [diff] [blame] | 399 | S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(), |
| 400 | diag::err_await_suspend_invalid_return_type) |
| 401 | << RetType; |
| 402 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
| 403 | << AwaitSuspend->getDirectCallee(); |
| 404 | Calls.IsInvalid = true; |
| 405 | } |
| 406 | } |
| 407 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 408 | return Calls; |
| 409 | } |
| 410 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 411 | static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise, |
| 412 | SourceLocation Loc, StringRef Name, |
| 413 | MultiExprArg Args) { |
| 414 | |
| 415 | // Form a reference to the promise. |
| 416 | ExprResult PromiseRef = S.BuildDeclRefExpr( |
| 417 | Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc); |
| 418 | if (PromiseRef.isInvalid()) |
| 419 | return ExprError(); |
| 420 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 421 | return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args); |
| 422 | } |
| 423 | |
| 424 | VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) { |
| 425 | assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); |
| 426 | auto *FD = cast<FunctionDecl>(CurContext); |
| 427 | |
| 428 | QualType T = |
| 429 | FD->getType()->isDependentType() |
| 430 | ? Context.DependentTy |
| 431 | : lookupPromiseType(*this, FD->getType()->castAs<FunctionProtoType>(), |
| 432 | Loc, FD->getLocation()); |
| 433 | if (T.isNull()) |
| 434 | return nullptr; |
| 435 | |
| 436 | auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(), |
| 437 | &PP.getIdentifierTable().get("__promise"), T, |
| 438 | Context.getTrivialTypeSourceInfo(T, Loc), SC_None); |
| 439 | CheckVariableDeclarationType(VD); |
| 440 | if (VD->isInvalidDecl()) |
| 441 | return nullptr; |
| 442 | ActOnUninitializedDecl(VD); |
Eric Fiselier | 37b8a37 | 2017-05-31 19:36:59 +0000 | [diff] [blame] | 443 | FD->addDecl(VD); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 444 | assert(!VD->isInvalidDecl()); |
| 445 | return VD; |
| 446 | } |
| 447 | |
| 448 | /// Check that this is a context in which a coroutine suspension can appear. |
| 449 | static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc, |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 450 | StringRef Keyword, |
| 451 | bool IsImplicit = false) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 452 | if (!isValidCoroutineContext(S, Loc, Keyword)) |
| 453 | return nullptr; |
| 454 | |
| 455 | assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope"); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 456 | |
| 457 | auto *ScopeInfo = S.getCurFunction(); |
| 458 | assert(ScopeInfo && "missing function scope for function"); |
| 459 | |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 460 | if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit) |
| 461 | ScopeInfo->setFirstCoroutineStmt(Loc, Keyword); |
| 462 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 463 | if (ScopeInfo->CoroutinePromise) |
| 464 | return ScopeInfo; |
| 465 | |
| 466 | ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc); |
| 467 | if (!ScopeInfo->CoroutinePromise) |
| 468 | return nullptr; |
| 469 | |
| 470 | return ScopeInfo; |
| 471 | } |
| 472 | |
| 473 | static bool actOnCoroutineBodyStart(Sema &S, Scope *SC, SourceLocation KWLoc, |
| 474 | StringRef Keyword) { |
| 475 | if (!checkCoroutineContext(S, KWLoc, Keyword)) |
| 476 | return false; |
| 477 | auto *ScopeInfo = S.getCurFunction(); |
| 478 | assert(ScopeInfo->CoroutinePromise); |
| 479 | |
| 480 | // If we have existing coroutine statements then we have already built |
| 481 | // the initial and final suspend points. |
| 482 | if (!ScopeInfo->NeedsCoroutineSuspends) |
| 483 | return true; |
| 484 | |
| 485 | ScopeInfo->setNeedsCoroutineSuspends(false); |
| 486 | |
| 487 | auto *Fn = cast<FunctionDecl>(S.CurContext); |
| 488 | SourceLocation Loc = Fn->getLocation(); |
| 489 | // Build the initial suspend point |
| 490 | auto buildSuspends = [&](StringRef Name) mutable -> StmtResult { |
| 491 | ExprResult Suspend = |
| 492 | buildPromiseCall(S, ScopeInfo->CoroutinePromise, Loc, Name, None); |
| 493 | if (Suspend.isInvalid()) |
| 494 | return StmtError(); |
| 495 | Suspend = buildOperatorCoawaitCall(S, SC, Loc, Suspend.get()); |
| 496 | if (Suspend.isInvalid()) |
| 497 | return StmtError(); |
| 498 | Suspend = S.BuildResolvedCoawaitExpr(Loc, Suspend.get(), |
| 499 | /*IsImplicit*/ true); |
| 500 | Suspend = S.ActOnFinishFullExpr(Suspend.get()); |
| 501 | if (Suspend.isInvalid()) { |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 502 | S.Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required) |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 503 | << ((Name == "initial_suspend") ? 0 : 1); |
| 504 | S.Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword; |
| 505 | return StmtError(); |
| 506 | } |
| 507 | return cast<Stmt>(Suspend.get()); |
| 508 | }; |
| 509 | |
| 510 | StmtResult InitSuspend = buildSuspends("initial_suspend"); |
| 511 | if (InitSuspend.isInvalid()) |
| 512 | return true; |
| 513 | |
| 514 | StmtResult FinalSuspend = buildSuspends("final_suspend"); |
| 515 | if (FinalSuspend.isInvalid()) |
| 516 | return true; |
| 517 | |
| 518 | ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get()); |
| 519 | |
| 520 | return true; |
| 521 | } |
| 522 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 523 | ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 524 | if (!actOnCoroutineBodyStart(*this, S, Loc, "co_await")) { |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 525 | CorrectDelayedTyposInExpr(E); |
| 526 | return ExprError(); |
| 527 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 528 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 529 | if (E->getType()->isPlaceholderType()) { |
| 530 | ExprResult R = CheckPlaceholderExpr(E); |
| 531 | if (R.isInvalid()) return ExprError(); |
| 532 | E = R.get(); |
| 533 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 534 | ExprResult Lookup = buildOperatorCoawaitLookupExpr(*this, S, Loc); |
| 535 | if (Lookup.isInvalid()) |
| 536 | return ExprError(); |
| 537 | return BuildUnresolvedCoawaitExpr(Loc, E, |
| 538 | cast<UnresolvedLookupExpr>(Lookup.get())); |
| 539 | } |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 540 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 541 | ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *E, |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 542 | UnresolvedLookupExpr *Lookup) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 543 | auto *FSI = checkCoroutineContext(*this, Loc, "co_await"); |
| 544 | if (!FSI) |
| 545 | return ExprError(); |
| 546 | |
| 547 | if (E->getType()->isPlaceholderType()) { |
| 548 | ExprResult R = CheckPlaceholderExpr(E); |
| 549 | if (R.isInvalid()) |
| 550 | return ExprError(); |
| 551 | E = R.get(); |
| 552 | } |
| 553 | |
| 554 | auto *Promise = FSI->CoroutinePromise; |
| 555 | if (Promise->getType()->isDependentType()) { |
| 556 | Expr *Res = |
| 557 | new (Context) DependentCoawaitExpr(Loc, Context.DependentTy, E, Lookup); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 558 | return Res; |
| 559 | } |
| 560 | |
| 561 | auto *RD = Promise->getType()->getAsCXXRecordDecl(); |
| 562 | if (lookupMember(*this, "await_transform", RD, Loc)) { |
| 563 | ExprResult R = buildPromiseCall(*this, Promise, Loc, "await_transform", E); |
| 564 | if (R.isInvalid()) { |
| 565 | Diag(Loc, |
| 566 | diag::note_coroutine_promise_implicit_await_transform_required_here) |
| 567 | << E->getSourceRange(); |
| 568 | return ExprError(); |
| 569 | } |
| 570 | E = R.get(); |
| 571 | } |
| 572 | ExprResult Awaitable = buildOperatorCoawaitCall(*this, Loc, E, Lookup); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 573 | if (Awaitable.isInvalid()) |
| 574 | return ExprError(); |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 575 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 576 | return BuildResolvedCoawaitExpr(Loc, Awaitable.get()); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 577 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 578 | |
| 579 | ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *E, |
| 580 | bool IsImplicit) { |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 581 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 582 | if (!Coroutine) |
| 583 | return ExprError(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 584 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 585 | if (E->getType()->isPlaceholderType()) { |
| 586 | ExprResult R = CheckPlaceholderExpr(E); |
| 587 | if (R.isInvalid()) return ExprError(); |
| 588 | E = R.get(); |
| 589 | } |
| 590 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 591 | if (E->getType()->isDependentType()) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 592 | Expr *Res = new (Context) |
| 593 | CoawaitExpr(Loc, Context.DependentTy, E, IsImplicit); |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 594 | return Res; |
| 595 | } |
| 596 | |
Richard Smith | 1f38edd | 2015-11-22 03:13:02 +0000 | [diff] [blame] | 597 | // If the expression is a temporary, materialize it as an lvalue so that we |
| 598 | // can use it multiple times. |
| 599 | if (E->getValueKind() == VK_RValue) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 600 | E = CreateMaterializeTemporaryExpr(E->getType(), E, true); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 601 | |
| 602 | // Build the await_ready, await_suspend, await_resume calls. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 603 | ReadySuspendResumeResult RSS = |
| 604 | buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 605 | if (RSS.IsInvalid) |
| 606 | return ExprError(); |
| 607 | |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 608 | Expr *Res = |
| 609 | new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1], |
| 610 | RSS.Results[2], RSS.OpaqueValue, IsImplicit); |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 611 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 612 | return Res; |
| 613 | } |
| 614 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 615 | ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 616 | if (!actOnCoroutineBodyStart(*this, S, Loc, "co_yield")) { |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 617 | CorrectDelayedTyposInExpr(E); |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 618 | return ExprError(); |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 619 | } |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 620 | |
| 621 | // Build yield_value call. |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 622 | ExprResult Awaitable = buildPromiseCall( |
| 623 | *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 624 | if (Awaitable.isInvalid()) |
| 625 | return ExprError(); |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame] | 626 | |
| 627 | // Build 'operator co_await' call. |
| 628 | Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get()); |
| 629 | if (Awaitable.isInvalid()) |
| 630 | return ExprError(); |
| 631 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 632 | return BuildCoyieldExpr(Loc, Awaitable.get()); |
| 633 | } |
| 634 | ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) { |
| 635 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield"); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 636 | if (!Coroutine) |
| 637 | return ExprError(); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 638 | |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 639 | if (E->getType()->isPlaceholderType()) { |
| 640 | ExprResult R = CheckPlaceholderExpr(E); |
| 641 | if (R.isInvalid()) return ExprError(); |
| 642 | E = R.get(); |
| 643 | } |
| 644 | |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 645 | if (E->getType()->isDependentType()) { |
| 646 | Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, E); |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 647 | return Res; |
| 648 | } |
| 649 | |
Richard Smith | 1f38edd | 2015-11-22 03:13:02 +0000 | [diff] [blame] | 650 | // If the expression is a temporary, materialize it as an lvalue so that we |
| 651 | // can use it multiple times. |
| 652 | if (E->getValueKind() == VK_RValue) |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 653 | E = CreateMaterializeTemporaryExpr(E->getType(), E, true); |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 654 | |
| 655 | // Build the await_ready, await_suspend, await_resume calls. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 656 | ReadySuspendResumeResult RSS = |
| 657 | buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E); |
Richard Smith | d7bed4d | 2015-11-22 02:57:17 +0000 | [diff] [blame] | 658 | if (RSS.IsInvalid) |
| 659 | return ExprError(); |
| 660 | |
| 661 | Expr *Res = new (Context) CoyieldExpr(Loc, E, RSS.Results[0], RSS.Results[1], |
Gor Nishanov | ce43bd2 | 2017-03-11 01:30:17 +0000 | [diff] [blame] | 662 | RSS.Results[2], RSS.OpaqueValue); |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 663 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 664 | return Res; |
| 665 | } |
| 666 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 667 | StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) { |
| 668 | if (!actOnCoroutineBodyStart(*this, S, Loc, "co_return")) { |
Eric Fiselier | a546528 | 2016-09-29 21:47:39 +0000 | [diff] [blame] | 669 | CorrectDelayedTyposInExpr(E); |
| 670 | return StmtError(); |
| 671 | } |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 672 | return BuildCoreturnStmt(Loc, E); |
| 673 | } |
Gor Nishanov | 3e048bb | 2016-10-04 00:31:16 +0000 | [diff] [blame] | 674 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 675 | StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E, |
| 676 | bool IsImplicit) { |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 677 | auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 678 | if (!FSI) |
Richard Smith | 71d403e | 2015-11-22 07:33:28 +0000 | [diff] [blame] | 679 | return StmtError(); |
| 680 | |
| 681 | if (E && E->getType()->isPlaceholderType() && |
| 682 | !E->getType()->isSpecificPlaceholderType(BuiltinType::Overload)) { |
Richard Smith | 10610f7 | 2015-11-20 22:57:24 +0000 | [diff] [blame] | 683 | ExprResult R = CheckPlaceholderExpr(E); |
| 684 | if (R.isInvalid()) return StmtError(); |
| 685 | E = R.get(); |
| 686 | } |
| 687 | |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 688 | // 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] | 689 | // 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] | 690 | // resolution. |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 691 | VarDecl *Promise = FSI->CoroutinePromise; |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 692 | ExprResult PC; |
Eric Fiselier | 9813131 | 2016-10-06 21:23:38 +0000 | [diff] [blame] | 693 | if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 694 | PC = buildPromiseCall(*this, Promise, Loc, "return_value", E); |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 695 | } else { |
| 696 | E = MakeFullDiscardedValueExpr(E).get(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 697 | PC = buildPromiseCall(*this, Promise, Loc, "return_void", None); |
Richard Smith | 4ba6660 | 2015-11-22 07:05:16 +0000 | [diff] [blame] | 698 | } |
| 699 | if (PC.isInvalid()) |
| 700 | return StmtError(); |
| 701 | |
| 702 | Expr *PCE = ActOnFinishFullExpr(PC.get()).get(); |
| 703 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 704 | Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 705 | return Res; |
| 706 | } |
| 707 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 708 | /// Look up the std::nothrow object. |
| 709 | static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) { |
| 710 | NamespaceDecl *Std = S.getStdNamespace(); |
| 711 | assert(Std && "Should already be diagnosed"); |
| 712 | |
| 713 | LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc, |
| 714 | Sema::LookupOrdinaryName); |
| 715 | if (!S.LookupQualifiedName(Result, Std)) { |
| 716 | // FIXME: <experimental/coroutine> should have been included already. |
| 717 | // If we require it to include <new> then this diagnostic is no longer |
| 718 | // needed. |
| 719 | S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found); |
| 720 | return nullptr; |
| 721 | } |
| 722 | |
| 723 | // FIXME: Mark the variable as ODR used. This currently does not work |
| 724 | // likely due to the scope at in which this function is called. |
| 725 | auto *VD = Result.getAsSingle<VarDecl>(); |
| 726 | if (!VD) { |
| 727 | Result.suppressDiagnostics(); |
| 728 | // We found something weird. Complain about the first thing we found. |
| 729 | NamedDecl *Found = *Result.begin(); |
| 730 | S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow); |
| 731 | return nullptr; |
| 732 | } |
| 733 | |
| 734 | ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc); |
| 735 | if (DR.isInvalid()) |
| 736 | return nullptr; |
| 737 | |
| 738 | return DR.get(); |
| 739 | } |
| 740 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 741 | // Find an appropriate delete for the promise. |
| 742 | static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc, |
| 743 | QualType PromiseType) { |
| 744 | FunctionDecl *OperatorDelete = nullptr; |
| 745 | |
| 746 | DeclarationName DeleteName = |
| 747 | S.Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
| 748 | |
| 749 | auto *PointeeRD = PromiseType->getAsCXXRecordDecl(); |
| 750 | assert(PointeeRD && "PromiseType must be a CxxRecordDecl type"); |
| 751 | |
| 752 | if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete)) |
| 753 | return nullptr; |
| 754 | |
| 755 | if (!OperatorDelete) { |
| 756 | // Look for a global declaration. |
| 757 | const bool CanProvideSize = S.isCompleteType(Loc, PromiseType); |
| 758 | const bool Overaligned = false; |
| 759 | OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize, |
| 760 | Overaligned, DeleteName); |
| 761 | } |
| 762 | S.MarkFunctionReferenced(Loc, OperatorDelete); |
| 763 | return OperatorDelete; |
| 764 | } |
| 765 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 766 | |
| 767 | void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) { |
| 768 | FunctionScopeInfo *Fn = getCurFunction(); |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 769 | assert(Fn && Fn->isCoroutine() && "not a coroutine"); |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 770 | if (!Body) { |
| 771 | assert(FD->isInvalidDecl() && |
| 772 | "a null body is only allowed for invalid declarations"); |
| 773 | return; |
| 774 | } |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 775 | // We have a function that uses coroutine keywords, but we failed to build |
| 776 | // the promise type. |
| 777 | if (!Fn->CoroutinePromise) |
| 778 | return FD->setInvalidDecl(); |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 779 | |
| 780 | if (isa<CoroutineBodyStmt>(Body)) { |
Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 781 | // Nothing todo. the body is already a transformed coroutine body statement. |
Gor Nishanov | 6dcb0eb | 2017-03-09 03:09:43 +0000 | [diff] [blame] | 782 | return; |
| 783 | } |
| 784 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 785 | // Coroutines [stmt.return]p1: |
| 786 | // A return statement shall not appear in a coroutine. |
| 787 | if (Fn->FirstReturnLoc.isValid()) { |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 788 | assert(Fn->FirstCoroutineStmtLoc.isValid() && |
| 789 | "first coroutine location not set"); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 790 | Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine); |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 791 | Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 792 | << Fn->getFirstCoroutineStmtKeyword(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 793 | } |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 794 | CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body); |
| 795 | if (Builder.isInvalid() || !Builder.buildStatements()) |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 796 | return FD->setInvalidDecl(); |
| 797 | |
| 798 | // Build body for the coroutine wrapper statement. |
| 799 | Body = CoroutineBodyStmt::Create(Context, Builder); |
| 800 | } |
| 801 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 802 | CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD, |
| 803 | sema::FunctionScopeInfo &Fn, |
| 804 | Stmt *Body) |
| 805 | : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()), |
| 806 | IsPromiseDependentType( |
| 807 | !Fn.CoroutinePromise || |
| 808 | Fn.CoroutinePromise->getType()->isDependentType()) { |
| 809 | this->Body = Body; |
| 810 | if (!IsPromiseDependentType) { |
| 811 | PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl(); |
| 812 | assert(PromiseRecordDecl && "Type should have already been checked"); |
| 813 | } |
| 814 | this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend(); |
| 815 | } |
| 816 | |
| 817 | bool CoroutineStmtBuilder::buildStatements() { |
| 818 | assert(this->IsValid && "coroutine already invalid"); |
| 819 | this->IsValid = makeReturnObject() && makeParamMoves(); |
| 820 | if (this->IsValid && !IsPromiseDependentType) |
| 821 | buildDependentStatements(); |
| 822 | return this->IsValid; |
| 823 | } |
| 824 | |
| 825 | bool CoroutineStmtBuilder::buildDependentStatements() { |
| 826 | assert(this->IsValid && "coroutine already invalid"); |
| 827 | assert(!this->IsPromiseDependentType && |
| 828 | "coroutine cannot have a dependent promise type"); |
| 829 | this->IsValid = makeOnException() && makeOnFallthrough() && |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 830 | makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() && |
| 831 | makeNewAndDeleteExpr(); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 832 | return this->IsValid; |
| 833 | } |
| 834 | |
| 835 | bool CoroutineStmtBuilder::makePromiseStmt() { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 836 | // Form a declaration statement for the promise declaration, so that AST |
| 837 | // visitors can more easily find it. |
| 838 | StmtResult PromiseStmt = |
| 839 | S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc); |
| 840 | if (PromiseStmt.isInvalid()) |
| 841 | return false; |
| 842 | |
| 843 | this->Promise = PromiseStmt.get(); |
| 844 | return true; |
| 845 | } |
| 846 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 847 | bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 848 | if (Fn.hasInvalidCoroutineSuspends()) |
| 849 | return false; |
| 850 | this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first); |
| 851 | this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second); |
| 852 | return true; |
| 853 | } |
| 854 | |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 855 | static bool diagReturnOnAllocFailure(Sema &S, Expr *E, |
| 856 | CXXRecordDecl *PromiseRecordDecl, |
| 857 | FunctionScopeInfo &Fn) { |
| 858 | auto Loc = E->getExprLoc(); |
| 859 | if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) { |
| 860 | auto *Decl = DeclRef->getDecl(); |
| 861 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) { |
| 862 | if (Method->isStatic()) |
| 863 | return true; |
| 864 | else |
| 865 | Loc = Decl->getLocation(); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | S.Diag( |
| 870 | Loc, |
| 871 | diag::err_coroutine_promise_get_return_object_on_allocation_failure) |
| 872 | << PromiseRecordDecl; |
| 873 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 874 | << Fn.getFirstCoroutineStmtKeyword(); |
| 875 | return false; |
| 876 | } |
| 877 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 878 | bool CoroutineStmtBuilder::makeReturnOnAllocFailure() { |
| 879 | assert(!IsPromiseDependentType && |
| 880 | "cannot make statement while the promise type is dependent"); |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 881 | |
| 882 | // [dcl.fct.def.coroutine]/8 |
| 883 | // The unqualified-id get_return_object_on_allocation_failure is looked up in |
| 884 | // the scope of class P by class member access lookup (3.4.5). ... |
| 885 | // If an allocation function returns nullptr, ... the coroutine return value |
| 886 | // is obtained by a call to ... get_return_object_on_allocation_failure(). |
| 887 | |
| 888 | DeclarationName DN = |
| 889 | S.PP.getIdentifierInfo("get_return_object_on_allocation_failure"); |
| 890 | LookupResult Found(S, DN, Loc, Sema::LookupMemberName); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 891 | if (!S.LookupQualifiedName(Found, PromiseRecordDecl)) |
| 892 | return true; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 893 | |
| 894 | CXXScopeSpec SS; |
| 895 | ExprResult DeclNameExpr = |
| 896 | S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 897 | if (DeclNameExpr.isInvalid()) |
| 898 | return false; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 899 | |
| 900 | if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn)) |
| 901 | return false; |
| 902 | |
| 903 | ExprResult ReturnObjectOnAllocationFailure = |
| 904 | S.ActOnCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 905 | if (ReturnObjectOnAllocationFailure.isInvalid()) |
| 906 | return false; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 907 | |
Gor Nishanov | c4a1908 | 2017-03-28 02:51:45 +0000 | [diff] [blame] | 908 | StmtResult ReturnStmt = |
| 909 | S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get()); |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 910 | if (ReturnStmt.isInvalid()) { |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 911 | S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here) |
| 912 | << DN; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 913 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 914 | << Fn.getFirstCoroutineStmtKeyword(); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 915 | return false; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 916 | } |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 917 | |
| 918 | this->ReturnStmtOnAllocFailure = ReturnStmt.get(); |
| 919 | return true; |
| 920 | } |
| 921 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 922 | bool CoroutineStmtBuilder::makeNewAndDeleteExpr() { |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 923 | // Form and check allocation and deallocation calls. |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 924 | assert(!IsPromiseDependentType && |
| 925 | "cannot make statement while the promise type is dependent"); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 926 | QualType PromiseType = Fn.CoroutinePromise->getType(); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 927 | |
| 928 | if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type)) |
| 929 | return false; |
| 930 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 931 | const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr; |
| 932 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 933 | // FIXME: Add support for stateful allocators. |
| 934 | |
| 935 | FunctionDecl *OperatorNew = nullptr; |
| 936 | FunctionDecl *OperatorDelete = nullptr; |
| 937 | FunctionDecl *UnusedResult = nullptr; |
| 938 | bool PassAlignment = false; |
Eric Fiselier | f747f53 | 2017-04-18 05:08:08 +0000 | [diff] [blame] | 939 | SmallVector<Expr *, 1> PlacementArgs; |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 940 | |
| 941 | S.FindAllocationFunctions(Loc, SourceRange(), |
| 942 | /*UseGlobal*/ false, PromiseType, |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 943 | /*isArray*/ false, PassAlignment, PlacementArgs, |
| 944 | OperatorNew, UnusedResult); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 945 | |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 946 | bool IsGlobalOverload = |
| 947 | OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext()); |
| 948 | // If we didn't find a class-local new declaration and non-throwing new |
| 949 | // was is required then we need to lookup the non-throwing global operator |
| 950 | // instead. |
| 951 | if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) { |
| 952 | auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc); |
| 953 | if (!StdNoThrow) |
| 954 | return false; |
Eric Fiselier | f747f53 | 2017-04-18 05:08:08 +0000 | [diff] [blame] | 955 | PlacementArgs = {StdNoThrow}; |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 956 | OperatorNew = nullptr; |
| 957 | S.FindAllocationFunctions(Loc, SourceRange(), |
| 958 | /*UseGlobal*/ true, PromiseType, |
| 959 | /*isArray*/ false, PassAlignment, PlacementArgs, |
| 960 | OperatorNew, UnusedResult); |
| 961 | } |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 962 | |
Eric Fiselier | c512875 | 2017-04-18 05:30:39 +0000 | [diff] [blame] | 963 | assert(OperatorNew && "expected definition of operator new to be found"); |
| 964 | |
| 965 | if (RequiresNoThrowAlloc) { |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 966 | const auto *FT = OperatorNew->getType()->getAs<FunctionProtoType>(); |
| 967 | if (!FT->isNothrow(S.Context, /*ResultIfDependent*/ false)) { |
| 968 | S.Diag(OperatorNew->getLocation(), |
| 969 | diag::err_coroutine_promise_new_requires_nothrow) |
| 970 | << OperatorNew; |
| 971 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
| 972 | << OperatorNew; |
| 973 | return false; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr) |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 978 | return false; |
| 979 | |
| 980 | Expr *FramePtr = |
| 981 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {}); |
| 982 | |
| 983 | Expr *FrameSize = |
| 984 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_size, {}); |
| 985 | |
| 986 | // Make new call. |
| 987 | |
| 988 | ExprResult NewRef = |
| 989 | S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc); |
| 990 | if (NewRef.isInvalid()) |
| 991 | return false; |
| 992 | |
Eric Fiselier | f747f53 | 2017-04-18 05:08:08 +0000 | [diff] [blame] | 993 | SmallVector<Expr *, 2> NewArgs(1, FrameSize); |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 994 | for (auto Arg : PlacementArgs) |
| 995 | NewArgs.push_back(Arg); |
| 996 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 997 | ExprResult NewExpr = |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 998 | S.ActOnCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc); |
| 999 | NewExpr = S.ActOnFinishFullExpr(NewExpr.get()); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1000 | if (NewExpr.isInvalid()) |
| 1001 | return false; |
| 1002 | |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1003 | // Make delete call. |
| 1004 | |
| 1005 | QualType OpDeleteQualType = OperatorDelete->getType(); |
| 1006 | |
| 1007 | ExprResult DeleteRef = |
| 1008 | S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc); |
| 1009 | if (DeleteRef.isInvalid()) |
| 1010 | return false; |
| 1011 | |
| 1012 | Expr *CoroFree = |
| 1013 | buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_free, {FramePtr}); |
| 1014 | |
| 1015 | SmallVector<Expr *, 2> DeleteArgs{CoroFree}; |
| 1016 | |
| 1017 | // Check if we need to pass the size. |
| 1018 | const auto *OpDeleteType = |
| 1019 | OpDeleteQualType.getTypePtr()->getAs<FunctionProtoType>(); |
| 1020 | if (OpDeleteType->getNumParams() > 1) |
| 1021 | DeleteArgs.push_back(FrameSize); |
| 1022 | |
| 1023 | ExprResult DeleteExpr = |
| 1024 | S.ActOnCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc); |
Eric Fiselier | f692e7d | 2017-04-18 03:12:48 +0000 | [diff] [blame] | 1025 | DeleteExpr = S.ActOnFinishFullExpr(DeleteExpr.get()); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1026 | if (DeleteExpr.isInvalid()) |
| 1027 | return false; |
| 1028 | |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1029 | this->Allocate = NewExpr.get(); |
| 1030 | this->Deallocate = DeleteExpr.get(); |
Gor Nishanov | 8df64e9 | 2016-10-27 16:28:31 +0000 | [diff] [blame] | 1031 | |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1035 | bool CoroutineStmtBuilder::makeOnFallthrough() { |
| 1036 | assert(!IsPromiseDependentType && |
| 1037 | "cannot make statement while the promise type is dependent"); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1038 | |
| 1039 | // [dcl.fct.def.coroutine]/4 |
| 1040 | // The unqualified-ids 'return_void' and 'return_value' are looked up in |
| 1041 | // 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] | 1042 | bool HasRVoid, HasRValue; |
| 1043 | LookupResult LRVoid = |
| 1044 | lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid); |
| 1045 | LookupResult LRValue = |
| 1046 | lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1047 | |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 1048 | StmtResult Fallthrough; |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1049 | if (HasRVoid && HasRValue) { |
| 1050 | // FIXME Improve this diagnostic |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 1051 | S.Diag(FD.getLocation(), |
| 1052 | diag::err_coroutine_promise_incompatible_return_functions) |
| 1053 | << PromiseRecordDecl; |
| 1054 | S.Diag(LRVoid.getRepresentativeDecl()->getLocation(), |
| 1055 | diag::note_member_first_declared_here) |
| 1056 | << LRVoid.getLookupName(); |
| 1057 | S.Diag(LRValue.getRepresentativeDecl()->getLocation(), |
| 1058 | diag::note_member_first_declared_here) |
| 1059 | << LRValue.getLookupName(); |
| 1060 | return false; |
| 1061 | } else if (!HasRVoid && !HasRValue) { |
| 1062 | // FIXME: The PDTS currently specifies this case as UB, not ill-formed. |
| 1063 | // However we still diagnose this as an error since until the PDTS is fixed. |
| 1064 | S.Diag(FD.getLocation(), |
| 1065 | diag::err_coroutine_promise_requires_return_function) |
| 1066 | << PromiseRecordDecl; |
| 1067 | S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here) |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1068 | << PromiseRecordDecl; |
| 1069 | return false; |
| 1070 | } else if (HasRVoid) { |
| 1071 | // If the unqualified-id return_void is found, flowing off the end of a |
| 1072 | // coroutine is equivalent to a co_return with no operand. Otherwise, |
| 1073 | // flowing off the end of a coroutine results in undefined behavior. |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1074 | Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr, |
| 1075 | /*IsImplicit*/false); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1076 | Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get()); |
| 1077 | if (Fallthrough.isInvalid()) |
| 1078 | return false; |
Eric Fiselier | 709d1b3 | 2016-10-27 07:30:31 +0000 | [diff] [blame] | 1079 | } |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1080 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1081 | this->OnFallthrough = Fallthrough.get(); |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1085 | bool CoroutineStmtBuilder::makeOnException() { |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1086 | // Try to form 'p.unhandled_exception();' |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1087 | assert(!IsPromiseDependentType && |
| 1088 | "cannot make statement while the promise type is dependent"); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1089 | |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1090 | const bool RequireUnhandledException = S.getLangOpts().CXXExceptions; |
| 1091 | |
| 1092 | if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) { |
| 1093 | auto DiagID = |
| 1094 | RequireUnhandledException |
| 1095 | ? diag::err_coroutine_promise_unhandled_exception_required |
| 1096 | : diag:: |
| 1097 | warn_coroutine_promise_unhandled_exception_required_with_exceptions; |
| 1098 | S.Diag(Loc, DiagID) << PromiseRecordDecl; |
Gor Nishanov | 29ff638 | 2017-05-24 14:34:19 +0000 | [diff] [blame] | 1099 | S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here) |
| 1100 | << PromiseRecordDecl; |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1101 | return !RequireUnhandledException; |
| 1102 | } |
| 1103 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1104 | // If exceptions are disabled, don't try to build OnException. |
| 1105 | if (!S.getLangOpts().CXXExceptions) |
| 1106 | return true; |
| 1107 | |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1108 | ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc, |
| 1109 | "unhandled_exception", None); |
| 1110 | UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc); |
| 1111 | if (UnhandledException.isInvalid()) |
| 1112 | return false; |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1113 | |
Gor Nishanov | 5b050e4 | 2017-05-22 22:33:17 +0000 | [diff] [blame] | 1114 | // Since the body of the coroutine will be wrapped in try-catch, it will |
| 1115 | // be incompatible with SEH __try if present in a function. |
| 1116 | if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) { |
| 1117 | S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions); |
| 1118 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 1119 | << Fn.getFirstCoroutineStmtKeyword(); |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
Eric Fiselier | a9fdb34 | 2017-03-23 00:33:33 +0000 | [diff] [blame] | 1123 | this->OnException = UnhandledException.get(); |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1124 | return true; |
| 1125 | } |
| 1126 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1127 | bool CoroutineStmtBuilder::makeReturnObject() { |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1128 | // Build implicit 'p.get_return_object()' expression and form initialization |
| 1129 | // of return type from it. |
| 1130 | ExprResult ReturnObject = |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1131 | buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None); |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1132 | if (ReturnObject.isInvalid()) |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1133 | return false; |
Richard Smith | 2af65c4 | 2015-11-24 02:34:39 +0000 | [diff] [blame] | 1134 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1135 | this->ReturnValue = ReturnObject.get(); |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1139 | static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) { |
| 1140 | if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) { |
| 1141 | auto *MethodDecl = MbrRef->getMethodDecl(); |
Eric Fiselier | fc50f62 | 2017-05-25 14:59:39 +0000 | [diff] [blame] | 1142 | S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here) |
| 1143 | << MethodDecl; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 1144 | } |
| 1145 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
| 1146 | << Fn.getFirstCoroutineStmtKeyword(); |
| 1147 | } |
| 1148 | |
| 1149 | bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() { |
| 1150 | assert(!IsPromiseDependentType && |
| 1151 | "cannot make statement while the promise type is dependent"); |
| 1152 | assert(this->ReturnValue && "ReturnValue must be already formed"); |
| 1153 | |
| 1154 | QualType const GroType = this->ReturnValue->getType(); |
| 1155 | assert(!GroType->isDependentType() && |
| 1156 | "get_return_object type must no longer be dependent"); |
| 1157 | |
| 1158 | QualType const FnRetType = FD.getReturnType(); |
| 1159 | assert(!FnRetType->isDependentType() && |
| 1160 | "get_return_object type must no longer be dependent"); |
| 1161 | |
| 1162 | if (FnRetType->isVoidType()) { |
| 1163 | ExprResult Res = S.ActOnFinishFullExpr(this->ReturnValue, Loc); |
| 1164 | if (Res.isInvalid()) |
| 1165 | return false; |
| 1166 | |
| 1167 | this->ResultDecl = Res.get(); |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
| 1171 | if (GroType->isVoidType()) { |
| 1172 | // Trigger a nice error message. |
| 1173 | InitializedEntity Entity = |
| 1174 | InitializedEntity::InitializeResult(Loc, FnRetType, false); |
| 1175 | S.PerformMoveOrCopyInitialization(Entity, nullptr, FnRetType, ReturnValue); |
| 1176 | noteMemberDeclaredHere(S, ReturnValue, Fn); |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | auto *GroDecl = VarDecl::Create( |
| 1181 | S.Context, &FD, FD.getLocation(), FD.getLocation(), |
| 1182 | &S.PP.getIdentifierTable().get("__coro_gro"), GroType, |
| 1183 | S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None); |
| 1184 | |
| 1185 | S.CheckVariableDeclarationType(GroDecl); |
| 1186 | if (GroDecl->isInvalidDecl()) |
| 1187 | return false; |
| 1188 | |
| 1189 | InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl); |
| 1190 | ExprResult Res = S.PerformMoveOrCopyInitialization(Entity, nullptr, GroType, |
| 1191 | this->ReturnValue); |
| 1192 | if (Res.isInvalid()) |
| 1193 | return false; |
| 1194 | |
| 1195 | Res = S.ActOnFinishFullExpr(Res.get()); |
| 1196 | if (Res.isInvalid()) |
| 1197 | return false; |
| 1198 | |
| 1199 | if (GroType == FnRetType) { |
| 1200 | GroDecl->setNRVOVariable(true); |
| 1201 | } |
| 1202 | |
| 1203 | S.AddInitializerToDecl(GroDecl, Res.get(), |
| 1204 | /*DirectInit=*/false); |
| 1205 | |
| 1206 | S.FinalizeDeclaration(GroDecl); |
| 1207 | |
| 1208 | // Form a declaration statement for the return declaration, so that AST |
| 1209 | // visitors can more easily find it. |
| 1210 | StmtResult GroDeclStmt = |
| 1211 | S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(GroDecl), Loc, Loc); |
| 1212 | if (GroDeclStmt.isInvalid()) |
| 1213 | return false; |
| 1214 | |
| 1215 | this->ResultDecl = GroDeclStmt.get(); |
| 1216 | |
| 1217 | ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc); |
| 1218 | if (declRef.isInvalid()) |
| 1219 | return false; |
| 1220 | |
| 1221 | StmtResult ReturnStmt = S.BuildReturnStmt(Loc, declRef.get()); |
| 1222 | if (ReturnStmt.isInvalid()) { |
| 1223 | noteMemberDeclaredHere(S, ReturnValue, Fn); |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | this->ReturnStmt = ReturnStmt.get(); |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1231 | // Create a static_cast\<T&&>(expr). |
| 1232 | static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) { |
| 1233 | if (T.isNull()) |
| 1234 | T = E->getType(); |
| 1235 | QualType TargetType = S.BuildReferenceType( |
| 1236 | T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName()); |
| 1237 | SourceLocation ExprLoc = E->getLocStart(); |
| 1238 | TypeSourceInfo *TargetLoc = |
| 1239 | S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc); |
| 1240 | |
| 1241 | return S |
| 1242 | .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
| 1243 | SourceRange(ExprLoc, ExprLoc), E->getSourceRange()) |
| 1244 | .get(); |
| 1245 | } |
| 1246 | |
| 1247 | /// \brief Build a variable declaration for move parameter. |
| 1248 | static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type, |
| 1249 | StringRef Name) { |
| 1250 | DeclContext *DC = S.CurContext; |
| 1251 | IdentifierInfo *II = &S.PP.getIdentifierTable().get(Name); |
| 1252 | TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc); |
| 1253 | VarDecl *Decl = |
| 1254 | VarDecl::Create(S.Context, DC, Loc, Loc, II, Type, TInfo, SC_None); |
| 1255 | Decl->setImplicit(); |
| 1256 | return Decl; |
| 1257 | } |
| 1258 | |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 1259 | bool CoroutineStmtBuilder::makeParamMoves() { |
Gor Nishanov | 33d5fd2 | 2017-05-24 20:09:14 +0000 | [diff] [blame] | 1260 | for (auto *paramDecl : FD.parameters()) { |
| 1261 | auto Ty = paramDecl->getType(); |
| 1262 | if (Ty->isDependentType()) |
| 1263 | continue; |
| 1264 | |
| 1265 | // No need to copy scalars, llvm will take care of them. |
| 1266 | if (Ty->getAsCXXRecordDecl()) { |
| 1267 | if (!paramDecl->getIdentifier()) |
| 1268 | continue; |
| 1269 | |
| 1270 | ExprResult ParamRef = |
| 1271 | S.BuildDeclRefExpr(paramDecl, paramDecl->getType(), |
| 1272 | ExprValueKind::VK_LValue, Loc); // FIXME: scope? |
| 1273 | if (ParamRef.isInvalid()) |
| 1274 | return false; |
| 1275 | |
| 1276 | Expr *RCast = castForMoving(S, ParamRef.get()); |
| 1277 | |
| 1278 | auto D = buildVarDecl(S, Loc, Ty, paramDecl->getIdentifier()->getName()); |
| 1279 | |
| 1280 | S.AddInitializerToDecl(D, RCast, /*DirectInit=*/true); |
| 1281 | |
| 1282 | // Convert decl to a statement. |
| 1283 | StmtResult Stmt = S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(D), Loc, Loc); |
| 1284 | if (Stmt.isInvalid()) |
| 1285 | return false; |
| 1286 | |
| 1287 | ParamMovesVector.push_back(Stmt.get()); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | // Convert to ArrayRef in CtorArgs structure that builder inherits from. |
| 1292 | ParamMoves = ParamMovesVector; |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 1293 | return true; |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 1294 | } |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 1295 | |
| 1296 | StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) { |
| 1297 | CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args); |
| 1298 | if (!Res) |
| 1299 | return StmtError(); |
| 1300 | return Res; |
| 1301 | } |