Coverage Report

Created: 2020-11-24 06:42

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