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