Coverage Report

Created: 2023-11-11 10:31

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