blob: 471fab90e8042e3cc1af91fdf89c46e96a67fa18 [file] [log] [blame]
Richard Smithcfd53b42015-10-22 06:13:50 +00001//===--- 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 Smith9f690bd2015-10-27 06:02:45 +000015#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 Smithcfd53b42015-10-22 06:13:50 +000020using namespace clang;
21using namespace sema;
22
Richard Smith9f690bd2015-10-27 06:02:45 +000023/// Look up the std::coroutine_traits<...>::promise_type for the given
24/// function type.
25static 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 Smith9b2f53e2015-11-19 02:36:35 +000085 // 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 Smith9f690bd2015-10-27 06:02:45 +000091 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 Smithcfd53b42015-10-22 06:13:50 +0000100static FunctionScopeInfo *
101checkCoroutineContext(Sema &S, SourceLocation Loc, StringRef Keyword) {
Richard Smith744b2242015-11-20 02:54:01 +0000102 // '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 Smithcfd53b42015-10-22 06:13:50 +0000105 return nullptr;
Richard Smith744b2242015-11-20 02:54:01 +0000106 }
Richard Smithcfd53b42015-10-22 06:13:50 +0000107
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 Smith9f690bd2015-10-27 06:02:45 +0000129
130 // If we don't have a promise variable, build one now.
Richard Smith23da82c2015-11-20 22:40:06 +0000131 if (!ScopeInfo->CoroutinePromise) {
Richard Smith9f690bd2015-10-27 06:02:45 +0000132 QualType T =
Richard Smith23da82c2015-11-20 22:40:06 +0000133 FD->getType()->isDependentType()
134 ? S.Context.DependentTy
135 : lookupPromiseType(S, FD->getType()->castAs<FunctionProtoType>(),
136 Loc);
Richard Smith9f690bd2015-10-27 06:02:45 +0000137 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 Smithcfd53b42015-10-22 06:13:50 +0000150 return ScopeInfo;
151 }
152
153 return nullptr;
154}
155
Richard Smith9f690bd2015-10-27 06:02:45 +0000156/// Build a call to 'operator co_await' if there is a suitable operator for
157/// the given expression.
158static 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 Smithcfd53b42015-10-22 06:13:50 +0000165
Richard Smith9f690bd2015-10-27 06:02:45 +0000166struct ReadySuspendResumeResult {
167 bool IsInvalid;
168 Expr *Results[3];
169};
170
Richard Smith23da82c2015-11-20 22:40:06 +0000171static 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 Smith9f690bd2015-10-27 06:02:45 +0000188/// Build calls to await_ready, await_suspend, and await_resume for a co_await
189/// expression.
190static 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 Smith9f690bd2015-10-27 06:02:45 +0000197 Expr *Operand = new (S.Context) OpaqueValueExpr(
198 Loc, E->getType(), E->getValueKind(), E->getObjectKind(), E);
199
Richard Smith9f690bd2015-10-27 06:02:45 +0000200 // FIXME: Pass coroutine handle to await_suspend.
Richard Smith23da82c2015-11-20 22:40:06 +0000201 ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], None);
Richard Smith9f690bd2015-10-27 06:02:45 +0000202 if (Result.isInvalid())
203 return Calls;
204 Calls.Results[I] = Result.get();
205 }
206
207 Calls.IsInvalid = false;
208 return Calls;
209}
210
211ExprResult 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}
217ExprResult Sema::BuildCoawaitExpr(SourceLocation Loc, Expr *E) {
218 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await");
Richard Smith744b2242015-11-20 02:54:01 +0000219 if (!Coroutine)
220 return ExprError();
Richard Smith9f690bd2015-10-27 06:02:45 +0000221
222 if (E->getType()->isDependentType()) {
223 Expr *Res = new (Context) CoawaitExpr(Loc, Context.DependentTy, E);
Richard Smith744b2242015-11-20 02:54:01 +0000224 Coroutine->CoroutineStmts.push_back(Res);
Richard Smith9f690bd2015-10-27 06:02:45 +0000225 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 Smith744b2242015-11-20 02:54:01 +0000244 Coroutine->CoroutineStmts.push_back(Res);
Richard Smithcfd53b42015-10-22 06:13:50 +0000245 return Res;
246}
247
Richard Smith23da82c2015-11-20 22:40:06 +0000248static 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 Smith9f690bd2015-10-27 06:02:45 +0000263ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) {
Richard Smith23da82c2015-11-20 22:40:06 +0000264 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 Smith9f690bd2015-10-27 06:02:45 +0000270 if (Awaitable.isInvalid())
271 return ExprError();
Richard Smith23da82c2015-11-20 22:40:06 +0000272
273 // Build 'operator co_await' call.
274 Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get());
275 if (Awaitable.isInvalid())
276 return ExprError();
277
Richard Smith9f690bd2015-10-27 06:02:45 +0000278 return BuildCoyieldExpr(Loc, Awaitable.get());
279}
280ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) {
281 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield");
Richard Smith744b2242015-11-20 02:54:01 +0000282 if (!Coroutine)
283 return ExprError();
Richard Smithcfd53b42015-10-22 06:13:50 +0000284
Richard Smith9f690bd2015-10-27 06:02:45 +0000285 // FIXME: Build await_* calls.
286 Expr *Res = new (Context) CoyieldExpr(Loc, Context.VoidTy, E);
Richard Smith744b2242015-11-20 02:54:01 +0000287 Coroutine->CoroutineStmts.push_back(Res);
Richard Smithcfd53b42015-10-22 06:13:50 +0000288 return Res;
289}
290
291StmtResult Sema::ActOnCoreturnStmt(SourceLocation Loc, Expr *E) {
Richard Smith9f690bd2015-10-27 06:02:45 +0000292 return BuildCoreturnStmt(Loc, E);
293}
294StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E) {
295 auto *Coroutine = checkCoroutineContext(*this, Loc, "co_return");
Richard Smith744b2242015-11-20 02:54:01 +0000296 if (!Coroutine)
297 return StmtError();
Richard Smithcfd53b42015-10-22 06:13:50 +0000298
Richard Smith9f690bd2015-10-27 06:02:45 +0000299 // FIXME: Build return_* calls.
300 Stmt *Res = new (Context) CoreturnStmt(Loc, E);
Richard Smith744b2242015-11-20 02:54:01 +0000301 Coroutine->CoroutineStmts.push_back(Res);
Richard Smithcfd53b42015-10-22 06:13:50 +0000302 return Res;
303}
304
305void 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 Smith9f690bd2015-10-27 06:02:45 +0000311 if (Fn->FirstReturnLoc.isValid()) {
312 Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine);
Richard Smithcfd53b42015-10-22 06:13:50 +0000313 auto *First = Fn->CoroutineStmts[0];
314 Diag(First->getLocStart(), diag::note_declared_coroutine_here)
Richard Smith9f690bd2015-10-27 06:02:45 +0000315 << (isa<CoawaitExpr>(First) ? 0 :
316 isa<CoyieldExpr>(First) ? 1 : 2);
Richard Smithcfd53b42015-10-22 06:13:50 +0000317 }
318
319 bool AnyCoawaits = false;
320 bool AnyCoyields = false;
321 for (auto *CoroutineStmt : Fn->CoroutineStmts) {
Richard Smith9f690bd2015-10-27 06:02:45 +0000322 AnyCoawaits |= isa<CoawaitExpr>(CoroutineStmt);
323 AnyCoyields |= isa<CoyieldExpr>(CoroutineStmt);
Richard Smithcfd53b42015-10-22 06:13:50 +0000324 }
325
326 if (!AnyCoawaits && !AnyCoyields)
327 Diag(Fn->CoroutineStmts.front()->getLocStart(),
Richard Smith9f690bd2015-10-27 06:02:45 +0000328 diag::ext_coroutine_without_co_await_co_yield);
Richard Smithcfd53b42015-10-22 06:13:50 +0000329
Richard Smith9f690bd2015-10-27 06:02:45 +0000330 // FIXME: Perform analysis of initial and final suspend,
331 // and set_exception call.
Richard Smithcfd53b42015-10-22 06:13:50 +0000332}