Coverage Report

Created: 2022-01-18 06:27

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