Coverage Report

Created: 2021-06-15 06:44

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