Coverage Report

Created: 2021-09-21 08:58

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