/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaCoroutine.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- SemaCoroutine.cpp - Semantic Analysis for Coroutines --------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for C++ Coroutines. |
10 | | // |
11 | | // This file contains references to sections of the Coroutines TS, which |
12 | | // can be found at http://wg21.link/coroutines. |
13 | | // |
14 | | //===----------------------------------------------------------------------===// |
15 | | |
16 | | #include "CoroutineStmtBuilder.h" |
17 | | #include "clang/AST/ASTLambda.h" |
18 | | #include "clang/AST/Decl.h" |
19 | | #include "clang/AST/ExprCXX.h" |
20 | | #include "clang/AST/StmtCXX.h" |
21 | | #include "clang/Basic/Builtins.h" |
22 | | #include "clang/Lex/Preprocessor.h" |
23 | | #include "clang/Sema/Initialization.h" |
24 | | #include "clang/Sema/Overload.h" |
25 | | #include "clang/Sema/ScopeInfo.h" |
26 | | #include "clang/Sema/SemaInternal.h" |
27 | | #include "llvm/ADT/SmallSet.h" |
28 | | |
29 | | using namespace clang; |
30 | | using namespace sema; |
31 | | |
32 | | static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, |
33 | 2.67k | SourceLocation Loc, bool &Res) { |
34 | 2.67k | DeclarationName DN = S.PP.getIdentifierInfo(Name); |
35 | 2.67k | LookupResult LR(S, DN, Loc, Sema::LookupMemberName); |
36 | | // Suppress diagnostics when a private member is selected. The same warnings |
37 | | // will be produced again when building the call. |
38 | 2.67k | LR.suppressDiagnostics(); |
39 | 2.67k | Res = S.LookupQualifiedName(LR, RD); |
40 | 2.67k | return LR; |
41 | 2.67k | } |
42 | | |
43 | | static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD, |
44 | 1.19k | SourceLocation Loc) { |
45 | 1.19k | bool Res; |
46 | 1.19k | lookupMember(S, Name, RD, Loc, Res); |
47 | 1.19k | return Res; |
48 | 1.19k | } |
49 | | |
50 | | /// Look up the std::coroutine_traits<...>::promise_type for the given |
51 | | /// function type. |
52 | | static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD, |
53 | 929 | SourceLocation KwLoc) { |
54 | 929 | const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>(); |
55 | 929 | const SourceLocation FuncLoc = FD->getLocation(); |
56 | | |
57 | 929 | NamespaceDecl *CoroNamespace = nullptr; |
58 | 929 | ClassTemplateDecl *CoroTraits = |
59 | 929 | S.lookupCoroutineTraits(KwLoc, FuncLoc, CoroNamespace); |
60 | 929 | if (!CoroTraits) { |
61 | 23 | return QualType(); |
62 | 23 | } |
63 | | |
64 | | // Form template argument list for coroutine_traits<R, P1, P2, ...> according |
65 | | // to [dcl.fct.def.coroutine]3 |
66 | 906 | TemplateArgumentListInfo Args(KwLoc, KwLoc); |
67 | 1.78k | auto AddArg = [&](QualType T) { |
68 | 1.78k | Args.addArgument(TemplateArgumentLoc( |
69 | 1.78k | TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc))); |
70 | 1.78k | }; |
71 | 906 | AddArg(FnType->getReturnType()); |
72 | | // If the function is a non-static member function, add the type |
73 | | // of the implicit object parameter before the formal parameters. |
74 | 906 | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
75 | 291 | if (MD->isInstance()) { |
76 | | // [over.match.funcs]4 |
77 | | // For non-static member functions, the type of the implicit object |
78 | | // parameter is |
79 | | // -- "lvalue reference to cv X" for functions declared without a |
80 | | // ref-qualifier or with the & ref-qualifier |
81 | | // -- "rvalue reference to cv X" for functions declared with the && |
82 | | // ref-qualifier |
83 | 227 | QualType T = MD->getThisType()->castAs<PointerType>()->getPointeeType(); |
84 | 227 | T = FnType->getRefQualifier() == RQ_RValue |
85 | 227 | ? S.Context.getRValueReferenceType(T)37 |
86 | 227 | : S.Context.getLValueReferenceType(T, /*SpelledAsLValue*/ true)190 ; |
87 | 227 | AddArg(T); |
88 | 227 | } |
89 | 291 | } |
90 | 906 | for (QualType T : FnType->getParamTypes()) |
91 | 647 | AddArg(T); |
92 | | |
93 | | // Build the template-id. |
94 | 906 | QualType CoroTrait = |
95 | 906 | S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args); |
96 | 906 | if (CoroTrait.isNull()) |
97 | 0 | return QualType(); |
98 | 906 | if (S.RequireCompleteType(KwLoc, CoroTrait, |
99 | 906 | diag::err_coroutine_type_missing_specialization)) |
100 | 2 | return QualType(); |
101 | | |
102 | 904 | auto *RD = CoroTrait->getAsCXXRecordDecl(); |
103 | 904 | assert(RD && "specialization of class template is not a class?"); |
104 | | |
105 | | // Look up the ::promise_type member. |
106 | 0 | LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc, |
107 | 904 | Sema::LookupOrdinaryName); |
108 | 904 | S.LookupQualifiedName(R, RD); |
109 | 904 | auto *Promise = R.getAsSingle<TypeDecl>(); |
110 | 904 | if (!Promise) { |
111 | 30 | S.Diag(FuncLoc, |
112 | 30 | diag::err_implied_std_coroutine_traits_promise_type_not_found) |
113 | 30 | << RD; |
114 | 30 | return QualType(); |
115 | 30 | } |
116 | | // The promise type is required to be a class type. |
117 | 874 | QualType PromiseType = S.Context.getTypeDeclType(Promise); |
118 | | |
119 | 874 | auto buildElaboratedType = [&]() { |
120 | 874 | auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, CoroNamespace); |
121 | 874 | NNS = NestedNameSpecifier::Create(S.Context, NNS, false, |
122 | 874 | CoroTrait.getTypePtr()); |
123 | 874 | return S.Context.getElaboratedType(ETK_None, NNS, PromiseType); |
124 | 874 | }; |
125 | | |
126 | 874 | if (!PromiseType->getAsCXXRecordDecl()) { |
127 | 5 | S.Diag(FuncLoc, |
128 | 5 | diag::err_implied_std_coroutine_traits_promise_type_not_class) |
129 | 5 | << buildElaboratedType(); |
130 | 5 | return QualType(); |
131 | 5 | } |
132 | 869 | if (S.RequireCompleteType(FuncLoc, buildElaboratedType(), |
133 | 869 | diag::err_coroutine_promise_type_incomplete)) |
134 | 5 | return QualType(); |
135 | | |
136 | 864 | return PromiseType; |
137 | 869 | } |
138 | | |
139 | | /// Look up the std::coroutine_handle<PromiseType>. |
140 | | static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType, |
141 | 2.29k | SourceLocation Loc) { |
142 | 2.29k | if (PromiseType.isNull()) |
143 | 0 | return QualType(); |
144 | | |
145 | 2.29k | NamespaceDecl *CoroNamespace = S.getCachedCoroNamespace(); |
146 | 2.29k | assert(CoroNamespace && "Should already be diagnosed"); |
147 | | |
148 | 0 | LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"), |
149 | 2.29k | Loc, Sema::LookupOrdinaryName); |
150 | 2.29k | if (!S.LookupQualifiedName(Result, CoroNamespace)) { |
151 | 5 | S.Diag(Loc, diag::err_implied_coroutine_type_not_found) |
152 | 5 | << "std::coroutine_handle"; |
153 | 5 | return QualType(); |
154 | 5 | } |
155 | | |
156 | 2.28k | ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>(); |
157 | 2.28k | if (!CoroHandle) { |
158 | 0 | Result.suppressDiagnostics(); |
159 | | // We found something weird. Complain about the first thing we found. |
160 | 0 | NamedDecl *Found = *Result.begin(); |
161 | 0 | S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle); |
162 | 0 | return QualType(); |
163 | 0 | } |
164 | | |
165 | | // Form template argument list for coroutine_handle<Promise>. |
166 | 2.28k | TemplateArgumentListInfo Args(Loc, Loc); |
167 | 2.28k | Args.addArgument(TemplateArgumentLoc( |
168 | 2.28k | TemplateArgument(PromiseType), |
169 | 2.28k | S.Context.getTrivialTypeSourceInfo(PromiseType, Loc))); |
170 | | |
171 | | // Build the template-id. |
172 | 2.28k | QualType CoroHandleType = |
173 | 2.28k | S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args); |
174 | 2.28k | if (CoroHandleType.isNull()) |
175 | 0 | return QualType(); |
176 | 2.28k | if (S.RequireCompleteType(Loc, CoroHandleType, |
177 | 2.28k | diag::err_coroutine_type_missing_specialization)) |
178 | 0 | return QualType(); |
179 | | |
180 | 2.28k | return CoroHandleType; |
181 | 2.28k | } |
182 | | |
183 | | static bool isValidCoroutineContext(Sema &S, SourceLocation Loc, |
184 | 5.54k | StringRef Keyword) { |
185 | | // [expr.await]p2 dictates that 'co_await' and 'co_yield' must be used within |
186 | | // a function body. |
187 | | // FIXME: This also covers [expr.await]p2: "An await-expression shall not |
188 | | // appear in a default argument." But the diagnostic QoI here could be |
189 | | // improved to inform the user that default arguments specifically are not |
190 | | // allowed. |
191 | 5.54k | auto *FD = dyn_cast<FunctionDecl>(S.CurContext); |
192 | 5.54k | if (!FD) { |
193 | 5 | S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext) |
194 | 5 | ? diag::err_coroutine_objc_method0 |
195 | 5 | : diag::err_coroutine_outside_function) << Keyword; |
196 | 5 | return false; |
197 | 5 | } |
198 | | |
199 | | // An enumeration for mapping the diagnostic type to the correct diagnostic |
200 | | // selection index. |
201 | 5.53k | enum InvalidFuncDiag { |
202 | 5.53k | DiagCtor = 0, |
203 | 5.53k | DiagDtor, |
204 | 5.53k | DiagMain, |
205 | 5.53k | DiagConstexpr, |
206 | 5.53k | DiagAutoRet, |
207 | 5.53k | DiagVarargs, |
208 | 5.53k | DiagConsteval, |
209 | 5.53k | }; |
210 | 5.53k | bool Diagnosed = false; |
211 | 5.53k | auto DiagInvalid = [&](InvalidFuncDiag ID) { |
212 | 42 | S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword; |
213 | 42 | Diagnosed = true; |
214 | 42 | return false; |
215 | 42 | }; |
216 | | |
217 | | // Diagnose when a constructor, destructor |
218 | | // or the function 'main' are declared as a coroutine. |
219 | 5.53k | auto *MD = dyn_cast<CXXMethodDecl>(FD); |
220 | | // [class.ctor]p11: "A constructor shall not be a coroutine." |
221 | 5.53k | if (MD && isa<CXXConstructorDecl>(MD)1.76k ) |
222 | 10 | return DiagInvalid(DiagCtor); |
223 | | // [class.dtor]p17: "A destructor shall not be a coroutine." |
224 | 5.52k | else if (MD && isa<CXXDestructorDecl>(MD)1.75k ) |
225 | 5 | return DiagInvalid(DiagDtor); |
226 | | // [basic.start.main]p3: "The function main shall not be a coroutine." |
227 | 5.52k | else if (FD->isMain()) |
228 | 5 | return DiagInvalid(DiagMain); |
229 | | |
230 | | // Emit a diagnostics for each of the following conditions which is not met. |
231 | | // [expr.const]p2: "An expression e is a core constant expression unless the |
232 | | // evaluation of e [...] would evaluate one of the following expressions: |
233 | | // [...] an await-expression [...] a yield-expression." |
234 | 5.51k | if (FD->isConstexpr()) |
235 | 7 | DiagInvalid(FD->isConsteval() ? DiagConsteval0 : DiagConstexpr); |
236 | | // [dcl.spec.auto]p15: "A function declared with a return type that uses a |
237 | | // placeholder type shall not be a coroutine." |
238 | 5.51k | if (FD->getReturnType()->isUndeducedType()) |
239 | 10 | DiagInvalid(DiagAutoRet); |
240 | | // [dcl.fct.def.coroutine]p1 |
241 | | // The parameter-declaration-clause of the coroutine shall not terminate with |
242 | | // an ellipsis that is not part of a parameter-declaration. |
243 | 5.51k | if (FD->isVariadic()) |
244 | 5 | DiagInvalid(DiagVarargs); |
245 | | |
246 | 5.51k | return !Diagnosed; |
247 | 5.53k | } |
248 | | |
249 | | /// Build a call to 'operator co_await' if there is a suitable operator for |
250 | | /// the given expression. |
251 | | ExprResult Sema::BuildOperatorCoawaitCall(SourceLocation Loc, Expr *E, |
252 | 2.84k | UnresolvedLookupExpr *Lookup) { |
253 | 2.84k | UnresolvedSet<16> Functions; |
254 | 2.84k | Functions.append(Lookup->decls_begin(), Lookup->decls_end()); |
255 | 2.84k | return CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E); |
256 | 2.84k | } |
257 | | |
258 | | static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S, |
259 | 1.91k | SourceLocation Loc, Expr *E) { |
260 | 1.91k | ExprResult R = SemaRef.BuildOperatorCoawaitLookupExpr(S, Loc); |
261 | 1.91k | if (R.isInvalid()) |
262 | 0 | return ExprError(); |
263 | 1.91k | return SemaRef.BuildOperatorCoawaitCall(Loc, E, |
264 | 1.91k | cast<UnresolvedLookupExpr>(R.get())); |
265 | 1.91k | } |
266 | | |
267 | | static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType, |
268 | 2.29k | SourceLocation Loc) { |
269 | 2.29k | QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc); |
270 | 2.29k | if (CoroHandleType.isNull()) |
271 | 5 | return ExprError(); |
272 | | |
273 | 2.28k | DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType); |
274 | 2.28k | LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc, |
275 | 2.28k | Sema::LookupOrdinaryName); |
276 | 2.28k | if (!S.LookupQualifiedName(Found, LookupCtx)) { |
277 | 5 | S.Diag(Loc, diag::err_coroutine_handle_missing_member) |
278 | 5 | << "from_address"; |
279 | 5 | return ExprError(); |
280 | 5 | } |
281 | | |
282 | 2.28k | Expr *FramePtr = |
283 | 2.28k | S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_frame, {}); |
284 | | |
285 | 2.28k | CXXScopeSpec SS; |
286 | 2.28k | ExprResult FromAddr = |
287 | 2.28k | S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); |
288 | 2.28k | if (FromAddr.isInvalid()) |
289 | 0 | return ExprError(); |
290 | | |
291 | 2.28k | return S.BuildCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc); |
292 | 2.28k | } |
293 | | |
294 | | struct ReadySuspendResumeResult { |
295 | | enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume }; |
296 | | Expr *Results[3]; |
297 | | OpaqueValueExpr *OpaqueValue; |
298 | | bool IsInvalid; |
299 | | }; |
300 | | |
301 | | static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc, |
302 | 11.2k | StringRef Name, MultiExprArg Args) { |
303 | 11.2k | DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc); |
304 | | |
305 | | // FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&. |
306 | 11.2k | CXXScopeSpec SS; |
307 | 11.2k | ExprResult Result = S.BuildMemberReferenceExpr( |
308 | 11.2k | Base, Base->getType(), Loc, /*IsPtr=*/false, SS, |
309 | 11.2k | SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr, |
310 | 11.2k | /*Scope=*/nullptr); |
311 | 11.2k | if (Result.isInvalid()) |
312 | 91 | return ExprError(); |
313 | | |
314 | | // We meant exactly what we asked for. No need for typo correction. |
315 | 11.1k | if (auto *TE = dyn_cast<TypoExpr>(Result.get())) { |
316 | 15 | S.clearDelayedTypo(TE); |
317 | 15 | S.Diag(Loc, diag::err_no_member) |
318 | 15 | << NameInfo.getName() << Base->getType()->getAsCXXRecordDecl() |
319 | 15 | << Base->getSourceRange(); |
320 | 15 | return ExprError(); |
321 | 15 | } |
322 | | |
323 | 11.1k | return S.BuildCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr); |
324 | 11.1k | } |
325 | | |
326 | | // See if return type is coroutine-handle and if so, invoke builtin coro-resume |
327 | | // on its address. This is to enable experimental support for coroutine-handle |
328 | | // returning await_suspend that results in a guaranteed tail call to the target |
329 | | // coroutine. |
330 | | static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E, |
331 | 2.27k | SourceLocation Loc) { |
332 | 2.27k | if (RetType->isReferenceType()) |
333 | 10 | return nullptr; |
334 | 2.26k | Type const *T = RetType.getTypePtr(); |
335 | 2.26k | if (!T->isClassType() && !T->isStructureType()) |
336 | 2.23k | return nullptr; |
337 | | |
338 | | // FIXME: Add convertability check to coroutine_handle<>. Possibly via |
339 | | // EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment |
340 | | // a private function in SemaExprCXX.cpp |
341 | | |
342 | 31 | ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", None); |
343 | 31 | if (AddressExpr.isInvalid()) |
344 | 0 | return nullptr; |
345 | | |
346 | 31 | Expr *JustAddress = AddressExpr.get(); |
347 | | |
348 | | // Check that the type of AddressExpr is void* |
349 | 31 | if (!JustAddress->getType().getTypePtr()->isVoidPointerType()) |
350 | 2 | S.Diag(cast<CallExpr>(JustAddress)->getCalleeDecl()->getLocation(), |
351 | 2 | diag::warn_coroutine_handle_address_invalid_return_type) |
352 | 2 | << JustAddress->getType(); |
353 | | |
354 | | // Clean up temporary objects so that they don't live across suspension points |
355 | | // unnecessarily. We choose to clean up before the call to |
356 | | // __builtin_coro_resume so that the cleanup code are not inserted in-between |
357 | | // the resume call and return instruction, which would interfere with the |
358 | | // musttail call contract. |
359 | 31 | JustAddress = S.MaybeCreateExprWithCleanups(JustAddress); |
360 | 31 | return S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_resume, |
361 | 31 | JustAddress); |
362 | 31 | } |
363 | | |
364 | | /// Build calls to await_ready, await_suspend, and await_resume for a co_await |
365 | | /// expression. |
366 | | /// The generated AST tries to clean up temporary objects as early as |
367 | | /// possible so that they don't live across suspension points if possible. |
368 | | /// Having temporary objects living across suspension points unnecessarily can |
369 | | /// lead to large frame size, and also lead to memory corruptions if the |
370 | | /// coroutine frame is destroyed after coming back from suspension. This is done |
371 | | /// by wrapping both the await_ready call and the await_suspend call with |
372 | | /// ExprWithCleanups. In the end of this function, we also need to explicitly |
373 | | /// set cleanup state so that the CoawaitExpr is also wrapped with an |
374 | | /// ExprWithCleanups to clean up the awaiter associated with the co_await |
375 | | /// expression. |
376 | | static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise, |
377 | 2.35k | SourceLocation Loc, Expr *E) { |
378 | 2.35k | OpaqueValueExpr *Operand = new (S.Context) |
379 | 2.35k | OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E); |
380 | | |
381 | | // Assume valid until we see otherwise. |
382 | | // Further operations are responsible for setting IsInalid to true. |
383 | 2.35k | ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/false}; |
384 | | |
385 | 2.35k | using ACT = ReadySuspendResumeResult::AwaitCallType; |
386 | | |
387 | 2.35k | auto BuildSubExpr = [&](ACT CallType, StringRef Func, |
388 | 6.91k | MultiExprArg Arg) -> Expr * { |
389 | 6.91k | ExprResult Result = buildMemberCall(S, Operand, Loc, Func, Arg); |
390 | 6.91k | if (Result.isInvalid()) { |
391 | 67 | Calls.IsInvalid = true; |
392 | 67 | return nullptr; |
393 | 67 | } |
394 | 6.84k | Calls.Results[CallType] = Result.get(); |
395 | 6.84k | return Result.get(); |
396 | 6.91k | }; |
397 | | |
398 | 2.35k | CallExpr *AwaitReady = |
399 | 2.35k | cast_or_null<CallExpr>(BuildSubExpr(ACT::ACT_Ready, "await_ready", None)); |
400 | 2.35k | if (!AwaitReady) |
401 | 57 | return Calls; |
402 | 2.29k | if (!AwaitReady->getType()->isDependentType()) { |
403 | | // [expr.await]p3 [...] |
404 | | // — await-ready is the expression e.await_ready(), contextually converted |
405 | | // to bool. |
406 | 2.29k | ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady); |
407 | 2.29k | if (Conv.isInvalid()) { |
408 | 5 | S.Diag(AwaitReady->getDirectCallee()->getBeginLoc(), |
409 | 5 | diag::note_await_ready_no_bool_conversion); |
410 | 5 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
411 | 5 | << AwaitReady->getDirectCallee() << E->getSourceRange(); |
412 | 5 | Calls.IsInvalid = true; |
413 | 5 | } else |
414 | 2.28k | Calls.Results[ACT::ACT_Ready] = S.MaybeCreateExprWithCleanups(Conv.get()); |
415 | 2.29k | } |
416 | | |
417 | 2.29k | ExprResult CoroHandleRes = |
418 | 2.29k | buildCoroutineHandle(S, CoroPromise->getType(), Loc); |
419 | 2.29k | if (CoroHandleRes.isInvalid()) { |
420 | 10 | Calls.IsInvalid = true; |
421 | 10 | return Calls; |
422 | 10 | } |
423 | 2.28k | Expr *CoroHandle = CoroHandleRes.get(); |
424 | 2.28k | CallExpr *AwaitSuspend = cast_or_null<CallExpr>( |
425 | 2.28k | BuildSubExpr(ACT::ACT_Suspend, "await_suspend", CoroHandle)); |
426 | 2.28k | if (!AwaitSuspend) |
427 | 5 | return Calls; |
428 | 2.27k | if (!AwaitSuspend->getType()->isDependentType()) { |
429 | | // [expr.await]p3 [...] |
430 | | // - await-suspend is the expression e.await_suspend(h), which shall be |
431 | | // a prvalue of type void, bool, or std::coroutine_handle<Z> for some |
432 | | // type Z. |
433 | 2.27k | QualType RetType = AwaitSuspend->getCallReturnType(S.Context); |
434 | | |
435 | | // Experimental support for coroutine_handle returning await_suspend. |
436 | 2.27k | if (Expr *TailCallSuspend = |
437 | 2.27k | maybeTailCall(S, RetType, AwaitSuspend, Loc)) |
438 | | // Note that we don't wrap the expression with ExprWithCleanups here |
439 | | // because that might interfere with tailcall contract (e.g. inserting |
440 | | // clean up instructions in-between tailcall and return). Instead |
441 | | // ExprWithCleanups is wrapped within maybeTailCall() prior to the resume |
442 | | // call. |
443 | 31 | Calls.Results[ACT::ACT_Suspend] = TailCallSuspend; |
444 | 2.24k | else { |
445 | | // non-class prvalues always have cv-unqualified types |
446 | 2.24k | if (RetType->isReferenceType() || |
447 | 2.24k | (2.23k !RetType->isBooleanType()2.23k && !RetType->isVoidType()2.22k )) { |
448 | 15 | S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(), |
449 | 15 | diag::err_await_suspend_invalid_return_type) |
450 | 15 | << RetType; |
451 | 15 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
452 | 15 | << AwaitSuspend->getDirectCallee(); |
453 | 15 | Calls.IsInvalid = true; |
454 | 15 | } else |
455 | 2.23k | Calls.Results[ACT::ACT_Suspend] = |
456 | 2.23k | S.MaybeCreateExprWithCleanups(AwaitSuspend); |
457 | 2.24k | } |
458 | 2.27k | } |
459 | | |
460 | 2.27k | BuildSubExpr(ACT::ACT_Resume, "await_resume", None); |
461 | | |
462 | | // Make sure the awaiter object gets a chance to be cleaned up. |
463 | 2.27k | S.Cleanup.setExprNeedsCleanups(true); |
464 | | |
465 | 2.27k | return Calls; |
466 | 2.28k | } |
467 | | |
468 | | static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise, |
469 | | SourceLocation Loc, StringRef Name, |
470 | 4.28k | MultiExprArg Args) { |
471 | | |
472 | | // Form a reference to the promise. |
473 | 4.28k | ExprResult PromiseRef = S.BuildDeclRefExpr( |
474 | 4.28k | Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc); |
475 | 4.28k | if (PromiseRef.isInvalid()) |
476 | 0 | return ExprError(); |
477 | | |
478 | 4.28k | return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args); |
479 | 4.28k | } |
480 | | |
481 | 1.15k | VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) { |
482 | 1.15k | assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); |
483 | 0 | auto *FD = cast<FunctionDecl>(CurContext); |
484 | 1.15k | bool IsThisDependentType = [&] { |
485 | 1.15k | if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(FD)) |
486 | 408 | return MD->isInstance() && MD->getThisType()->isDependentType()344 ; |
487 | 749 | else |
488 | 749 | return false; |
489 | 1.15k | }(); |
490 | | |
491 | 1.15k | QualType T = FD->getType()->isDependentType() || IsThisDependentType991 |
492 | 1.15k | ? Context.DependentTy228 |
493 | 1.15k | : lookupPromiseType(*this, FD, Loc)929 ; |
494 | 1.15k | if (T.isNull()) |
495 | 65 | return nullptr; |
496 | | |
497 | 1.09k | auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(), |
498 | 1.09k | &PP.getIdentifierTable().get("__promise"), T, |
499 | 1.09k | Context.getTrivialTypeSourceInfo(T, Loc), SC_None); |
500 | 1.09k | VD->setImplicit(); |
501 | 1.09k | CheckVariableDeclarationType(VD); |
502 | 1.09k | if (VD->isInvalidDecl()) |
503 | 0 | return nullptr; |
504 | | |
505 | 1.09k | auto *ScopeInfo = getCurFunction(); |
506 | | |
507 | | // Build a list of arguments, based on the coroutine function's arguments, |
508 | | // that if present will be passed to the promise type's constructor. |
509 | 1.09k | llvm::SmallVector<Expr *, 4> CtorArgExprs; |
510 | | |
511 | | // Add implicit object parameter. |
512 | 1.09k | if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
513 | 393 | if (MD->isInstance() && !isLambdaCallOperator(MD)334 ) { |
514 | 270 | ExprResult ThisExpr = ActOnCXXThis(Loc); |
515 | 270 | if (ThisExpr.isInvalid()) |
516 | 0 | return nullptr; |
517 | 270 | ThisExpr = CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get()); |
518 | 270 | if (ThisExpr.isInvalid()) |
519 | 0 | return nullptr; |
520 | 270 | CtorArgExprs.push_back(ThisExpr.get()); |
521 | 270 | } |
522 | 393 | } |
523 | | |
524 | | // Add the coroutine function's parameters. |
525 | 1.09k | auto &Moves = ScopeInfo->CoroutineParameterMoves; |
526 | 1.09k | for (auto *PD : FD->parameters()) { |
527 | 859 | if (PD->getType()->isDependentType()) |
528 | 172 | continue; |
529 | | |
530 | 687 | auto RefExpr = ExprEmpty(); |
531 | 687 | auto Move = Moves.find(PD); |
532 | 687 | assert(Move != Moves.end() && |
533 | 687 | "Coroutine function parameter not inserted into move map"); |
534 | | // If a reference to the function parameter exists in the coroutine |
535 | | // frame, use that reference. |
536 | 0 | auto *MoveDecl = |
537 | 687 | cast<VarDecl>(cast<DeclStmt>(Move->second)->getSingleDecl()); |
538 | 687 | RefExpr = |
539 | 687 | BuildDeclRefExpr(MoveDecl, MoveDecl->getType().getNonReferenceType(), |
540 | 687 | ExprValueKind::VK_LValue, FD->getLocation()); |
541 | 687 | if (RefExpr.isInvalid()) |
542 | 0 | return nullptr; |
543 | 687 | CtorArgExprs.push_back(RefExpr.get()); |
544 | 687 | } |
545 | | |
546 | | // If we have a non-zero number of constructor arguments, try to use them. |
547 | | // Otherwise, fall back to the promise type's default constructor. |
548 | 1.09k | if (!CtorArgExprs.empty()) { |
549 | | // Create an initialization sequence for the promise type using the |
550 | | // constructor arguments, wrapped in a parenthesized list expression. |
551 | 596 | Expr *PLE = ParenListExpr::Create(Context, FD->getLocation(), |
552 | 596 | CtorArgExprs, FD->getLocation()); |
553 | 596 | InitializedEntity Entity = InitializedEntity::InitializeVariable(VD); |
554 | 596 | InitializationKind Kind = InitializationKind::CreateForInit( |
555 | 596 | VD->getLocation(), /*DirectInit=*/true, PLE); |
556 | 596 | InitializationSequence InitSeq(*this, Entity, Kind, CtorArgExprs, |
557 | 596 | /*TopLevelOfInitList=*/false, |
558 | 596 | /*TreatUnavailableAsInvalid=*/false); |
559 | | |
560 | | // [dcl.fct.def.coroutine]5.7 |
561 | | // promise-constructor-arguments is determined as follows: overload |
562 | | // resolution is performed on a promise constructor call created by |
563 | | // assembling an argument list q_1 ... q_n . If a viable constructor is |
564 | | // found ([over.match.viable]), then promise-constructor-arguments is ( q_1 |
565 | | // , ..., q_n ), otherwise promise-constructor-arguments is empty. |
566 | 596 | if (InitSeq) { |
567 | 134 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, CtorArgExprs); |
568 | 134 | if (Result.isInvalid()) { |
569 | 0 | VD->setInvalidDecl(); |
570 | 134 | } else if (Result.get()) { |
571 | 134 | VD->setInit(MaybeCreateExprWithCleanups(Result.get())); |
572 | 134 | VD->setInitStyle(VarDecl::CallInit); |
573 | 134 | CheckCompleteVariableDeclaration(VD); |
574 | 134 | } |
575 | 134 | } else |
576 | 462 | ActOnUninitializedDecl(VD); |
577 | 596 | } else |
578 | 496 | ActOnUninitializedDecl(VD); |
579 | | |
580 | 1.09k | FD->addDecl(VD); |
581 | 1.09k | return VD; |
582 | 1.09k | } |
583 | | |
584 | | /// Check that this is a context in which a coroutine suspension can appear. |
585 | | static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc, |
586 | | StringRef Keyword, |
587 | 5.54k | bool IsImplicit = false) { |
588 | 5.54k | if (!isValidCoroutineContext(S, Loc, Keyword)) |
589 | 42 | return nullptr; |
590 | | |
591 | 5.49k | assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope"); |
592 | | |
593 | 0 | auto *ScopeInfo = S.getCurFunction(); |
594 | 5.49k | assert(ScopeInfo && "missing function scope for function"); |
595 | | |
596 | 5.49k | if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit1.63k ) |
597 | 1.15k | ScopeInfo->setFirstCoroutineStmt(Loc, Keyword); |
598 | | |
599 | 5.49k | if (ScopeInfo->CoroutinePromise) |
600 | 4.57k | return ScopeInfo; |
601 | | |
602 | 921 | if (!S.buildCoroutineParameterMoves(Loc)) |
603 | 5 | return nullptr; |
604 | | |
605 | 916 | ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc); |
606 | 916 | if (!ScopeInfo->CoroutinePromise) |
607 | 65 | return nullptr; |
608 | | |
609 | 851 | return ScopeInfo; |
610 | 916 | } |
611 | | |
612 | | /// Recursively check \p E and all its children to see if any call target |
613 | | /// (including constructor call) is declared noexcept. Also any value returned |
614 | | /// from the call has a noexcept destructor. |
615 | | static void checkNoThrow(Sema &S, const Stmt *E, |
616 | 8.49k | llvm::SmallPtrSetImpl<const Decl *> &ThrowingDecls) { |
617 | 8.49k | auto checkDeclNoexcept = [&](const Decl *D, bool IsDtor = false) { |
618 | | // In the case of dtor, the call to dtor is implicit and hence we should |
619 | | // pass nullptr to canCalleeThrow. |
620 | 4.11k | if (Sema::canCalleeThrow(S, IsDtor ? nullptr16 : cast<Expr>(E)4.09k , D)) { |
621 | 51 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
622 | | // co_await promise.final_suspend() could end up calling |
623 | | // __builtin_coro_resume for symmetric transfer if await_suspend() |
624 | | // returns a handle. In that case, even __builtin_coro_resume is not |
625 | | // declared as noexcept and may throw, it does not throw _into_ the |
626 | | // coroutine that just suspended, but rather throws back out from |
627 | | // whoever called coroutine_handle::resume(), hence we claim that |
628 | | // logically it does not throw. |
629 | 51 | if (FD->getBuiltinID() == Builtin::BI__builtin_coro_resume) |
630 | 19 | return; |
631 | 51 | } |
632 | 32 | if (ThrowingDecls.empty()) { |
633 | | // [dcl.fct.def.coroutine]p15 |
634 | | // The expression co_await promise.final_suspend() shall not be |
635 | | // potentially-throwing ([except.spec]). |
636 | | // |
637 | | // First time seeing an error, emit the error message. |
638 | 6 | S.Diag(cast<FunctionDecl>(S.CurContext)->getLocation(), |
639 | 6 | diag::err_coroutine_promise_final_suspend_requires_nothrow); |
640 | 6 | } |
641 | 32 | ThrowingDecls.insert(D); |
642 | 32 | } |
643 | 4.11k | }; |
644 | | |
645 | 8.49k | if (auto *CE = dyn_cast<CXXConstructExpr>(E)) { |
646 | 0 | CXXConstructorDecl *Ctor = CE->getConstructor(); |
647 | 0 | checkDeclNoexcept(Ctor); |
648 | | // Check the corresponding destructor of the constructor. |
649 | 0 | checkDeclNoexcept(Ctor->getParent()->getDestructor(), /*IsDtor=*/true); |
650 | 8.49k | } else if (auto *CE = dyn_cast<CallExpr>(E)) { |
651 | 4.55k | if (CE->isTypeDependent()) |
652 | 456 | return; |
653 | | |
654 | 4.09k | checkDeclNoexcept(CE->getCalleeDecl()); |
655 | 4.09k | QualType ReturnType = CE->getCallReturnType(S.getASTContext()); |
656 | | // Check the destructor of the call return type, if any. |
657 | 4.09k | if (ReturnType.isDestructedType() == |
658 | 4.09k | QualType::DestructionKind::DK_cxx_destructor) { |
659 | 16 | const auto *T = |
660 | 16 | cast<RecordType>(ReturnType.getCanonicalType().getTypePtr()); |
661 | 16 | checkDeclNoexcept(cast<CXXRecordDecl>(T->getDecl())->getDestructor(), |
662 | 16 | /*IsDtor=*/true); |
663 | 16 | } |
664 | 4.09k | } else |
665 | 8.13k | for (const auto *Child : E->children())3.94k { |
666 | 8.13k | if (!Child) |
667 | 684 | continue; |
668 | 7.44k | checkNoThrow(S, Child, ThrowingDecls); |
669 | 7.44k | } |
670 | 8.49k | } |
671 | | |
672 | 1.04k | bool Sema::checkFinalSuspendNoThrow(const Stmt *FinalSuspend) { |
673 | 1.04k | llvm::SmallPtrSet<const Decl *, 4> ThrowingDecls; |
674 | | // We first collect all declarations that should not throw but not declared |
675 | | // with noexcept. We then sort them based on the location before printing. |
676 | | // This is to avoid emitting the same note multiple times on the same |
677 | | // declaration, and also provide a deterministic order for the messages. |
678 | 1.04k | checkNoThrow(*this, FinalSuspend, ThrowingDecls); |
679 | 1.04k | auto SortedDecls = llvm::SmallVector<const Decl *, 4>{ThrowingDecls.begin(), |
680 | 1.04k | ThrowingDecls.end()}; |
681 | 1.04k | sort(SortedDecls, [](const Decl *A, const Decl *B) { |
682 | 32 | return A->getEndLoc() < B->getEndLoc(); |
683 | 32 | }); |
684 | 1.04k | for (const auto *D : SortedDecls) { |
685 | 22 | Diag(D->getEndLoc(), diag::note_coroutine_function_declare_noexcept); |
686 | 22 | } |
687 | 1.04k | return ThrowingDecls.empty(); |
688 | 1.04k | } |
689 | | |
690 | | bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc, |
691 | 1.17k | StringRef Keyword) { |
692 | 1.17k | if (!checkCoroutineContext(*this, KWLoc, Keyword)) |
693 | 112 | return false; |
694 | 1.06k | auto *ScopeInfo = getCurFunction(); |
695 | 1.06k | assert(ScopeInfo->CoroutinePromise); |
696 | | |
697 | | // If we have existing coroutine statements then we have already built |
698 | | // the initial and final suspend points. |
699 | 1.06k | if (!ScopeInfo->NeedsCoroutineSuspends) |
700 | 214 | return true; |
701 | | |
702 | 851 | ScopeInfo->setNeedsCoroutineSuspends(false); |
703 | | |
704 | 851 | auto *Fn = cast<FunctionDecl>(CurContext); |
705 | 851 | SourceLocation Loc = Fn->getLocation(); |
706 | | // Build the initial suspend point |
707 | 1.66k | auto buildSuspends = [&](StringRef Name) mutable -> StmtResult { |
708 | 1.66k | ExprResult Operand = |
709 | 1.66k | buildPromiseCall(*this, ScopeInfo->CoroutinePromise, Loc, Name, None); |
710 | 1.66k | if (Operand.isInvalid()) |
711 | 15 | return StmtError(); |
712 | 1.65k | ExprResult Suspend = |
713 | 1.65k | buildOperatorCoawaitCall(*this, SC, Loc, Operand.get()); |
714 | 1.65k | if (Suspend.isInvalid()) |
715 | 0 | return StmtError(); |
716 | 1.65k | Suspend = BuildResolvedCoawaitExpr(Loc, Operand.get(), Suspend.get(), |
717 | 1.65k | /*IsImplicit*/ true); |
718 | 1.65k | Suspend = ActOnFinishFullExpr(Suspend.get(), /*DiscardedValue*/ false); |
719 | 1.65k | if (Suspend.isInvalid()) { |
720 | 30 | Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required) |
721 | 30 | << ((Name == "initial_suspend") ? 025 : 15 ); |
722 | 30 | Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword; |
723 | 30 | return StmtError(); |
724 | 30 | } |
725 | 1.62k | return cast<Stmt>(Suspend.get()); |
726 | 1.65k | }; |
727 | | |
728 | 851 | StmtResult InitSuspend = buildSuspends("initial_suspend"); |
729 | 851 | if (InitSuspend.isInvalid()) |
730 | 35 | return true; |
731 | | |
732 | 816 | StmtResult FinalSuspend = buildSuspends("final_suspend"); |
733 | 816 | if (FinalSuspend.isInvalid() || !checkFinalSuspendNoThrow(FinalSuspend.get())806 ) |
734 | 14 | return true; |
735 | | |
736 | 802 | ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get()); |
737 | | |
738 | 802 | return true; |
739 | 816 | } |
740 | | |
741 | | // Recursively walks up the scope hierarchy until either a 'catch' or a function |
742 | | // scope is found, whichever comes first. |
743 | 708 | static bool isWithinCatchScope(Scope *S) { |
744 | | // 'co_await' and 'co_yield' keywords are disallowed within catch blocks, but |
745 | | // lambdas that use 'co_await' are allowed. The loop below ends when a |
746 | | // function scope is found in order to ensure the following behavior: |
747 | | // |
748 | | // void foo() { // <- function scope |
749 | | // try { // |
750 | | // co_await x; // <- 'co_await' is OK within a function scope |
751 | | // } catch { // <- catch scope |
752 | | // co_await x; // <- 'co_await' is not OK within a catch scope |
753 | | // []() { // <- function scope |
754 | | // co_await x; // <- 'co_await' is OK within a function scope |
755 | | // }(); |
756 | | // } |
757 | | // } |
758 | 819 | while (S && !S->isFunctionScope()807 ) { |
759 | 126 | if (S->isCatchScope()) |
760 | 15 | return true; |
761 | 111 | S = S->getParent(); |
762 | 111 | } |
763 | 693 | return false; |
764 | 708 | } |
765 | | |
766 | | // [expr.await]p2, emphasis added: "An await-expression shall appear only in |
767 | | // a *potentially evaluated* expression within the compound-statement of a |
768 | | // function-body *outside of a handler* [...] A context within a function |
769 | | // where an await-expression can appear is called a suspension context of the |
770 | | // function." |
771 | | static void checkSuspensionContext(Sema &S, SourceLocation Loc, |
772 | 708 | StringRef Keyword) { |
773 | | // First emphasis of [expr.await]p2: must be a potentially evaluated context. |
774 | | // That is, 'co_await' and 'co_yield' cannot appear in subexpressions of |
775 | | // \c sizeof. |
776 | 708 | if (S.isUnevaluatedContext()) |
777 | 30 | S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword; |
778 | | |
779 | | // Second emphasis of [expr.await]p2: must be outside of an exception handler. |
780 | 708 | if (isWithinCatchScope(S.getCurScope())) |
781 | 15 | S.Diag(Loc, diag::err_coroutine_within_handler) << Keyword; |
782 | 708 | } |
783 | | |
784 | 495 | ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) { |
785 | 495 | if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) { |
786 | 65 | CorrectDelayedTyposInExpr(E); |
787 | 65 | return ExprError(); |
788 | 65 | } |
789 | | |
790 | 430 | checkSuspensionContext(*this, Loc, "co_await"); |
791 | | |
792 | 430 | if (E->hasPlaceholderType()) { |
793 | 10 | ExprResult R = CheckPlaceholderExpr(E); |
794 | 10 | if (R.isInvalid()) return ExprError()0 ; |
795 | 10 | E = R.get(); |
796 | 10 | } |
797 | 430 | ExprResult Lookup = BuildOperatorCoawaitLookupExpr(S, Loc); |
798 | 430 | if (Lookup.isInvalid()) |
799 | 0 | return ExprError(); |
800 | 430 | return BuildUnresolvedCoawaitExpr(Loc, E, |
801 | 430 | cast<UnresolvedLookupExpr>(Lookup.get())); |
802 | 430 | } |
803 | | |
804 | 2.85k | ExprResult Sema::BuildOperatorCoawaitLookupExpr(Scope *S, SourceLocation Loc) { |
805 | 2.85k | DeclarationName OpName = |
806 | 2.85k | Context.DeclarationNames.getCXXOperatorName(OO_Coawait); |
807 | 2.85k | LookupResult Operators(*this, OpName, SourceLocation(), |
808 | 2.85k | Sema::LookupOperatorName); |
809 | 2.85k | LookupName(Operators, S); |
810 | | |
811 | 2.85k | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous"); |
812 | 0 | const auto &Functions = Operators.asUnresolvedSet(); |
813 | 2.85k | bool IsOverloaded = |
814 | 2.85k | Functions.size() > 1 || |
815 | 2.85k | (2.66k Functions.size() == 12.66k && isa<FunctionTemplateDecl>(*Functions.begin())135 ); |
816 | 2.85k | Expr *CoawaitOp = UnresolvedLookupExpr::Create( |
817 | 2.85k | Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(), |
818 | 2.85k | DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, IsOverloaded, |
819 | 2.85k | Functions.begin(), Functions.end()); |
820 | 2.85k | assert(CoawaitOp); |
821 | 0 | return CoawaitOp; |
822 | 2.85k | } |
823 | | |
824 | | // Attempts to resolve and build a CoawaitExpr from "raw" inputs, bailing out to |
825 | | // DependentCoawaitExpr if needed. |
826 | | ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *Operand, |
827 | 548 | UnresolvedLookupExpr *Lookup) { |
828 | 548 | auto *FSI = checkCoroutineContext(*this, Loc, "co_await"); |
829 | 548 | if (!FSI) |
830 | 0 | return ExprError(); |
831 | | |
832 | 548 | if (Operand->hasPlaceholderType()) { |
833 | 0 | ExprResult R = CheckPlaceholderExpr(Operand); |
834 | 0 | if (R.isInvalid()) |
835 | 0 | return ExprError(); |
836 | 0 | Operand = R.get(); |
837 | 0 | } |
838 | | |
839 | 548 | auto *Promise = FSI->CoroutinePromise; |
840 | 548 | if (Promise->getType()->isDependentType()) { |
841 | 111 | Expr *Res = new (Context) |
842 | 111 | DependentCoawaitExpr(Loc, Context.DependentTy, Operand, Lookup); |
843 | 111 | return Res; |
844 | 111 | } |
845 | | |
846 | 437 | auto *RD = Promise->getType()->getAsCXXRecordDecl(); |
847 | 437 | auto *Transformed = Operand; |
848 | 437 | if (lookupMember(*this, "await_transform", RD, Loc)) { |
849 | 94 | ExprResult R = |
850 | 94 | buildPromiseCall(*this, Promise, Loc, "await_transform", Operand); |
851 | 94 | if (R.isInvalid()) { |
852 | 11 | Diag(Loc, |
853 | 11 | diag::note_coroutine_promise_implicit_await_transform_required_here) |
854 | 11 | << Operand->getSourceRange(); |
855 | 11 | return ExprError(); |
856 | 11 | } |
857 | 83 | Transformed = R.get(); |
858 | 83 | } |
859 | 426 | ExprResult Awaiter = BuildOperatorCoawaitCall(Loc, Transformed, Lookup); |
860 | 426 | if (Awaiter.isInvalid()) |
861 | 14 | return ExprError(); |
862 | | |
863 | 412 | return BuildResolvedCoawaitExpr(Loc, Operand, Awaiter.get()); |
864 | 426 | } |
865 | | |
866 | | ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *Operand, |
867 | 2.56k | Expr *Awaiter, bool IsImplicit) { |
868 | 2.56k | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit); |
869 | 2.56k | if (!Coroutine) |
870 | 0 | return ExprError(); |
871 | | |
872 | 2.56k | if (Awaiter->hasPlaceholderType()) { |
873 | 0 | ExprResult R = CheckPlaceholderExpr(Awaiter); |
874 | 0 | if (R.isInvalid()) return ExprError(); |
875 | 0 | Awaiter = R.get(); |
876 | 0 | } |
877 | | |
878 | 2.56k | if (Awaiter->getType()->isDependentType()) { |
879 | 478 | Expr *Res = new (Context) |
880 | 478 | CoawaitExpr(Loc, Context.DependentTy, Operand, Awaiter, IsImplicit); |
881 | 478 | return Res; |
882 | 478 | } |
883 | | |
884 | | // If the expression is a temporary, materialize it as an lvalue so that we |
885 | | // can use it multiple times. |
886 | 2.08k | if (Awaiter->isPRValue()) |
887 | 1.86k | Awaiter = CreateMaterializeTemporaryExpr(Awaiter->getType(), Awaiter, true); |
888 | | |
889 | | // The location of the `co_await` token cannot be used when constructing |
890 | | // the member call expressions since it's before the location of `Expr`, which |
891 | | // is used as the start of the member call expression. |
892 | 2.08k | SourceLocation CallLoc = Awaiter->getExprLoc(); |
893 | | |
894 | | // Build the await_ready, await_suspend, await_resume calls. |
895 | 2.08k | ReadySuspendResumeResult RSS = |
896 | 2.08k | buildCoawaitCalls(*this, Coroutine->CoroutinePromise, CallLoc, Awaiter); |
897 | 2.08k | if (RSS.IsInvalid) |
898 | 92 | return ExprError(); |
899 | | |
900 | 1.99k | Expr *Res = new (Context) |
901 | 1.99k | CoawaitExpr(Loc, Operand, Awaiter, RSS.Results[0], RSS.Results[1], |
902 | 1.99k | RSS.Results[2], RSS.OpaqueValue, IsImplicit); |
903 | | |
904 | 1.99k | return Res; |
905 | 2.08k | } |
906 | | |
907 | 293 | ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) { |
908 | 293 | if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) { |
909 | 15 | CorrectDelayedTyposInExpr(E); |
910 | 15 | return ExprError(); |
911 | 15 | } |
912 | | |
913 | 278 | checkSuspensionContext(*this, Loc, "co_yield"); |
914 | | |
915 | | // Build yield_value call. |
916 | 278 | ExprResult Awaitable = buildPromiseCall( |
917 | 278 | *this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E); |
918 | 278 | if (Awaitable.isInvalid()) |
919 | 15 | return ExprError(); |
920 | | |
921 | | // Build 'operator co_await' call. |
922 | 263 | Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get()); |
923 | 263 | if (Awaitable.isInvalid()) |
924 | 0 | return ExprError(); |
925 | | |
926 | 263 | return BuildCoyieldExpr(Loc, Awaitable.get()); |
927 | 263 | } |
928 | 361 | ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) { |
929 | 361 | auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield"); |
930 | 361 | if (!Coroutine) |
931 | 0 | return ExprError(); |
932 | | |
933 | 361 | if (E->hasPlaceholderType()) { |
934 | 0 | ExprResult R = CheckPlaceholderExpr(E); |
935 | 0 | if (R.isInvalid()) return ExprError(); |
936 | 0 | E = R.get(); |
937 | 0 | } |
938 | | |
939 | 361 | Expr *Operand = E; |
940 | | |
941 | 361 | if (E->getType()->isDependentType()) { |
942 | 98 | Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, Operand, E); |
943 | 98 | return Res; |
944 | 98 | } |
945 | | |
946 | | // If the expression is a temporary, materialize it as an lvalue so that we |
947 | | // can use it multiple times. |
948 | 263 | if (E->isPRValue()) |
949 | 263 | E = CreateMaterializeTemporaryExpr(E->getType(), E, true); |
950 | | |
951 | | // Build the await_ready, await_suspend, await_resume calls. |
952 | 263 | ReadySuspendResumeResult RSS = buildCoawaitCalls( |
953 | 263 | *this, Coroutine->CoroutinePromise, Loc, E); |
954 | 263 | if (RSS.IsInvalid) |
955 | 5 | return ExprError(); |
956 | | |
957 | 258 | Expr *Res = |
958 | 258 | new (Context) CoyieldExpr(Loc, Operand, E, RSS.Results[0], RSS.Results[1], |
959 | 258 | RSS.Results[2], RSS.OpaqueValue); |
960 | | |
961 | 258 | return Res; |
962 | 263 | } |
963 | | |
964 | 374 | StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) { |
965 | 374 | if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) { |
966 | 30 | CorrectDelayedTyposInExpr(E); |
967 | 30 | return StmtError(); |
968 | 30 | } |
969 | 344 | return BuildCoreturnStmt(Loc, E); |
970 | 374 | } |
971 | | |
972 | | StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E, |
973 | 889 | bool IsImplicit) { |
974 | 889 | auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit); |
975 | 889 | if (!FSI) |
976 | 0 | return StmtError(); |
977 | | |
978 | 889 | if (E && E->hasPlaceholderType()136 && |
979 | 889 | !E->hasPlaceholderType(BuiltinType::Overload)10 ) { |
980 | 0 | ExprResult R = CheckPlaceholderExpr(E); |
981 | 0 | if (R.isInvalid()) return StmtError(); |
982 | 0 | E = R.get(); |
983 | 0 | } |
984 | | |
985 | 889 | VarDecl *Promise = FSI->CoroutinePromise; |
986 | 889 | ExprResult PC; |
987 | 889 | if (E && (136 isa<InitListExpr>(E)136 || !E->getType()->isVoidType()124 )) { |
988 | 134 | getNamedReturnInfo(E, SimplerImplicitMoveMode::ForceOn); |
989 | 134 | PC = buildPromiseCall(*this, Promise, Loc, "return_value", E); |
990 | 755 | } else { |
991 | 755 | E = MakeFullDiscardedValueExpr(E).get(); |
992 | 755 | PC = buildPromiseCall(*this, Promise, Loc, "return_void", None); |
993 | 755 | } |
994 | 889 | if (PC.isInvalid()) |
995 | 32 | return StmtError(); |
996 | | |
997 | 857 | Expr *PCE = ActOnFinishFullExpr(PC.get(), /*DiscardedValue*/ false).get(); |
998 | | |
999 | 857 | Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit); |
1000 | 857 | return Res; |
1001 | 889 | } |
1002 | | |
1003 | | /// Look up the std::nothrow object. |
1004 | 24 | static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) { |
1005 | 24 | NamespaceDecl *Std = S.getStdNamespace(); |
1006 | 24 | assert(Std && "Should already be diagnosed"); |
1007 | | |
1008 | 0 | LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc, |
1009 | 24 | Sema::LookupOrdinaryName); |
1010 | 24 | if (!S.LookupQualifiedName(Result, Std)) { |
1011 | | // <coroutine> is not requred to include <new>, so we couldn't omit |
1012 | | // the check here. |
1013 | 0 | S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found); |
1014 | 0 | return nullptr; |
1015 | 0 | } |
1016 | | |
1017 | 24 | auto *VD = Result.getAsSingle<VarDecl>(); |
1018 | 24 | if (!VD) { |
1019 | 0 | Result.suppressDiagnostics(); |
1020 | | // We found something weird. Complain about the first thing we found. |
1021 | 0 | NamedDecl *Found = *Result.begin(); |
1022 | 0 | S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow); |
1023 | 0 | return nullptr; |
1024 | 0 | } |
1025 | | |
1026 | 24 | ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc); |
1027 | 24 | if (DR.isInvalid()) |
1028 | 0 | return nullptr; |
1029 | | |
1030 | 24 | return DR.get(); |
1031 | 24 | } |
1032 | | |
1033 | | // Find an appropriate delete for the promise. |
1034 | | static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc, |
1035 | 693 | QualType PromiseType) { |
1036 | 693 | FunctionDecl *OperatorDelete = nullptr; |
1037 | | |
1038 | 693 | DeclarationName DeleteName = |
1039 | 693 | S.Context.DeclarationNames.getCXXOperatorName(OO_Delete); |
1040 | | |
1041 | 693 | auto *PointeeRD = PromiseType->getAsCXXRecordDecl(); |
1042 | 693 | assert(PointeeRD && "PromiseType must be a CxxRecordDecl type"); |
1043 | | |
1044 | | // [dcl.fct.def.coroutine]p12 |
1045 | | // The deallocation function's name is looked up by searching for it in the |
1046 | | // scope of the promise type. If nothing is found, a search is performed in |
1047 | | // the global scope. |
1048 | 693 | if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete)) |
1049 | 0 | return nullptr; |
1050 | | |
1051 | | // FIXME: We didn't implement following selection: |
1052 | | // [dcl.fct.def.coroutine]p12 |
1053 | | // If both a usual deallocation function with only a pointer parameter and a |
1054 | | // usual deallocation function with both a pointer parameter and a size |
1055 | | // parameter are found, then the selected deallocation function shall be the |
1056 | | // one with two parameters. Otherwise, the selected deallocation function |
1057 | | // shall be the function with one parameter. |
1058 | | |
1059 | 693 | if (!OperatorDelete) { |
1060 | | // Look for a global declaration. |
1061 | 689 | const bool CanProvideSize = S.isCompleteType(Loc, PromiseType); |
1062 | 689 | const bool Overaligned = false; |
1063 | 689 | OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize, |
1064 | 689 | Overaligned, DeleteName); |
1065 | 689 | } |
1066 | 693 | S.MarkFunctionReferenced(Loc, OperatorDelete); |
1067 | 693 | return OperatorDelete; |
1068 | 693 | } |
1069 | | |
1070 | | |
1071 | 1.15k | void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) { |
1072 | 1.15k | FunctionScopeInfo *Fn = getCurFunction(); |
1073 | 1.15k | assert(Fn && Fn->isCoroutine() && "not a coroutine"); |
1074 | 1.15k | if (!Body) { |
1075 | 46 | assert(FD->isInvalidDecl() && |
1076 | 46 | "a null body is only allowed for invalid declarations"); |
1077 | 0 | return; |
1078 | 46 | } |
1079 | | // We have a function that uses coroutine keywords, but we failed to build |
1080 | | // the promise type. |
1081 | 1.10k | if (!Fn->CoroutinePromise) |
1082 | 65 | return FD->setInvalidDecl(); |
1083 | | |
1084 | 1.04k | if (isa<CoroutineBodyStmt>(Body)) { |
1085 | | // Nothing todo. the body is already a transformed coroutine body statement. |
1086 | 193 | return; |
1087 | 193 | } |
1088 | | |
1089 | | // The always_inline attribute doesn't reliably apply to a coroutine, |
1090 | | // because the coroutine will be split into pieces and some pieces |
1091 | | // might be called indirectly, as in a virtual call. Even the ramp |
1092 | | // function cannot be inlined at -O0, due to pipeline ordering |
1093 | | // problems (see https://llvm.org/PR53413). Tell the user about it. |
1094 | 851 | if (FD->hasAttr<AlwaysInlineAttr>()) |
1095 | 6 | Diag(FD->getLocation(), diag::warn_always_inline_coroutine); |
1096 | | |
1097 | | // [stmt.return.coroutine]p1: |
1098 | | // A coroutine shall not enclose a return statement ([stmt.return]). |
1099 | 851 | if (Fn->FirstReturnLoc.isValid()) { |
1100 | 65 | assert(Fn->FirstCoroutineStmtLoc.isValid() && |
1101 | 65 | "first coroutine location not set"); |
1102 | 0 | Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine); |
1103 | 65 | Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
1104 | 65 | << Fn->getFirstCoroutineStmtKeyword(); |
1105 | 65 | } |
1106 | 0 | CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body); |
1107 | 851 | if (Builder.isInvalid() || !Builder.buildStatements()802 ) |
1108 | 99 | return FD->setInvalidDecl(); |
1109 | | |
1110 | | // Build body for the coroutine wrapper statement. |
1111 | 752 | Body = CoroutineBodyStmt::Create(Context, Builder); |
1112 | 752 | } |
1113 | | |
1114 | | CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD, |
1115 | | sema::FunctionScopeInfo &Fn, |
1116 | | Stmt *Body) |
1117 | | : S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()), |
1118 | | IsPromiseDependentType( |
1119 | | !Fn.CoroutinePromise || |
1120 | 1.05k | Fn.CoroutinePromise->getType()->isDependentType()) { |
1121 | 1.05k | this->Body = Body; |
1122 | | |
1123 | 1.05k | for (auto KV : Fn.CoroutineParameterMoves) |
1124 | 649 | this->ParamMovesVector.push_back(KV.second); |
1125 | 1.05k | this->ParamMoves = this->ParamMovesVector; |
1126 | | |
1127 | 1.05k | if (!IsPromiseDependentType) { |
1128 | 831 | PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl(); |
1129 | 831 | assert(PromiseRecordDecl && "Type should have already been checked"); |
1130 | 831 | } |
1131 | 1.05k | this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend(); |
1132 | 1.05k | } |
1133 | | |
1134 | 802 | bool CoroutineStmtBuilder::buildStatements() { |
1135 | 802 | assert(this->IsValid && "coroutine already invalid"); |
1136 | 0 | this->IsValid = makeReturnObject(); |
1137 | 802 | if (this->IsValid && !IsPromiseDependentType797 ) |
1138 | 589 | buildDependentStatements(); |
1139 | 802 | return this->IsValid; |
1140 | 802 | } |
1141 | | |
1142 | 753 | bool CoroutineStmtBuilder::buildDependentStatements() { |
1143 | 753 | assert(this->IsValid && "coroutine already invalid"); |
1144 | 0 | assert(!this->IsPromiseDependentType && |
1145 | 753 | "coroutine cannot have a dependent promise type"); |
1146 | 753 | this->IsValid = makeOnException() && makeOnFallthrough()741 && |
1147 | 753 | makeGroDeclAndReturnStmt()731 && makeReturnOnAllocFailure()721 && |
1148 | 753 | makeNewAndDeleteExpr()706 ; |
1149 | 753 | return this->IsValid; |
1150 | 753 | } |
1151 | | |
1152 | 1.05k | bool CoroutineStmtBuilder::makePromiseStmt() { |
1153 | | // Form a declaration statement for the promise declaration, so that AST |
1154 | | // visitors can more easily find it. |
1155 | 1.05k | StmtResult PromiseStmt = |
1156 | 1.05k | S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc); |
1157 | 1.05k | if (PromiseStmt.isInvalid()) |
1158 | 0 | return false; |
1159 | | |
1160 | 1.05k | this->Promise = PromiseStmt.get(); |
1161 | 1.05k | return true; |
1162 | 1.05k | } |
1163 | | |
1164 | 1.05k | bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() { |
1165 | 1.05k | if (Fn.hasInvalidCoroutineSuspends()) |
1166 | 49 | return false; |
1167 | 1.01k | this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first); |
1168 | 1.01k | this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second); |
1169 | 1.01k | return true; |
1170 | 1.05k | } |
1171 | | |
1172 | | static bool diagReturnOnAllocFailure(Sema &S, Expr *E, |
1173 | | CXXRecordDecl *PromiseRecordDecl, |
1174 | 54 | FunctionScopeInfo &Fn) { |
1175 | 54 | auto Loc = E->getExprLoc(); |
1176 | 54 | if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) { |
1177 | 54 | auto *Decl = DeclRef->getDecl(); |
1178 | 54 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) { |
1179 | 54 | if (Method->isStatic()) |
1180 | 49 | return true; |
1181 | 5 | else |
1182 | 5 | Loc = Decl->getLocation(); |
1183 | 54 | } |
1184 | 54 | } |
1185 | | |
1186 | 5 | S.Diag( |
1187 | 5 | Loc, |
1188 | 5 | diag::err_coroutine_promise_get_return_object_on_allocation_failure) |
1189 | 5 | << PromiseRecordDecl; |
1190 | 5 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
1191 | 5 | << Fn.getFirstCoroutineStmtKeyword(); |
1192 | 5 | return false; |
1193 | 54 | } |
1194 | | |
1195 | 721 | bool CoroutineStmtBuilder::makeReturnOnAllocFailure() { |
1196 | 721 | assert(!IsPromiseDependentType && |
1197 | 721 | "cannot make statement while the promise type is dependent"); |
1198 | | |
1199 | | // [dcl.fct.def.coroutine]p10 |
1200 | | // If a search for the name get_return_object_on_allocation_failure in |
1201 | | // the scope of the promise type ([class.member.lookup]) finds any |
1202 | | // declarations, then the result of a call to an allocation function used to |
1203 | | // obtain storage for the coroutine state is assumed to return nullptr if it |
1204 | | // fails to obtain storage, ... If the allocation function returns nullptr, |
1205 | | // ... and the return value is obtained by a call to |
1206 | | // T::get_return_object_on_allocation_failure(), where T is the |
1207 | | // promise type. |
1208 | 0 | DeclarationName DN = |
1209 | 721 | S.PP.getIdentifierInfo("get_return_object_on_allocation_failure"); |
1210 | 721 | LookupResult Found(S, DN, Loc, Sema::LookupMemberName); |
1211 | 721 | if (!S.LookupQualifiedName(Found, PromiseRecordDecl)) |
1212 | 667 | return true; |
1213 | | |
1214 | 54 | CXXScopeSpec SS; |
1215 | 54 | ExprResult DeclNameExpr = |
1216 | 54 | S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false); |
1217 | 54 | if (DeclNameExpr.isInvalid()) |
1218 | 0 | return false; |
1219 | | |
1220 | 54 | if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn)) |
1221 | 5 | return false; |
1222 | | |
1223 | 49 | ExprResult ReturnObjectOnAllocationFailure = |
1224 | 49 | S.BuildCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc); |
1225 | 49 | if (ReturnObjectOnAllocationFailure.isInvalid()) |
1226 | 0 | return false; |
1227 | | |
1228 | 49 | StmtResult ReturnStmt = |
1229 | 49 | S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get()); |
1230 | 49 | if (ReturnStmt.isInvalid()) { |
1231 | 10 | S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here) |
1232 | 10 | << DN; |
1233 | 10 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
1234 | 10 | << Fn.getFirstCoroutineStmtKeyword(); |
1235 | 10 | return false; |
1236 | 10 | } |
1237 | | |
1238 | 39 | this->ReturnStmtOnAllocFailure = ReturnStmt.get(); |
1239 | 39 | return true; |
1240 | 49 | } |
1241 | | |
1242 | | // Collect placement arguments for allocation function of coroutine FD. |
1243 | | // Return true if we collect placement arguments succesfully. Return false, |
1244 | | // otherwise. |
1245 | | static bool collectPlacementArgs(Sema &S, FunctionDecl &FD, SourceLocation Loc, |
1246 | 30 | SmallVectorImpl<Expr *> &PlacementArgs) { |
1247 | 30 | if (auto *MD = dyn_cast<CXXMethodDecl>(&FD)) { |
1248 | 0 | if (MD->isInstance() && !isLambdaCallOperator(MD)) { |
1249 | 0 | ExprResult ThisExpr = S.ActOnCXXThis(Loc); |
1250 | 0 | if (ThisExpr.isInvalid()) |
1251 | 0 | return false; |
1252 | 0 | ThisExpr = S.CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get()); |
1253 | 0 | if (ThisExpr.isInvalid()) |
1254 | 0 | return false; |
1255 | 0 | PlacementArgs.push_back(ThisExpr.get()); |
1256 | 0 | } |
1257 | 0 | } |
1258 | | |
1259 | 50 | for (auto *PD : FD.parameters())30 { |
1260 | 50 | if (PD->getType()->isDependentType()) |
1261 | 0 | continue; |
1262 | | |
1263 | | // Build a reference to the parameter. |
1264 | 50 | auto PDLoc = PD->getLocation(); |
1265 | 50 | ExprResult PDRefExpr = |
1266 | 50 | S.BuildDeclRefExpr(PD, PD->getOriginalType().getNonReferenceType(), |
1267 | 50 | ExprValueKind::VK_LValue, PDLoc); |
1268 | 50 | if (PDRefExpr.isInvalid()) |
1269 | 0 | return false; |
1270 | | |
1271 | 50 | PlacementArgs.push_back(PDRefExpr.get()); |
1272 | 50 | } |
1273 | | |
1274 | 30 | return true; |
1275 | 30 | } |
1276 | | |
1277 | 706 | bool CoroutineStmtBuilder::makeNewAndDeleteExpr() { |
1278 | | // Form and check allocation and deallocation calls. |
1279 | 706 | assert(!IsPromiseDependentType && |
1280 | 706 | "cannot make statement while the promise type is dependent"); |
1281 | 0 | QualType PromiseType = Fn.CoroutinePromise->getType(); |
1282 | | |
1283 | 706 | if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type)) |
1284 | 0 | return false; |
1285 | | |
1286 | 706 | const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr; |
1287 | | |
1288 | | // According to [dcl.fct.def.coroutine]p9, Lookup allocation functions using a |
1289 | | // parameter list composed of the requested size of the coroutine state being |
1290 | | // allocated, followed by the coroutine function's arguments. If a matching |
1291 | | // allocation function exists, use it. Otherwise, use an allocation function |
1292 | | // that just takes the requested size. |
1293 | | // |
1294 | | // [dcl.fct.def.coroutine]p9 |
1295 | | // An implementation may need to allocate additional storage for a |
1296 | | // coroutine. |
1297 | | // This storage is known as the coroutine state and is obtained by calling a |
1298 | | // non-array allocation function ([basic.stc.dynamic.allocation]). The |
1299 | | // allocation function's name is looked up by searching for it in the scope of |
1300 | | // the promise type. |
1301 | | // - If any declarations are found, overload resolution is performed on a |
1302 | | // function call created by assembling an argument list. The first argument is |
1303 | | // the amount of space requested, and has type std::size_t. The |
1304 | | // lvalues p1 ... pn are the succeeding arguments. |
1305 | | // |
1306 | | // ...where "p1 ... pn" are defined earlier as: |
1307 | | // |
1308 | | // [dcl.fct.def.coroutine]p3 |
1309 | | // The promise type of a coroutine is `std::coroutine_traits<R, P1, ..., |
1310 | | // Pn>` |
1311 | | // , where R is the return type of the function, and `P1, ..., Pn` are the |
1312 | | // sequence of types of the non-object function parameters, preceded by the |
1313 | | // type of the object parameter ([dcl.fct]) if the coroutine is a non-static |
1314 | | // member function. [dcl.fct.def.coroutine]p4 In the following, p_i is an |
1315 | | // lvalue of type P_i, where p1 denotes the object parameter and p_i+1 denotes |
1316 | | // the i-th non-object function parameter for a non-static member function, |
1317 | | // and p_i denotes the i-th function parameter otherwise. For a non-static |
1318 | | // member function, q_1 is an lvalue that denotes *this; any other q_i is an |
1319 | | // lvalue that denotes the parameter copy corresponding to p_i. |
1320 | | |
1321 | 706 | FunctionDecl *OperatorNew = nullptr; |
1322 | 706 | FunctionDecl *OperatorDelete = nullptr; |
1323 | 706 | FunctionDecl *UnusedResult = nullptr; |
1324 | 706 | bool PassAlignment = false; |
1325 | 706 | SmallVector<Expr *, 1> PlacementArgs; |
1326 | | |
1327 | 706 | bool PromiseContainsNew = [this, &PromiseType]() -> bool { |
1328 | 706 | DeclarationName NewName = |
1329 | 706 | S.getASTContext().DeclarationNames.getCXXOperatorName(OO_New); |
1330 | 706 | LookupResult R(S, NewName, Loc, Sema::LookupOrdinaryName); |
1331 | | |
1332 | 706 | if (PromiseType->isRecordType()) |
1333 | 706 | S.LookupQualifiedName(R, PromiseType->getAsCXXRecordDecl()); |
1334 | | |
1335 | 706 | return !R.empty() && !R.isAmbiguous()31 ; |
1336 | 706 | }(); |
1337 | | |
1338 | 715 | auto LookupAllocationFunction = [&]() { |
1339 | | // [dcl.fct.def.coroutine]p9 |
1340 | | // The allocation function's name is looked up by searching for it in the |
1341 | | // scope of the promise type. |
1342 | | // - If any declarations are found, ... |
1343 | | // - If no declarations are found in the scope of the promise type, a search |
1344 | | // is performed in the global scope. |
1345 | 715 | Sema::AllocationFunctionScope NewScope = |
1346 | 715 | PromiseContainsNew ? Sema::AFS_Class39 : Sema::AFS_Global676 ; |
1347 | 715 | S.FindAllocationFunctions(Loc, SourceRange(), |
1348 | 715 | NewScope, |
1349 | 715 | /*DeleteScope*/ Sema::AFS_Both, PromiseType, |
1350 | 715 | /*isArray*/ false, PassAlignment, PlacementArgs, |
1351 | 715 | OperatorNew, UnusedResult, /*Diagnose*/ false); |
1352 | 715 | }; |
1353 | | |
1354 | | // We don't expect to call to global operator new with (size, p0, …, pn). |
1355 | | // So if we choose to lookup the allocation function in global scope, we |
1356 | | // shouldn't lookup placement arguments. |
1357 | 706 | if (PromiseContainsNew && !collectPlacementArgs(S, FD, Loc, PlacementArgs)30 ) |
1358 | 0 | return false; |
1359 | | |
1360 | 706 | LookupAllocationFunction(); |
1361 | | |
1362 | | // [dcl.fct.def.coroutine]p9 |
1363 | | // If no viable function is found ([over.match.viable]), overload resolution |
1364 | | // is performed again on a function call created by passing just the amount of |
1365 | | // space required as an argument of type std::size_t. |
1366 | 706 | if (!OperatorNew && !PlacementArgs.empty()10 && PromiseContainsNew9 ) { |
1367 | 9 | PlacementArgs.clear(); |
1368 | 9 | LookupAllocationFunction(); |
1369 | 9 | } |
1370 | | |
1371 | 706 | bool IsGlobalOverload = |
1372 | 706 | OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext())703 ; |
1373 | | // If we didn't find a class-local new declaration and non-throwing new |
1374 | | // was is required then we need to lookup the non-throwing global operator |
1375 | | // instead. |
1376 | 706 | if (RequiresNoThrowAlloc && (39 !OperatorNew39 || IsGlobalOverload39 )) { |
1377 | 24 | auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc); |
1378 | 24 | if (!StdNoThrow) |
1379 | 0 | return false; |
1380 | 24 | PlacementArgs = {StdNoThrow}; |
1381 | 24 | OperatorNew = nullptr; |
1382 | 24 | S.FindAllocationFunctions(Loc, SourceRange(), /*NewScope*/ Sema::AFS_Both, |
1383 | 24 | /*DeleteScope*/ Sema::AFS_Both, PromiseType, |
1384 | 24 | /*isArray*/ false, PassAlignment, PlacementArgs, |
1385 | 24 | OperatorNew, UnusedResult); |
1386 | 24 | } |
1387 | | |
1388 | 706 | if (!OperatorNew) { |
1389 | 3 | if (PromiseContainsNew) |
1390 | 3 | S.Diag(Loc, diag::err_coroutine_unusable_new) << PromiseType << &FD; |
1391 | | |
1392 | 3 | return false; |
1393 | 3 | } |
1394 | | |
1395 | 703 | if (RequiresNoThrowAlloc) { |
1396 | 39 | const auto *FT = OperatorNew->getType()->castAs<FunctionProtoType>(); |
1397 | 39 | if (!FT->isNothrow(/*ResultIfDependent*/ false)) { |
1398 | 10 | S.Diag(OperatorNew->getLocation(), |
1399 | 10 | diag::err_coroutine_promise_new_requires_nothrow) |
1400 | 10 | << OperatorNew; |
1401 | 10 | S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required) |
1402 | 10 | << OperatorNew; |
1403 | 10 | return false; |
1404 | 10 | } |
1405 | 39 | } |
1406 | | |
1407 | 693 | if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr) { |
1408 | | // FIXME: We should add an error here. According to: |
1409 | | // [dcl.fct.def.coroutine]p12 |
1410 | | // If no usual deallocation function is found, the program is ill-formed. |
1411 | 0 | return false; |
1412 | 0 | } |
1413 | | |
1414 | 693 | Expr *FramePtr = |
1415 | 693 | S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_frame, {}); |
1416 | | |
1417 | 693 | Expr *FrameSize = |
1418 | 693 | S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_size, {}); |
1419 | | |
1420 | | // Make new call. |
1421 | | |
1422 | 693 | ExprResult NewRef = |
1423 | 693 | S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc); |
1424 | 693 | if (NewRef.isInvalid()) |
1425 | 0 | return false; |
1426 | | |
1427 | 693 | SmallVector<Expr *, 2> NewArgs(1, FrameSize); |
1428 | 693 | llvm::append_range(NewArgs, PlacementArgs); |
1429 | | |
1430 | 693 | ExprResult NewExpr = |
1431 | 693 | S.BuildCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc); |
1432 | 693 | NewExpr = S.ActOnFinishFullExpr(NewExpr.get(), /*DiscardedValue*/ false); |
1433 | 693 | if (NewExpr.isInvalid()) |
1434 | 0 | return false; |
1435 | | |
1436 | | // Make delete call. |
1437 | | |
1438 | 693 | QualType OpDeleteQualType = OperatorDelete->getType(); |
1439 | | |
1440 | 693 | ExprResult DeleteRef = |
1441 | 693 | S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc); |
1442 | 693 | if (DeleteRef.isInvalid()) |
1443 | 0 | return false; |
1444 | | |
1445 | 693 | Expr *CoroFree = |
1446 | 693 | S.BuildBuiltinCallExpr(Loc, Builtin::BI__builtin_coro_free, {FramePtr}); |
1447 | | |
1448 | 693 | SmallVector<Expr *, 2> DeleteArgs{CoroFree}; |
1449 | | |
1450 | | // [dcl.fct.def.coroutine]p12 |
1451 | | // The selected deallocation function shall be called with the address of |
1452 | | // the block of storage to be reclaimed as its first argument. If a |
1453 | | // deallocation function with a parameter of type std::size_t is |
1454 | | // used, the size of the block is passed as the corresponding argument. |
1455 | 693 | const auto *OpDeleteType = |
1456 | 693 | OpDeleteQualType.getTypePtr()->castAs<FunctionProtoType>(); |
1457 | 693 | if (OpDeleteType->getNumParams() > 1) |
1458 | 2 | DeleteArgs.push_back(FrameSize); |
1459 | | |
1460 | 693 | ExprResult DeleteExpr = |
1461 | 693 | S.BuildCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc); |
1462 | 693 | DeleteExpr = |
1463 | 693 | S.ActOnFinishFullExpr(DeleteExpr.get(), /*DiscardedValue*/ false); |
1464 | 693 | if (DeleteExpr.isInvalid()) |
1465 | 0 | return false; |
1466 | | |
1467 | 693 | this->Allocate = NewExpr.get(); |
1468 | 693 | this->Deallocate = DeleteExpr.get(); |
1469 | | |
1470 | 693 | return true; |
1471 | 693 | } |
1472 | | |
1473 | 741 | bool CoroutineStmtBuilder::makeOnFallthrough() { |
1474 | 741 | assert(!IsPromiseDependentType && |
1475 | 741 | "cannot make statement while the promise type is dependent"); |
1476 | | |
1477 | | // [dcl.fct.def.coroutine]/p6 |
1478 | | // If searches for the names return_void and return_value in the scope of |
1479 | | // the promise type each find any declarations, the program is ill-formed. |
1480 | | // [Note 1: If return_void is found, flowing off the end of a coroutine is |
1481 | | // equivalent to a co_return with no operand. Otherwise, flowing off the end |
1482 | | // of a coroutine results in undefined behavior ([stmt.return.coroutine]). — |
1483 | | // end note] |
1484 | 0 | bool HasRVoid, HasRValue; |
1485 | 741 | LookupResult LRVoid = |
1486 | 741 | lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid); |
1487 | 741 | LookupResult LRValue = |
1488 | 741 | lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue); |
1489 | | |
1490 | 741 | StmtResult Fallthrough; |
1491 | 741 | if (HasRVoid && HasRValue498 ) { |
1492 | | // FIXME Improve this diagnostic |
1493 | 10 | S.Diag(FD.getLocation(), |
1494 | 10 | diag::err_coroutine_promise_incompatible_return_functions) |
1495 | 10 | << PromiseRecordDecl; |
1496 | 10 | S.Diag(LRVoid.getRepresentativeDecl()->getLocation(), |
1497 | 10 | diag::note_member_first_declared_here) |
1498 | 10 | << LRVoid.getLookupName(); |
1499 | 10 | S.Diag(LRValue.getRepresentativeDecl()->getLocation(), |
1500 | 10 | diag::note_member_first_declared_here) |
1501 | 10 | << LRValue.getLookupName(); |
1502 | 10 | return false; |
1503 | 731 | } else if (!HasRVoid && !HasRValue243 ) { |
1504 | | // We need to set 'Fallthrough'. Otherwise the other analysis part might |
1505 | | // think the coroutine has defined a return_value method. So it might emit |
1506 | | // **false** positive warning. e.g., |
1507 | | // |
1508 | | // promise_without_return_func foo() { |
1509 | | // co_await something(); |
1510 | | // } |
1511 | | // |
1512 | | // Then AnalysisBasedWarning would emit a warning about `foo()` lacking a |
1513 | | // co_return statements, which isn't correct. |
1514 | 6 | Fallthrough = S.ActOnNullStmt(PromiseRecordDecl->getLocation()); |
1515 | 6 | if (Fallthrough.isInvalid()) |
1516 | 0 | return false; |
1517 | 725 | } else if (HasRVoid) { |
1518 | 488 | Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr, |
1519 | 488 | /*IsImplicit*/false); |
1520 | 488 | Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get()); |
1521 | 488 | if (Fallthrough.isInvalid()) |
1522 | 0 | return false; |
1523 | 488 | } |
1524 | | |
1525 | 731 | this->OnFallthrough = Fallthrough.get(); |
1526 | 731 | return true; |
1527 | 741 | } |
1528 | | |
1529 | 753 | bool CoroutineStmtBuilder::makeOnException() { |
1530 | | // Try to form 'p.unhandled_exception();' |
1531 | 753 | assert(!IsPromiseDependentType && |
1532 | 753 | "cannot make statement while the promise type is dependent"); |
1533 | | |
1534 | 0 | const bool RequireUnhandledException = S.getLangOpts().CXXExceptions; |
1535 | | |
1536 | 753 | if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) { |
1537 | 55 | auto DiagID = |
1538 | 55 | RequireUnhandledException |
1539 | 55 | ? diag::err_coroutine_promise_unhandled_exception_required10 |
1540 | 55 | : diag:: |
1541 | 45 | warn_coroutine_promise_unhandled_exception_required_with_exceptions; |
1542 | 55 | S.Diag(Loc, DiagID) << PromiseRecordDecl; |
1543 | 55 | S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here) |
1544 | 55 | << PromiseRecordDecl; |
1545 | 55 | return !RequireUnhandledException; |
1546 | 55 | } |
1547 | | |
1548 | | // If exceptions are disabled, don't try to build OnException. |
1549 | 698 | if (!S.getLangOpts().CXXExceptions) |
1550 | 146 | return true; |
1551 | | |
1552 | 552 | ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc, |
1553 | 552 | "unhandled_exception", None); |
1554 | 552 | UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc, |
1555 | 552 | /*DiscardedValue*/ false); |
1556 | 552 | if (UnhandledException.isInvalid()) |
1557 | 0 | return false; |
1558 | | |
1559 | | // Since the body of the coroutine will be wrapped in try-catch, it will |
1560 | | // be incompatible with SEH __try if present in a function. |
1561 | 552 | if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) { |
1562 | 2 | S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions); |
1563 | 2 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
1564 | 2 | << Fn.getFirstCoroutineStmtKeyword(); |
1565 | 2 | return false; |
1566 | 2 | } |
1567 | | |
1568 | 550 | this->OnException = UnhandledException.get(); |
1569 | 550 | return true; |
1570 | 552 | } |
1571 | | |
1572 | 802 | bool CoroutineStmtBuilder::makeReturnObject() { |
1573 | | // [dcl.fct.def.coroutine]p7 |
1574 | | // The expression promise.get_return_object() is used to initialize the |
1575 | | // returned reference or prvalue result object of a call to a coroutine. |
1576 | 802 | ExprResult ReturnObject = |
1577 | 802 | buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None); |
1578 | 802 | if (ReturnObject.isInvalid()) |
1579 | 5 | return false; |
1580 | | |
1581 | 797 | this->ReturnValue = ReturnObject.get(); |
1582 | 797 | return true; |
1583 | 802 | } |
1584 | | |
1585 | 10 | static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) { |
1586 | 10 | if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) { |
1587 | 10 | auto *MethodDecl = MbrRef->getMethodDecl(); |
1588 | 10 | S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here) |
1589 | 10 | << MethodDecl; |
1590 | 10 | } |
1591 | 10 | S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here) |
1592 | 10 | << Fn.getFirstCoroutineStmtKeyword(); |
1593 | 10 | } |
1594 | | |
1595 | 731 | bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() { |
1596 | 731 | assert(!IsPromiseDependentType && |
1597 | 731 | "cannot make statement while the promise type is dependent"); |
1598 | 0 | assert(this->ReturnValue && "ReturnValue must be already formed"); |
1599 | | |
1600 | 0 | QualType const GroType = this->ReturnValue->getType(); |
1601 | 731 | assert(!GroType->isDependentType() && |
1602 | 731 | "get_return_object type must no longer be dependent"); |
1603 | | |
1604 | 0 | QualType const FnRetType = FD.getReturnType(); |
1605 | 731 | assert(!FnRetType->isDependentType() && |
1606 | 731 | "get_return_object type must no longer be dependent"); |
1607 | | |
1608 | 731 | if (FnRetType->isVoidType()) { |
1609 | 261 | ExprResult Res = |
1610 | 261 | S.ActOnFinishFullExpr(this->ReturnValue, Loc, /*DiscardedValue*/ false); |
1611 | 261 | if (Res.isInvalid()) |
1612 | 0 | return false; |
1613 | | |
1614 | 261 | return true; |
1615 | 261 | } |
1616 | | |
1617 | 470 | if (GroType->isVoidType()) { |
1618 | | // Trigger a nice error message. |
1619 | 5 | InitializedEntity Entity = |
1620 | 5 | InitializedEntity::InitializeResult(Loc, FnRetType); |
1621 | 5 | S.PerformCopyInitialization(Entity, SourceLocation(), ReturnValue); |
1622 | 5 | noteMemberDeclaredHere(S, ReturnValue, Fn); |
1623 | 5 | return false; |
1624 | 5 | } |
1625 | | |
1626 | 465 | StmtResult ReturnStmt = S.BuildReturnStmt(Loc, ReturnValue); |
1627 | 465 | if (ReturnStmt.isInvalid()) { |
1628 | 5 | noteMemberDeclaredHere(S, ReturnValue, Fn); |
1629 | 5 | return false; |
1630 | 5 | } |
1631 | | |
1632 | 460 | this->ReturnStmt = ReturnStmt.get(); |
1633 | 460 | return true; |
1634 | 465 | } |
1635 | | |
1636 | | // Create a static_cast\<T&&>(expr). |
1637 | 228 | static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) { |
1638 | 228 | if (T.isNull()) |
1639 | 228 | T = E->getType(); |
1640 | 228 | QualType TargetType = S.BuildReferenceType( |
1641 | 228 | T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName()); |
1642 | 228 | SourceLocation ExprLoc = E->getBeginLoc(); |
1643 | 228 | TypeSourceInfo *TargetLoc = |
1644 | 228 | S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc); |
1645 | | |
1646 | 228 | return S |
1647 | 228 | .BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E, |
1648 | 228 | SourceRange(ExprLoc, ExprLoc), E->getSourceRange()) |
1649 | 228 | .get(); |
1650 | 228 | } |
1651 | | |
1652 | | /// Build a variable declaration for move parameter. |
1653 | | static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type, |
1654 | 707 | IdentifierInfo *II) { |
1655 | 707 | TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc); |
1656 | 707 | VarDecl *Decl = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type, |
1657 | 707 | TInfo, SC_None); |
1658 | 707 | Decl->setImplicit(); |
1659 | 707 | return Decl; |
1660 | 707 | } |
1661 | | |
1662 | | // Build statements that move coroutine function parameters to the coroutine |
1663 | | // frame, and store them on the function scope info. |
1664 | 1.16k | bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) { |
1665 | 1.16k | assert(isa<FunctionDecl>(CurContext) && "not in a function scope"); |
1666 | 0 | auto *FD = cast<FunctionDecl>(CurContext); |
1667 | | |
1668 | 1.16k | auto *ScopeInfo = getCurFunction(); |
1669 | 1.16k | if (!ScopeInfo->CoroutineParameterMoves.empty()) |
1670 | 5 | return false; |
1671 | | |
1672 | | // [dcl.fct.def.coroutine]p13 |
1673 | | // When a coroutine is invoked, after initializing its parameters |
1674 | | // ([expr.call]), a copy is created for each coroutine parameter. For a |
1675 | | // parameter of type cv T, the copy is a variable of type cv T with |
1676 | | // automatic storage duration that is direct-initialized from an xvalue of |
1677 | | // type T referring to the parameter. |
1678 | 1.15k | for (auto *PD : FD->parameters()) { |
1679 | 879 | if (PD->getType()->isDependentType()) |
1680 | 172 | continue; |
1681 | | |
1682 | 707 | ExprResult PDRefExpr = |
1683 | 707 | BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(), |
1684 | 707 | ExprValueKind::VK_LValue, Loc); // FIXME: scope? |
1685 | 707 | if (PDRefExpr.isInvalid()) |
1686 | 0 | return false; |
1687 | | |
1688 | 707 | Expr *CExpr = nullptr; |
1689 | 707 | if (PD->getType()->getAsCXXRecordDecl() || |
1690 | 707 | PD->getType()->isRValueReferenceType()524 ) |
1691 | 228 | CExpr = castForMoving(*this, PDRefExpr.get()); |
1692 | 479 | else |
1693 | 479 | CExpr = PDRefExpr.get(); |
1694 | | // [dcl.fct.def.coroutine]p13 |
1695 | | // The initialization and destruction of each parameter copy occurs in the |
1696 | | // context of the called coroutine. |
1697 | 707 | auto D = buildVarDecl(*this, Loc, PD->getType(), PD->getIdentifier()); |
1698 | 707 | AddInitializerToDecl(D, CExpr, /*DirectInit=*/true); |
1699 | | |
1700 | | // Convert decl to a statement. |
1701 | 707 | StmtResult Stmt = ActOnDeclStmt(ConvertDeclToDeclGroup(D), Loc, Loc); |
1702 | 707 | if (Stmt.isInvalid()) |
1703 | 0 | return false; |
1704 | | |
1705 | 707 | ScopeInfo->CoroutineParameterMoves.insert(std::make_pair(PD, Stmt.get())); |
1706 | 707 | } |
1707 | 1.15k | return true; |
1708 | 1.15k | } |
1709 | | |
1710 | 193 | StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) { |
1711 | 193 | CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args); |
1712 | 193 | if (!Res) |
1713 | 0 | return StmtError(); |
1714 | 193 | return Res; |
1715 | 193 | } |
1716 | | |
1717 | | ClassTemplateDecl *Sema::lookupCoroutineTraits(SourceLocation KwLoc, |
1718 | | SourceLocation FuncLoc, |
1719 | 929 | NamespaceDecl *&Namespace) { |
1720 | 929 | if (!StdCoroutineTraitsCache) { |
1721 | | // Because coroutines moved from std::experimental in the TS to std in |
1722 | | // C++20, we look in both places to give users time to transition their |
1723 | | // TS-specific code to C++20. Diagnostics are given when the TS usage is |
1724 | | // discovered. |
1725 | | // TODO: Become stricter when <experimental/coroutine> is removed. |
1726 | | |
1727 | 129 | auto const &TraitIdent = PP.getIdentifierTable().get("coroutine_traits"); |
1728 | | |
1729 | 129 | NamespaceDecl *StdSpace = getStdNamespace(); |
1730 | 129 | LookupResult ResStd(*this, &TraitIdent, FuncLoc, LookupOrdinaryName); |
1731 | 129 | bool InStd = StdSpace && LookupQualifiedName(ResStd, StdSpace)109 ; |
1732 | | |
1733 | 129 | NamespaceDecl *ExpSpace = lookupStdExperimentalNamespace(); |
1734 | 129 | LookupResult ResExp(*this, &TraitIdent, FuncLoc, LookupOrdinaryName); |
1735 | 129 | bool InExp = ExpSpace && LookupQualifiedName(ResExp, ExpSpace)47 ; |
1736 | | |
1737 | 129 | if (!InStd && !InExp63 ) { |
1738 | | // The goggles, they found nothing! |
1739 | 20 | Diag(KwLoc, diag::err_implied_coroutine_type_not_found) |
1740 | 20 | << "std::coroutine_traits"; |
1741 | 20 | return nullptr; |
1742 | 20 | } |
1743 | | |
1744 | | // Prefer ::std to std::experimental. |
1745 | 109 | auto &Result = InStd ? ResStd66 : ResExp43 ; |
1746 | 109 | CoroTraitsNamespaceCache = InStd ? StdSpace66 : ExpSpace43 ; |
1747 | | |
1748 | | // coroutine_traits is required to be a class template. |
1749 | 109 | StdCoroutineTraitsCache = Result.getAsSingle<ClassTemplateDecl>(); |
1750 | 109 | if (!StdCoroutineTraitsCache) { |
1751 | 0 | Result.suppressDiagnostics(); |
1752 | 0 | NamedDecl *Found = *Result.begin(); |
1753 | 0 | Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits); |
1754 | 0 | return nullptr; |
1755 | 0 | } |
1756 | | |
1757 | 109 | if (InExp) { |
1758 | | // Found in std::experimental |
1759 | 47 | Diag(KwLoc, diag::warn_deprecated_coroutine_namespace) |
1760 | 47 | << "coroutine_traits"; |
1761 | 47 | ResExp.suppressDiagnostics(); |
1762 | 47 | auto *Found = *ResExp.begin(); |
1763 | 47 | Diag(Found->getLocation(), diag::note_entity_declared_at) << Found; |
1764 | | |
1765 | 47 | if (InStd && |
1766 | 47 | StdCoroutineTraitsCache != ResExp.getAsSingle<ClassTemplateDecl>()4 ) { |
1767 | | // Also found something different in std |
1768 | 3 | Diag(KwLoc, |
1769 | 3 | diag::err_mixed_use_std_and_experimental_namespace_for_coroutine); |
1770 | 3 | Diag(StdCoroutineTraitsCache->getLocation(), |
1771 | 3 | diag::note_entity_declared_at) |
1772 | 3 | << StdCoroutineTraitsCache; |
1773 | | |
1774 | 3 | return nullptr; |
1775 | 3 | } |
1776 | 47 | } |
1777 | 109 | } |
1778 | 906 | Namespace = CoroTraitsNamespaceCache; |
1779 | 906 | return StdCoroutineTraitsCache; |
1780 | 929 | } |