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 | |
| 14 | #include "clang/Sema/SemaInternal.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" |
| 19 | #include "clang/Sema/Overload.h" |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace sema; |
| 22 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 23 | /// Look up the std::coroutine_traits<...>::promise_type for the given |
| 24 | /// function type. |
| 25 | static QualType lookupPromiseType(Sema &S, const FunctionProtoType *FnType, |
| 26 | SourceLocation Loc) { |
| 27 | // FIXME: Cache std::coroutine_traits once we've found it. |
| 28 | NamespaceDecl *Std = S.getStdNamespace(); |
| 29 | if (!Std) { |
| 30 | S.Diag(Loc, diag::err_implied_std_coroutine_traits_not_found); |
| 31 | return QualType(); |
| 32 | } |
| 33 | |
| 34 | LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_traits"), |
| 35 | Loc, Sema::LookupOrdinaryName); |
| 36 | if (!S.LookupQualifiedName(Result, Std)) { |
| 37 | S.Diag(Loc, diag::err_implied_std_coroutine_traits_not_found); |
| 38 | return QualType(); |
| 39 | } |
| 40 | |
| 41 | ClassTemplateDecl *CoroTraits = Result.getAsSingle<ClassTemplateDecl>(); |
| 42 | if (!CoroTraits) { |
| 43 | Result.suppressDiagnostics(); |
| 44 | // We found something weird. Complain about the first thing we found. |
| 45 | NamedDecl *Found = *Result.begin(); |
| 46 | S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits); |
| 47 | return QualType(); |
| 48 | } |
| 49 | |
| 50 | // Form template argument list for coroutine_traits<R, P1, P2, ...>. |
| 51 | TemplateArgumentListInfo Args(Loc, Loc); |
| 52 | Args.addArgument(TemplateArgumentLoc( |
| 53 | TemplateArgument(FnType->getReturnType()), |
| 54 | S.Context.getTrivialTypeSourceInfo(FnType->getReturnType(), Loc))); |
| 55 | for (QualType T : FnType->getParamTypes()) |
| 56 | Args.addArgument(TemplateArgumentLoc( |
| 57 | TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, Loc))); |
| 58 | |
| 59 | // Build the template-id. |
| 60 | QualType CoroTrait = |
| 61 | S.CheckTemplateIdType(TemplateName(CoroTraits), Loc, Args); |
| 62 | if (CoroTrait.isNull()) |
| 63 | return QualType(); |
| 64 | if (S.RequireCompleteType(Loc, CoroTrait, |
| 65 | diag::err_coroutine_traits_missing_specialization)) |
| 66 | return QualType(); |
| 67 | |
| 68 | CXXRecordDecl *RD = CoroTrait->getAsCXXRecordDecl(); |
| 69 | assert(RD && "specialization of class template is not a class?"); |
| 70 | |
| 71 | // Look up the ::promise_type member. |
| 72 | LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), Loc, |
| 73 | Sema::LookupOrdinaryName); |
| 74 | S.LookupQualifiedName(R, RD); |
| 75 | auto *Promise = R.getAsSingle<TypeDecl>(); |
| 76 | if (!Promise) { |
| 77 | S.Diag(Loc, diag::err_implied_std_coroutine_traits_promise_type_not_found) |
| 78 | << RD; |
| 79 | return QualType(); |
| 80 | } |
| 81 | |
| 82 | // The promise type is required to be a class type. |
| 83 | QualType PromiseType = S.Context.getTypeDeclType(Promise); |
| 84 | if (!PromiseType->getAsCXXRecordDecl()) { |
Richard Smith | 9b2f53e | 2015-11-19 02:36:35 +0000 | [diff] [blame] | 85 | // Use the fully-qualified name of the type. |
| 86 | auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, Std); |
| 87 | NNS = NestedNameSpecifier::Create(S.Context, NNS, false, |
| 88 | CoroTrait.getTypePtr()); |
| 89 | PromiseType = S.Context.getElaboratedType(ETK_None, NNS, PromiseType); |
| 90 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 91 | S.Diag(Loc, diag::err_implied_std_coroutine_traits_promise_type_not_class) |
| 92 | << PromiseType; |
| 93 | return QualType(); |
| 94 | } |
| 95 | |
| 96 | return PromiseType; |
| 97 | } |
| 98 | |
| 99 | /// Check that this is a context in which a coroutine suspension can appear. |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 100 | static FunctionScopeInfo * |
| 101 | checkCoroutineContext(Sema &S, SourceLocation Loc, StringRef Keyword) { |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 102 | // 'co_await' and 'co_yield' are not permitted in unevaluated operands. |
| 103 | if (S.isUnevaluatedContext()) { |
| 104 | S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword; |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 105 | return nullptr; |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 106 | } |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 107 | |
| 108 | // Any other usage must be within a function. |
| 109 | auto *FD = dyn_cast<FunctionDecl>(S.CurContext); |
| 110 | if (!FD) { |
| 111 | S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext) |
| 112 | ? diag::err_coroutine_objc_method |
| 113 | : diag::err_coroutine_outside_function) << Keyword; |
| 114 | } else if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD)) { |
| 115 | // Coroutines TS [special]/6: |
| 116 | // A special member function shall not be a coroutine. |
| 117 | // |
| 118 | // FIXME: We assume that this really means that a coroutine cannot |
| 119 | // be a constructor or destructor. |
| 120 | S.Diag(Loc, diag::err_coroutine_ctor_dtor) |
| 121 | << isa<CXXDestructorDecl>(FD) << Keyword; |
| 122 | } else if (FD->isConstexpr()) { |
| 123 | S.Diag(Loc, diag::err_coroutine_constexpr) << Keyword; |
| 124 | } else if (FD->isVariadic()) { |
| 125 | S.Diag(Loc, diag::err_coroutine_varargs) << Keyword; |
| 126 | } else { |
| 127 | auto *ScopeInfo = S.getCurFunction(); |
| 128 | assert(ScopeInfo && "missing function scope for function"); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 129 | |
| 130 | // If we don't have a promise variable, build one now. |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 131 | if (!ScopeInfo->CoroutinePromise) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 132 | QualType T = |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 133 | FD->getType()->isDependentType() |
| 134 | ? S.Context.DependentTy |
| 135 | : lookupPromiseType(S, FD->getType()->castAs<FunctionProtoType>(), |
| 136 | Loc); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 137 | if (T.isNull()) |
| 138 | return nullptr; |
| 139 | |
| 140 | // Create and default-initialize the promise. |
| 141 | ScopeInfo->CoroutinePromise = |
| 142 | VarDecl::Create(S.Context, FD, FD->getLocation(), FD->getLocation(), |
| 143 | &S.PP.getIdentifierTable().get("__promise"), T, |
| 144 | S.Context.getTrivialTypeSourceInfo(T, Loc), SC_None); |
| 145 | S.CheckVariableDeclarationType(ScopeInfo->CoroutinePromise); |
| 146 | if (!ScopeInfo->CoroutinePromise->isInvalidDecl()) |
| 147 | S.ActOnUninitializedDecl(ScopeInfo->CoroutinePromise, false); |
| 148 | } |
| 149 | |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 150 | return ScopeInfo; |
| 151 | } |
| 152 | |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 156 | /// Build a call to 'operator co_await' if there is a suitable operator for |
| 157 | /// the given expression. |
| 158 | static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S, |
| 159 | SourceLocation Loc, Expr *E) { |
| 160 | UnresolvedSet<16> Functions; |
| 161 | SemaRef.LookupOverloadedOperatorName(OO_Coawait, S, E->getType(), QualType(), |
| 162 | Functions); |
| 163 | return SemaRef.CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E); |
| 164 | } |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 165 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 166 | struct ReadySuspendResumeResult { |
| 167 | bool IsInvalid; |
| 168 | Expr *Results[3]; |
| 169 | }; |
| 170 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 171 | static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc, |
| 172 | StringRef Name, |
| 173 | MutableArrayRef<Expr *> Args) { |
| 174 | DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc); |
| 175 | |
| 176 | // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&. |
| 177 | CXXScopeSpec SS; |
| 178 | ExprResult Result = S.BuildMemberReferenceExpr( |
| 179 | Base, Base->getType(), Loc, /*IsPtr=*/false, SS, |
| 180 | SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr, |
| 181 | /*Scope=*/nullptr); |
| 182 | if (Result.isInvalid()) |
| 183 | return ExprError(); |
| 184 | |
| 185 | return S.ActOnCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr); |
| 186 | } |
| 187 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 188 | /// Build calls to await_ready, await_suspend, and await_resume for a co_await |
| 189 | /// expression. |
| 190 | static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, SourceLocation Loc, |
| 191 | Expr *E) { |
| 192 | // Assume invalid until we see otherwise. |
| 193 | ReadySuspendResumeResult Calls = {true, {}}; |
| 194 | |
| 195 | const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"}; |
| 196 | for (size_t I = 0, N = llvm::array_lengthof(Funcs); I != N; ++I) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 197 | Expr *Operand = new (S.Context) OpaqueValueExpr( |
| 198 | Loc, E->getType(), E->getValueKind(), E->getObjectKind(), E); |
| 199 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 200 | // FIXME: Pass coroutine handle to await_suspend. |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 201 | ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], None); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 202 | if (Result.isInvalid()) |
| 203 | return Calls; |
| 204 | Calls.Results[I] = Result.get(); |
| 205 | } |
| 206 | |
| 207 | Calls.IsInvalid = false; |
| 208 | return Calls; |
| 209 | } |
| 210 | |
| 211 | ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) { |
| 212 | ExprResult Awaitable = buildOperatorCoawaitCall(*this, S, Loc, E); |
| 213 | if (Awaitable.isInvalid()) |
| 214 | return ExprError(); |
| 215 | return BuildCoawaitExpr(Loc, Awaitable.get()); |
| 216 | } |
| 217 | ExprResult Sema::BuildCoawaitExpr(SourceLocation Loc, Expr *E) { |
| 218 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await"); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 219 | if (!Coroutine) |
| 220 | return ExprError(); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 221 | |
| 222 | if (E->getType()->isDependentType()) { |
| 223 | Expr *Res = new (Context) CoawaitExpr(Loc, Context.DependentTy, E); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 224 | Coroutine->CoroutineStmts.push_back(Res); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 225 | return Res; |
| 226 | } |
| 227 | |
| 228 | if (E->getType()->isPlaceholderType()) { |
| 229 | ExprResult R = CheckPlaceholderExpr(E); |
| 230 | if (R.isInvalid()) return ExprError(); |
| 231 | E = R.get(); |
| 232 | } |
| 233 | |
| 234 | // FIXME: If E is a prvalue, create a temporary. |
| 235 | // FIXME: If E is an xvalue, convert to lvalue. |
| 236 | |
| 237 | // Build the await_ready, await_suspend, await_resume calls. |
| 238 | ReadySuspendResumeResult RSS = buildCoawaitCalls(*this, Loc, E); |
| 239 | if (RSS.IsInvalid) |
| 240 | return ExprError(); |
| 241 | |
| 242 | Expr *Res = new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1], |
| 243 | RSS.Results[2]); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 244 | Coroutine->CoroutineStmts.push_back(Res); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 245 | return Res; |
| 246 | } |
| 247 | |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 248 | static ExprResult buildYieldValueCall(Sema &S, FunctionScopeInfo *Coroutine, |
| 249 | SourceLocation Loc, Expr *E) { |
| 250 | assert(Coroutine->CoroutinePromise && "no promise for coroutine"); |
| 251 | |
| 252 | // Form a reference to the promise. |
| 253 | auto *Promise = Coroutine->CoroutinePromise; |
| 254 | ExprResult PromiseRef = S.BuildDeclRefExpr( |
| 255 | Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc); |
| 256 | if (PromiseRef.isInvalid()) |
| 257 | return ExprError(); |
| 258 | |
| 259 | // Call 'yield_value', passing in E. |
| 260 | return buildMemberCall(S, PromiseRef.get(), Loc, "yield_value", E); |
| 261 | } |
| 262 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 263 | ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) { |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 264 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield"); |
| 265 | if (!Coroutine) |
| 266 | return ExprError(); |
| 267 | |
| 268 | // Build yield_value call. |
| 269 | ExprResult Awaitable = buildYieldValueCall(*this, Coroutine, Loc, E); |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 270 | if (Awaitable.isInvalid()) |
| 271 | return ExprError(); |
Richard Smith | 23da82c | 2015-11-20 22:40:06 +0000 | [diff] [blame^] | 272 | |
| 273 | // Build 'operator co_await' call. |
| 274 | Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get()); |
| 275 | if (Awaitable.isInvalid()) |
| 276 | return ExprError(); |
| 277 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 278 | return BuildCoyieldExpr(Loc, Awaitable.get()); |
| 279 | } |
| 280 | ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) { |
| 281 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield"); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 282 | if (!Coroutine) |
| 283 | return ExprError(); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 284 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 285 | // FIXME: Build await_* calls. |
| 286 | Expr *Res = new (Context) CoyieldExpr(Loc, Context.VoidTy, E); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 287 | Coroutine->CoroutineStmts.push_back(Res); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 288 | return Res; |
| 289 | } |
| 290 | |
| 291 | StmtResult Sema::ActOnCoreturnStmt(SourceLocation Loc, Expr *E) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 292 | return BuildCoreturnStmt(Loc, E); |
| 293 | } |
| 294 | StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E) { |
| 295 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_return"); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 296 | if (!Coroutine) |
| 297 | return StmtError(); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 298 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 299 | // FIXME: Build return_* calls. |
| 300 | Stmt *Res = new (Context) CoreturnStmt(Loc, E); |
Richard Smith | 744b224 | 2015-11-20 02:54:01 +0000 | [diff] [blame] | 301 | Coroutine->CoroutineStmts.push_back(Res); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 302 | return Res; |
| 303 | } |
| 304 | |
| 305 | void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *Body) { |
| 306 | FunctionScopeInfo *Fn = getCurFunction(); |
| 307 | assert(Fn && !Fn->CoroutineStmts.empty() && "not a coroutine"); |
| 308 | |
| 309 | // Coroutines [stmt.return]p1: |
| 310 | // A return statement shall not appear in a coroutine. |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 311 | if (Fn->FirstReturnLoc.isValid()) { |
| 312 | Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 313 | auto *First = Fn->CoroutineStmts[0]; |
| 314 | Diag(First->getLocStart(), diag::note_declared_coroutine_here) |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 315 | << (isa<CoawaitExpr>(First) ? 0 : |
| 316 | isa<CoyieldExpr>(First) ? 1 : 2); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | bool AnyCoawaits = false; |
| 320 | bool AnyCoyields = false; |
| 321 | for (auto *CoroutineStmt : Fn->CoroutineStmts) { |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 322 | AnyCoawaits |= isa<CoawaitExpr>(CoroutineStmt); |
| 323 | AnyCoyields |= isa<CoyieldExpr>(CoroutineStmt); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if (!AnyCoawaits && !AnyCoyields) |
| 327 | Diag(Fn->CoroutineStmts.front()->getLocStart(), |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 328 | diag::ext_coroutine_without_co_await_co_yield); |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 329 | |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 330 | // FIXME: Perform analysis of initial and final suspend, |
| 331 | // and set_exception call. |
Richard Smith | cfd53b4 | 2015-10-22 06:13:50 +0000 | [diff] [blame] | 332 | } |