2015-10-22 14:13:50 +08:00
|
|
|
|
//===--- SemaCoroutines.cpp - Semantic Analysis for Coroutines ------------===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// This file implements semantic analysis for C++ Coroutines.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
#include "CoroutineStmtBuilder.h"
|
2018-02-16 04:37:22 +08:00
|
|
|
|
#include "clang/AST/ASTLambda.h"
|
2015-10-27 14:02:45 +08:00
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
|
#include "clang/AST/StmtCXX.h"
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2015-11-24 10:34:39 +08:00
|
|
|
|
#include "clang/Sema/Initialization.h"
|
2015-10-27 14:02:45 +08:00
|
|
|
|
#include "clang/Sema/Overload.h"
|
2017-04-04 03:21:00 +08:00
|
|
|
|
#include "clang/Sema/SemaInternal.h"
|
|
|
|
|
|
2015-10-22 14:13:50 +08:00
|
|
|
|
using namespace clang;
|
|
|
|
|
using namespace sema;
|
|
|
|
|
|
2017-05-25 22:59:39 +08:00
|
|
|
|
static LookupResult lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
|
|
|
|
|
SourceLocation Loc, bool &Res) {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
DeclarationName DN = S.PP.getIdentifierInfo(Name);
|
|
|
|
|
LookupResult LR(S, DN, Loc, Sema::LookupMemberName);
|
|
|
|
|
// Suppress diagnostics when a private member is selected. The same warnings
|
|
|
|
|
// will be produced again when building the call.
|
|
|
|
|
LR.suppressDiagnostics();
|
2017-05-25 22:59:39 +08:00
|
|
|
|
Res = S.LookupQualifiedName(LR, RD);
|
|
|
|
|
return LR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool lookupMember(Sema &S, const char *Name, CXXRecordDecl *RD,
|
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
bool Res;
|
|
|
|
|
lookupMember(S, Name, RD, Loc, Res);
|
|
|
|
|
return Res;
|
2017-03-07 07:38:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
/// Look up the std::coroutine_traits<...>::promise_type for the given
|
|
|
|
|
/// function type.
|
2017-07-10 09:27:22 +08:00
|
|
|
|
static QualType lookupPromiseType(Sema &S, const FunctionDecl *FD,
|
|
|
|
|
SourceLocation KwLoc) {
|
|
|
|
|
const FunctionProtoType *FnType = FD->getType()->castAs<FunctionProtoType>();
|
|
|
|
|
const SourceLocation FuncLoc = FD->getLocation();
|
2015-10-27 14:02:45 +08:00
|
|
|
|
// FIXME: Cache std::coroutine_traits once we've found it.
|
2016-10-04 08:31:16 +08:00
|
|
|
|
NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace();
|
|
|
|
|
if (!StdExp) {
|
2017-03-09 11:09:43 +08:00
|
|
|
|
S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
|
|
|
|
|
<< "std::experimental::coroutine_traits";
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return QualType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_traits"),
|
2017-03-07 06:52:28 +08:00
|
|
|
|
FuncLoc, Sema::LookupOrdinaryName);
|
2016-10-04 08:31:16 +08:00
|
|
|
|
if (!S.LookupQualifiedName(Result, StdExp)) {
|
2017-03-09 11:09:43 +08:00
|
|
|
|
S.Diag(KwLoc, diag::err_implied_coroutine_type_not_found)
|
|
|
|
|
<< "std::experimental::coroutine_traits";
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return QualType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClassTemplateDecl *CoroTraits = Result.getAsSingle<ClassTemplateDecl>();
|
|
|
|
|
if (!CoroTraits) {
|
|
|
|
|
Result.suppressDiagnostics();
|
|
|
|
|
// We found something weird. Complain about the first thing we found.
|
|
|
|
|
NamedDecl *Found = *Result.begin();
|
|
|
|
|
S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_traits);
|
|
|
|
|
return QualType();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-10 09:27:22 +08:00
|
|
|
|
// Form template argument list for coroutine_traits<R, P1, P2, ...> according
|
|
|
|
|
// to [dcl.fct.def.coroutine]3
|
2017-03-07 06:52:28 +08:00
|
|
|
|
TemplateArgumentListInfo Args(KwLoc, KwLoc);
|
2017-07-10 09:27:22 +08:00
|
|
|
|
auto AddArg = [&](QualType T) {
|
2015-10-27 14:02:45 +08:00
|
|
|
|
Args.addArgument(TemplateArgumentLoc(
|
2017-03-07 06:52:28 +08:00
|
|
|
|
TemplateArgument(T), S.Context.getTrivialTypeSourceInfo(T, KwLoc)));
|
2017-07-10 09:27:22 +08:00
|
|
|
|
};
|
|
|
|
|
AddArg(FnType->getReturnType());
|
|
|
|
|
// If the function is a non-static member function, add the type
|
|
|
|
|
// of the implicit object parameter before the formal parameters.
|
|
|
|
|
if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
|
|
|
|
if (MD->isInstance()) {
|
|
|
|
|
// [over.match.funcs]4
|
|
|
|
|
// For non-static member functions, the type of the implicit object
|
|
|
|
|
// parameter is
|
2017-07-10 10:52:34 +08:00
|
|
|
|
// -- "lvalue reference to cv X" for functions declared without a
|
|
|
|
|
// ref-qualifier or with the & ref-qualifier
|
|
|
|
|
// -- "rvalue reference to cv X" for functions declared with the &&
|
|
|
|
|
// ref-qualifier
|
2017-07-10 09:27:22 +08:00
|
|
|
|
QualType T =
|
|
|
|
|
MD->getThisType(S.Context)->getAs<PointerType>()->getPointeeType();
|
|
|
|
|
T = FnType->getRefQualifier() == RQ_RValue
|
|
|
|
|
? S.Context.getRValueReferenceType(T)
|
|
|
|
|
: S.Context.getLValueReferenceType(T, /*SpelledAsLValue*/ true);
|
|
|
|
|
AddArg(T);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (QualType T : FnType->getParamTypes())
|
|
|
|
|
AddArg(T);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
|
|
|
|
|
// Build the template-id.
|
|
|
|
|
QualType CoroTrait =
|
2017-03-07 06:52:28 +08:00
|
|
|
|
S.CheckTemplateIdType(TemplateName(CoroTraits), KwLoc, Args);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
if (CoroTrait.isNull())
|
|
|
|
|
return QualType();
|
2017-03-07 06:52:28 +08:00
|
|
|
|
if (S.RequireCompleteType(KwLoc, CoroTrait,
|
2017-03-09 11:09:43 +08:00
|
|
|
|
diag::err_coroutine_type_missing_specialization))
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return QualType();
|
|
|
|
|
|
2017-03-07 06:52:28 +08:00
|
|
|
|
auto *RD = CoroTrait->getAsCXXRecordDecl();
|
2015-10-27 14:02:45 +08:00
|
|
|
|
assert(RD && "specialization of class template is not a class?");
|
|
|
|
|
|
|
|
|
|
// Look up the ::promise_type member.
|
2017-03-07 06:52:28 +08:00
|
|
|
|
LookupResult R(S, &S.PP.getIdentifierTable().get("promise_type"), KwLoc,
|
2015-10-27 14:02:45 +08:00
|
|
|
|
Sema::LookupOrdinaryName);
|
|
|
|
|
S.LookupQualifiedName(R, RD);
|
|
|
|
|
auto *Promise = R.getAsSingle<TypeDecl>();
|
|
|
|
|
if (!Promise) {
|
2017-03-07 06:52:28 +08:00
|
|
|
|
S.Diag(FuncLoc,
|
|
|
|
|
diag::err_implied_std_coroutine_traits_promise_type_not_found)
|
2016-10-28 00:28:31 +08:00
|
|
|
|
<< RD;
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return QualType();
|
|
|
|
|
}
|
|
|
|
|
// The promise type is required to be a class type.
|
|
|
|
|
QualType PromiseType = S.Context.getTypeDeclType(Promise);
|
2017-03-07 06:52:28 +08:00
|
|
|
|
|
|
|
|
|
auto buildElaboratedType = [&]() {
|
2016-10-04 08:31:16 +08:00
|
|
|
|
auto *NNS = NestedNameSpecifier::Create(S.Context, nullptr, StdExp);
|
2015-11-19 10:36:35 +08:00
|
|
|
|
NNS = NestedNameSpecifier::Create(S.Context, NNS, false,
|
|
|
|
|
CoroTrait.getTypePtr());
|
2017-03-07 06:52:28 +08:00
|
|
|
|
return S.Context.getElaboratedType(ETK_None, NNS, PromiseType);
|
|
|
|
|
};
|
2015-11-19 10:36:35 +08:00
|
|
|
|
|
2017-03-07 06:52:28 +08:00
|
|
|
|
if (!PromiseType->getAsCXXRecordDecl()) {
|
|
|
|
|
S.Diag(FuncLoc,
|
|
|
|
|
diag::err_implied_std_coroutine_traits_promise_type_not_class)
|
|
|
|
|
<< buildElaboratedType();
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return QualType();
|
|
|
|
|
}
|
2017-03-07 06:52:28 +08:00
|
|
|
|
if (S.RequireCompleteType(FuncLoc, buildElaboratedType(),
|
|
|
|
|
diag::err_coroutine_promise_type_incomplete))
|
|
|
|
|
return QualType();
|
2015-10-27 14:02:45 +08:00
|
|
|
|
|
|
|
|
|
return PromiseType;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 22:34:19 +08:00
|
|
|
|
/// Look up the std::experimental::coroutine_handle<PromiseType>.
|
2017-03-09 11:09:43 +08:00
|
|
|
|
static QualType lookupCoroutineHandleType(Sema &S, QualType PromiseType,
|
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
if (PromiseType.isNull())
|
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
|
|
NamespaceDecl *StdExp = S.lookupStdExperimentalNamespace();
|
|
|
|
|
assert(StdExp && "Should already be diagnosed");
|
|
|
|
|
|
|
|
|
|
LookupResult Result(S, &S.PP.getIdentifierTable().get("coroutine_handle"),
|
|
|
|
|
Loc, Sema::LookupOrdinaryName);
|
|
|
|
|
if (!S.LookupQualifiedName(Result, StdExp)) {
|
|
|
|
|
S.Diag(Loc, diag::err_implied_coroutine_type_not_found)
|
|
|
|
|
<< "std::experimental::coroutine_handle";
|
|
|
|
|
return QualType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClassTemplateDecl *CoroHandle = Result.getAsSingle<ClassTemplateDecl>();
|
|
|
|
|
if (!CoroHandle) {
|
|
|
|
|
Result.suppressDiagnostics();
|
|
|
|
|
// We found something weird. Complain about the first thing we found.
|
|
|
|
|
NamedDecl *Found = *Result.begin();
|
|
|
|
|
S.Diag(Found->getLocation(), diag::err_malformed_std_coroutine_handle);
|
|
|
|
|
return QualType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Form template argument list for coroutine_handle<Promise>.
|
|
|
|
|
TemplateArgumentListInfo Args(Loc, Loc);
|
|
|
|
|
Args.addArgument(TemplateArgumentLoc(
|
|
|
|
|
TemplateArgument(PromiseType),
|
|
|
|
|
S.Context.getTrivialTypeSourceInfo(PromiseType, Loc)));
|
|
|
|
|
|
|
|
|
|
// Build the template-id.
|
|
|
|
|
QualType CoroHandleType =
|
|
|
|
|
S.CheckTemplateIdType(TemplateName(CoroHandle), Loc, Args);
|
|
|
|
|
if (CoroHandleType.isNull())
|
|
|
|
|
return QualType();
|
|
|
|
|
if (S.RequireCompleteType(Loc, CoroHandleType,
|
|
|
|
|
diag::err_coroutine_type_missing_specialization))
|
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
|
|
return CoroHandleType;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 02:43:28 +08:00
|
|
|
|
static bool isValidCoroutineContext(Sema &S, SourceLocation Loc,
|
|
|
|
|
StringRef Keyword) {
|
2015-11-20 10:54:01 +08:00
|
|
|
|
// 'co_await' and 'co_yield' are not permitted in unevaluated operands.
|
|
|
|
|
if (S.isUnevaluatedContext()) {
|
|
|
|
|
S.Diag(Loc, diag::err_coroutine_unevaluated_context) << Keyword;
|
2016-10-28 02:43:28 +08:00
|
|
|
|
return false;
|
2015-11-20 10:54:01 +08:00
|
|
|
|
}
|
2015-10-22 14:13:50 +08:00
|
|
|
|
|
|
|
|
|
// Any other usage must be within a function.
|
|
|
|
|
auto *FD = dyn_cast<FunctionDecl>(S.CurContext);
|
|
|
|
|
if (!FD) {
|
|
|
|
|
S.Diag(Loc, isa<ObjCMethodDecl>(S.CurContext)
|
|
|
|
|
? diag::err_coroutine_objc_method
|
|
|
|
|
: diag::err_coroutine_outside_function) << Keyword;
|
2016-10-28 02:43:28 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// An enumeration for mapping the diagnostic type to the correct diagnostic
|
|
|
|
|
// selection index.
|
|
|
|
|
enum InvalidFuncDiag {
|
|
|
|
|
DiagCtor = 0,
|
|
|
|
|
DiagDtor,
|
|
|
|
|
DiagCopyAssign,
|
|
|
|
|
DiagMoveAssign,
|
|
|
|
|
DiagMain,
|
|
|
|
|
DiagConstexpr,
|
|
|
|
|
DiagAutoRet,
|
|
|
|
|
DiagVarargs,
|
|
|
|
|
};
|
|
|
|
|
bool Diagnosed = false;
|
|
|
|
|
auto DiagInvalid = [&](InvalidFuncDiag ID) {
|
|
|
|
|
S.Diag(Loc, diag::err_coroutine_invalid_func_context) << ID << Keyword;
|
|
|
|
|
Diagnosed = true;
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Diagnose when a constructor, destructor, copy/move assignment operator,
|
|
|
|
|
// or the function 'main' are declared as a coroutine.
|
|
|
|
|
auto *MD = dyn_cast<CXXMethodDecl>(FD);
|
|
|
|
|
if (MD && isa<CXXConstructorDecl>(MD))
|
|
|
|
|
return DiagInvalid(DiagCtor);
|
|
|
|
|
else if (MD && isa<CXXDestructorDecl>(MD))
|
|
|
|
|
return DiagInvalid(DiagDtor);
|
|
|
|
|
else if (MD && MD->isCopyAssignmentOperator())
|
|
|
|
|
return DiagInvalid(DiagCopyAssign);
|
|
|
|
|
else if (MD && MD->isMoveAssignmentOperator())
|
|
|
|
|
return DiagInvalid(DiagMoveAssign);
|
|
|
|
|
else if (FD->isMain())
|
|
|
|
|
return DiagInvalid(DiagMain);
|
|
|
|
|
|
|
|
|
|
// Emit a diagnostics for each of the following conditions which is not met.
|
|
|
|
|
if (FD->isConstexpr())
|
|
|
|
|
DiagInvalid(DiagConstexpr);
|
|
|
|
|
if (FD->getReturnType()->isUndeducedType())
|
|
|
|
|
DiagInvalid(DiagAutoRet);
|
|
|
|
|
if (FD->isVariadic())
|
|
|
|
|
DiagInvalid(DiagVarargs);
|
|
|
|
|
|
|
|
|
|
return !Diagnosed;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
static ExprResult buildOperatorCoawaitLookupExpr(Sema &SemaRef, Scope *S,
|
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
DeclarationName OpName =
|
|
|
|
|
SemaRef.Context.DeclarationNames.getCXXOperatorName(OO_Coawait);
|
|
|
|
|
LookupResult Operators(SemaRef, OpName, SourceLocation(),
|
|
|
|
|
Sema::LookupOperatorName);
|
|
|
|
|
SemaRef.LookupName(Operators, S);
|
|
|
|
|
|
|
|
|
|
assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
|
|
|
|
|
const auto &Functions = Operators.asUnresolvedSet();
|
|
|
|
|
bool IsOverloaded =
|
|
|
|
|
Functions.size() > 1 ||
|
|
|
|
|
(Functions.size() == 1 && isa<FunctionTemplateDecl>(*Functions.begin()));
|
|
|
|
|
Expr *CoawaitOp = UnresolvedLookupExpr::Create(
|
|
|
|
|
SemaRef.Context, /*NamingClass*/ nullptr, NestedNameSpecifierLoc(),
|
|
|
|
|
DeclarationNameInfo(OpName, Loc), /*RequiresADL*/ true, IsOverloaded,
|
|
|
|
|
Functions.begin(), Functions.end());
|
|
|
|
|
assert(CoawaitOp);
|
|
|
|
|
return CoawaitOp;
|
|
|
|
|
}
|
2016-10-28 02:43:28 +08:00
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
/// Build a call to 'operator co_await' if there is a suitable operator for
|
|
|
|
|
/// the given expression.
|
|
|
|
|
static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, SourceLocation Loc,
|
|
|
|
|
Expr *E,
|
|
|
|
|
UnresolvedLookupExpr *Lookup) {
|
|
|
|
|
UnresolvedSet<16> Functions;
|
|
|
|
|
Functions.append(Lookup->decls_begin(), Lookup->decls_end());
|
|
|
|
|
return SemaRef.CreateOverloadedUnaryOp(Loc, UO_Coawait, Functions, E);
|
|
|
|
|
}
|
2015-10-22 14:13:50 +08:00
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
static ExprResult buildOperatorCoawaitCall(Sema &SemaRef, Scope *S,
|
|
|
|
|
SourceLocation Loc, Expr *E) {
|
|
|
|
|
ExprResult R = buildOperatorCoawaitLookupExpr(SemaRef, S, Loc);
|
|
|
|
|
if (R.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
return buildOperatorCoawaitCall(SemaRef, Loc, E,
|
|
|
|
|
cast<UnresolvedLookupExpr>(R.get()));
|
2015-10-22 14:13:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 00:28:31 +08:00
|
|
|
|
static Expr *buildBuiltinCall(Sema &S, SourceLocation Loc, Builtin::ID Id,
|
2017-03-07 07:38:15 +08:00
|
|
|
|
MultiExprArg CallArgs) {
|
2016-10-28 00:28:31 +08:00
|
|
|
|
StringRef Name = S.Context.BuiltinInfo.getName(Id);
|
|
|
|
|
LookupResult R(S, &S.Context.Idents.get(Name), Loc, Sema::LookupOrdinaryName);
|
|
|
|
|
S.LookupName(R, S.TUScope, /*AllowBuiltinCreation=*/true);
|
|
|
|
|
|
|
|
|
|
auto *BuiltInDecl = R.getAsSingle<FunctionDecl>();
|
|
|
|
|
assert(BuiltInDecl && "failed to find builtin declaration");
|
|
|
|
|
|
|
|
|
|
ExprResult DeclRef =
|
|
|
|
|
S.BuildDeclRefExpr(BuiltInDecl, BuiltInDecl->getType(), VK_LValue, Loc);
|
|
|
|
|
assert(DeclRef.isUsable() && "Builtin reference cannot fail");
|
|
|
|
|
|
|
|
|
|
ExprResult Call =
|
|
|
|
|
S.ActOnCallExpr(/*Scope=*/nullptr, DeclRef.get(), Loc, CallArgs, Loc);
|
|
|
|
|
|
|
|
|
|
assert(!Call.isInvalid() && "Call to builtin cannot fail!");
|
|
|
|
|
return Call.get();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 11:09:43 +08:00
|
|
|
|
static ExprResult buildCoroutineHandle(Sema &S, QualType PromiseType,
|
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
QualType CoroHandleType = lookupCoroutineHandleType(S, PromiseType, Loc);
|
|
|
|
|
if (CoroHandleType.isNull())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
|
|
DeclContext *LookupCtx = S.computeDeclContext(CoroHandleType);
|
|
|
|
|
LookupResult Found(S, &S.PP.getIdentifierTable().get("from_address"), Loc,
|
|
|
|
|
Sema::LookupOrdinaryName);
|
|
|
|
|
if (!S.LookupQualifiedName(Found, LookupCtx)) {
|
|
|
|
|
S.Diag(Loc, diag::err_coroutine_handle_missing_member)
|
|
|
|
|
<< "from_address";
|
|
|
|
|
return ExprError();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Expr *FramePtr =
|
|
|
|
|
buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
|
|
|
|
|
|
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
|
ExprResult FromAddr =
|
|
|
|
|
S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
|
|
|
|
|
if (FromAddr.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
|
|
return S.ActOnCallExpr(nullptr, FromAddr.get(), Loc, FramePtr, Loc);
|
|
|
|
|
}
|
2015-10-27 14:02:45 +08:00
|
|
|
|
|
|
|
|
|
struct ReadySuspendResumeResult {
|
2017-05-29 02:21:12 +08:00
|
|
|
|
enum AwaitCallType { ACT_Ready, ACT_Suspend, ACT_Resume };
|
2015-10-27 14:02:45 +08:00
|
|
|
|
Expr *Results[3];
|
2017-03-11 09:30:17 +08:00
|
|
|
|
OpaqueValueExpr *OpaqueValue;
|
|
|
|
|
bool IsInvalid;
|
2015-10-27 14:02:45 +08:00
|
|
|
|
};
|
|
|
|
|
|
2015-11-21 06:40:06 +08:00
|
|
|
|
static ExprResult buildMemberCall(Sema &S, Expr *Base, SourceLocation Loc,
|
2017-03-07 07:38:15 +08:00
|
|
|
|
StringRef Name, MultiExprArg Args) {
|
2015-11-21 06:40:06 +08:00
|
|
|
|
DeclarationNameInfo NameInfo(&S.PP.getIdentifierTable().get(Name), Loc);
|
|
|
|
|
|
|
|
|
|
// FIXME: Fix BuildMemberReferenceExpr to take a const CXXScopeSpec&.
|
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
|
ExprResult Result = S.BuildMemberReferenceExpr(
|
|
|
|
|
Base, Base->getType(), Loc, /*IsPtr=*/false, SS,
|
|
|
|
|
SourceLocation(), nullptr, NameInfo, /*TemplateArgs=*/nullptr,
|
|
|
|
|
/*Scope=*/nullptr);
|
|
|
|
|
if (Result.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
|
|
return S.ActOnCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 12:46:54 +08:00
|
|
|
|
// See if return type is coroutine-handle and if so, invoke builtin coro-resume
|
|
|
|
|
// on its address. This is to enable experimental support for coroutine-handle
|
|
|
|
|
// returning await_suspend that results in a guranteed tail call to the target
|
|
|
|
|
// coroutine.
|
|
|
|
|
static Expr *maybeTailCall(Sema &S, QualType RetType, Expr *E,
|
|
|
|
|
SourceLocation Loc) {
|
|
|
|
|
if (RetType->isReferenceType())
|
|
|
|
|
return nullptr;
|
|
|
|
|
Type const *T = RetType.getTypePtr();
|
|
|
|
|
if (!T->isClassType() && !T->isStructureType())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
// FIXME: Add convertability check to coroutine_handle<>. Possibly via
|
|
|
|
|
// EvaluateBinaryTypeTrait(BTT_IsConvertible, ...) which is at the moment
|
|
|
|
|
// a private function in SemaExprCXX.cpp
|
|
|
|
|
|
|
|
|
|
ExprResult AddressExpr = buildMemberCall(S, E, Loc, "address", None);
|
|
|
|
|
if (AddressExpr.isInvalid())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
Expr *JustAddress = AddressExpr.get();
|
|
|
|
|
// FIXME: Check that the type of AddressExpr is void*
|
|
|
|
|
return buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_resume,
|
|
|
|
|
JustAddress);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
/// Build calls to await_ready, await_suspend, and await_resume for a co_await
|
|
|
|
|
/// expression.
|
2017-03-09 11:09:43 +08:00
|
|
|
|
static ReadySuspendResumeResult buildCoawaitCalls(Sema &S, VarDecl *CoroPromise,
|
|
|
|
|
SourceLocation Loc, Expr *E) {
|
2017-03-11 09:30:17 +08:00
|
|
|
|
OpaqueValueExpr *Operand = new (S.Context)
|
|
|
|
|
OpaqueValueExpr(Loc, E->getType(), VK_LValue, E->getObjectKind(), E);
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
// Assume invalid until we see otherwise.
|
2017-03-11 09:30:17 +08:00
|
|
|
|
ReadySuspendResumeResult Calls = {{}, Operand, /*IsInvalid=*/true};
|
2015-10-27 14:02:45 +08:00
|
|
|
|
|
2017-03-09 11:09:43 +08:00
|
|
|
|
ExprResult CoroHandleRes = buildCoroutineHandle(S, CoroPromise->getType(), Loc);
|
|
|
|
|
if (CoroHandleRes.isInvalid())
|
|
|
|
|
return Calls;
|
|
|
|
|
Expr *CoroHandle = CoroHandleRes.get();
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
const StringRef Funcs[] = {"await_ready", "await_suspend", "await_resume"};
|
2017-03-09 11:09:43 +08:00
|
|
|
|
MultiExprArg Args[] = {None, CoroHandle, None};
|
2015-10-27 14:02:45 +08:00
|
|
|
|
for (size_t I = 0, N = llvm::array_lengthof(Funcs); I != N; ++I) {
|
2017-03-09 11:09:43 +08:00
|
|
|
|
ExprResult Result = buildMemberCall(S, Operand, Loc, Funcs[I], Args[I]);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
if (Result.isInvalid())
|
|
|
|
|
return Calls;
|
|
|
|
|
Calls.Results[I] = Result.get();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-29 02:21:12 +08:00
|
|
|
|
// Assume the calls are valid; all further checking should make them invalid.
|
2015-10-27 14:02:45 +08:00
|
|
|
|
Calls.IsInvalid = false;
|
2017-05-29 02:21:12 +08:00
|
|
|
|
|
|
|
|
|
using ACT = ReadySuspendResumeResult::AwaitCallType;
|
|
|
|
|
CallExpr *AwaitReady = cast<CallExpr>(Calls.Results[ACT::ACT_Ready]);
|
|
|
|
|
if (!AwaitReady->getType()->isDependentType()) {
|
|
|
|
|
// [expr.await]p3 [...]
|
|
|
|
|
// — await-ready is the expression e.await_ready(), contextually converted
|
|
|
|
|
// to bool.
|
|
|
|
|
ExprResult Conv = S.PerformContextuallyConvertToBool(AwaitReady);
|
|
|
|
|
if (Conv.isInvalid()) {
|
|
|
|
|
S.Diag(AwaitReady->getDirectCallee()->getLocStart(),
|
|
|
|
|
diag::note_await_ready_no_bool_conversion);
|
|
|
|
|
S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
|
|
|
|
|
<< AwaitReady->getDirectCallee() << E->getSourceRange();
|
|
|
|
|
Calls.IsInvalid = true;
|
|
|
|
|
}
|
|
|
|
|
Calls.Results[ACT::ACT_Ready] = Conv.get();
|
|
|
|
|
}
|
|
|
|
|
CallExpr *AwaitSuspend = cast<CallExpr>(Calls.Results[ACT::ACT_Suspend]);
|
|
|
|
|
if (!AwaitSuspend->getType()->isDependentType()) {
|
|
|
|
|
// [expr.await]p3 [...]
|
|
|
|
|
// - await-suspend is the expression e.await_suspend(h), which shall be
|
|
|
|
|
// a prvalue of type void or bool.
|
2017-06-01 07:41:11 +08:00
|
|
|
|
QualType RetType = AwaitSuspend->getCallReturnType(S.Context);
|
2017-09-06 03:31:52 +08:00
|
|
|
|
|
2017-08-25 12:46:54 +08:00
|
|
|
|
// Experimental support for coroutine_handle returning await_suspend.
|
|
|
|
|
if (Expr *TailCallSuspend = maybeTailCall(S, RetType, AwaitSuspend, Loc))
|
|
|
|
|
Calls.Results[ACT::ACT_Suspend] = TailCallSuspend;
|
|
|
|
|
else {
|
|
|
|
|
// non-class prvalues always have cv-unqualified types
|
|
|
|
|
if (RetType->isReferenceType() ||
|
2017-09-06 03:31:52 +08:00
|
|
|
|
(!RetType->isBooleanType() && !RetType->isVoidType())) {
|
2017-08-25 12:46:54 +08:00
|
|
|
|
S.Diag(AwaitSuspend->getCalleeDecl()->getLocation(),
|
|
|
|
|
diag::err_await_suspend_invalid_return_type)
|
|
|
|
|
<< RetType;
|
|
|
|
|
S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
|
|
|
|
|
<< AwaitSuspend->getDirectCallee();
|
|
|
|
|
Calls.IsInvalid = true;
|
|
|
|
|
}
|
2017-05-29 02:21:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return Calls;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
static ExprResult buildPromiseCall(Sema &S, VarDecl *Promise,
|
|
|
|
|
SourceLocation Loc, StringRef Name,
|
|
|
|
|
MultiExprArg Args) {
|
|
|
|
|
|
|
|
|
|
// Form a reference to the promise.
|
|
|
|
|
ExprResult PromiseRef = S.BuildDeclRefExpr(
|
|
|
|
|
Promise, Promise->getType().getNonReferenceType(), VK_LValue, Loc);
|
|
|
|
|
if (PromiseRef.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
|
|
|
|
return buildMemberCall(S, PromiseRef.get(), Loc, Name, Args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VarDecl *Sema::buildCoroutinePromise(SourceLocation Loc) {
|
|
|
|
|
assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
|
|
|
|
|
auto *FD = cast<FunctionDecl>(CurContext);
|
2017-07-10 09:27:22 +08:00
|
|
|
|
bool IsThisDependentType = [&] {
|
|
|
|
|
if (auto *MD = dyn_cast_or_null<CXXMethodDecl>(FD))
|
|
|
|
|
return MD->isInstance() && MD->getThisType(Context)->isDependentType();
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
QualType T = FD->getType()->isDependentType() || IsThisDependentType
|
|
|
|
|
? Context.DependentTy
|
|
|
|
|
: lookupPromiseType(*this, FD, Loc);
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (T.isNull())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
auto *VD = VarDecl::Create(Context, FD, FD->getLocation(), FD->getLocation(),
|
|
|
|
|
&PP.getIdentifierTable().get("__promise"), T,
|
|
|
|
|
Context.getTrivialTypeSourceInfo(T, Loc), SC_None);
|
|
|
|
|
CheckVariableDeclarationType(VD);
|
|
|
|
|
if (VD->isInvalidDecl())
|
|
|
|
|
return nullptr;
|
2018-01-25 06:15:42 +08:00
|
|
|
|
|
|
|
|
|
auto *ScopeInfo = getCurFunction();
|
|
|
|
|
// Build a list of arguments, based on the coroutine functions arguments,
|
|
|
|
|
// that will be passed to the promise type's constructor.
|
|
|
|
|
llvm::SmallVector<Expr *, 4> CtorArgExprs;
|
|
|
|
|
auto &Moves = ScopeInfo->CoroutineParameterMoves;
|
|
|
|
|
for (auto *PD : FD->parameters()) {
|
|
|
|
|
if (PD->getType()->isDependentType())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
auto RefExpr = ExprEmpty();
|
|
|
|
|
auto Move = Moves.find(PD);
|
2018-02-16 04:37:22 +08:00
|
|
|
|
assert(Move != Moves.end() &&
|
|
|
|
|
"Coroutine function parameter not inserted into move map");
|
|
|
|
|
// If a reference to the function parameter exists in the coroutine
|
|
|
|
|
// frame, use that reference.
|
|
|
|
|
auto *MoveDecl =
|
|
|
|
|
cast<VarDecl>(cast<DeclStmt>(Move->second)->getSingleDecl());
|
|
|
|
|
RefExpr =
|
|
|
|
|
BuildDeclRefExpr(MoveDecl, MoveDecl->getType().getNonReferenceType(),
|
|
|
|
|
ExprValueKind::VK_LValue, FD->getLocation());
|
2018-01-25 06:15:42 +08:00
|
|
|
|
if (RefExpr.isInvalid())
|
|
|
|
|
return nullptr;
|
|
|
|
|
CtorArgExprs.push_back(RefExpr.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create an initialization sequence for the promise type using the
|
|
|
|
|
// constructor arguments, wrapped in a parenthesized list expression.
|
|
|
|
|
Expr *PLE = new (Context) ParenListExpr(Context, FD->getLocation(),
|
|
|
|
|
CtorArgExprs, FD->getLocation());
|
|
|
|
|
InitializedEntity Entity = InitializedEntity::InitializeVariable(VD);
|
|
|
|
|
InitializationKind Kind = InitializationKind::CreateForInit(
|
|
|
|
|
VD->getLocation(), /*DirectInit=*/true, PLE);
|
|
|
|
|
InitializationSequence InitSeq(*this, Entity, Kind, CtorArgExprs,
|
|
|
|
|
/*TopLevelOfInitList=*/false,
|
|
|
|
|
/*TreatUnavailableAsInvalid=*/false);
|
|
|
|
|
|
|
|
|
|
// Attempt to initialize the promise type with the arguments.
|
|
|
|
|
// If that fails, fall back to the promise type's default constructor.
|
|
|
|
|
if (InitSeq) {
|
|
|
|
|
ExprResult Result = InitSeq.Perform(*this, Entity, Kind, CtorArgExprs);
|
|
|
|
|
if (Result.isInvalid()) {
|
|
|
|
|
VD->setInvalidDecl();
|
|
|
|
|
} else if (Result.get()) {
|
|
|
|
|
VD->setInit(MaybeCreateExprWithCleanups(Result.get()));
|
|
|
|
|
VD->setInitStyle(VarDecl::CallInit);
|
|
|
|
|
CheckCompleteVariableDeclaration(VD);
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
ActOnUninitializedDecl(VD);
|
|
|
|
|
|
2017-06-01 03:36:59 +08:00
|
|
|
|
FD->addDecl(VD);
|
2017-03-07 07:38:15 +08:00
|
|
|
|
return VD;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Check that this is a context in which a coroutine suspension can appear.
|
|
|
|
|
static FunctionScopeInfo *checkCoroutineContext(Sema &S, SourceLocation Loc,
|
2017-03-11 10:35:37 +08:00
|
|
|
|
StringRef Keyword,
|
|
|
|
|
bool IsImplicit = false) {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (!isValidCoroutineContext(S, Loc, Keyword))
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
assert(isa<FunctionDecl>(S.CurContext) && "not in a function scope");
|
|
|
|
|
|
|
|
|
|
auto *ScopeInfo = S.getCurFunction();
|
|
|
|
|
assert(ScopeInfo && "missing function scope for function");
|
|
|
|
|
|
2017-03-11 10:35:37 +08:00
|
|
|
|
if (ScopeInfo->FirstCoroutineStmtLoc.isInvalid() && !IsImplicit)
|
|
|
|
|
ScopeInfo->setFirstCoroutineStmt(Loc, Keyword);
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (ScopeInfo->CoroutinePromise)
|
|
|
|
|
return ScopeInfo;
|
|
|
|
|
|
2018-01-25 06:15:42 +08:00
|
|
|
|
if (!S.buildCoroutineParameterMoves(Loc))
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
ScopeInfo->CoroutinePromise = S.buildCoroutinePromise(Loc);
|
|
|
|
|
if (!ScopeInfo->CoroutinePromise)
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
return ScopeInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-14 11:24:55 +08:00
|
|
|
|
bool Sema::ActOnCoroutineBodyStart(Scope *SC, SourceLocation KWLoc,
|
|
|
|
|
StringRef Keyword) {
|
|
|
|
|
if (!checkCoroutineContext(*this, KWLoc, Keyword))
|
2017-03-07 07:38:15 +08:00
|
|
|
|
return false;
|
2017-06-14 11:24:55 +08:00
|
|
|
|
auto *ScopeInfo = getCurFunction();
|
2017-03-07 07:38:15 +08:00
|
|
|
|
assert(ScopeInfo->CoroutinePromise);
|
|
|
|
|
|
|
|
|
|
// If we have existing coroutine statements then we have already built
|
|
|
|
|
// the initial and final suspend points.
|
|
|
|
|
if (!ScopeInfo->NeedsCoroutineSuspends)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
ScopeInfo->setNeedsCoroutineSuspends(false);
|
|
|
|
|
|
2017-06-14 11:24:55 +08:00
|
|
|
|
auto *Fn = cast<FunctionDecl>(CurContext);
|
2017-03-07 07:38:15 +08:00
|
|
|
|
SourceLocation Loc = Fn->getLocation();
|
|
|
|
|
// Build the initial suspend point
|
|
|
|
|
auto buildSuspends = [&](StringRef Name) mutable -> StmtResult {
|
|
|
|
|
ExprResult Suspend =
|
2017-06-14 11:24:55 +08:00
|
|
|
|
buildPromiseCall(*this, ScopeInfo->CoroutinePromise, Loc, Name, None);
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (Suspend.isInvalid())
|
|
|
|
|
return StmtError();
|
2017-06-14 11:24:55 +08:00
|
|
|
|
Suspend = buildOperatorCoawaitCall(*this, SC, Loc, Suspend.get());
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (Suspend.isInvalid())
|
|
|
|
|
return StmtError();
|
2017-06-14 11:24:55 +08:00
|
|
|
|
Suspend = BuildResolvedCoawaitExpr(Loc, Suspend.get(),
|
|
|
|
|
/*IsImplicit*/ true);
|
|
|
|
|
Suspend = ActOnFinishFullExpr(Suspend.get());
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (Suspend.isInvalid()) {
|
2017-06-14 11:24:55 +08:00
|
|
|
|
Diag(Loc, diag::note_coroutine_promise_suspend_implicitly_required)
|
2017-03-07 07:38:15 +08:00
|
|
|
|
<< ((Name == "initial_suspend") ? 0 : 1);
|
2017-06-14 11:24:55 +08:00
|
|
|
|
Diag(KWLoc, diag::note_declared_coroutine_here) << Keyword;
|
2017-03-07 07:38:15 +08:00
|
|
|
|
return StmtError();
|
|
|
|
|
}
|
|
|
|
|
return cast<Stmt>(Suspend.get());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
StmtResult InitSuspend = buildSuspends("initial_suspend");
|
|
|
|
|
if (InitSuspend.isInvalid())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
StmtResult FinalSuspend = buildSuspends("final_suspend");
|
|
|
|
|
if (FinalSuspend.isInvalid())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
ScopeInfo->setCoroutineSuspends(InitSuspend.get(), FinalSuspend.get());
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
ExprResult Sema::ActOnCoawaitExpr(Scope *S, SourceLocation Loc, Expr *E) {
|
2017-06-14 11:24:55 +08:00
|
|
|
|
if (!ActOnCoroutineBodyStart(S, Loc, "co_await")) {
|
2016-09-30 05:47:39 +08:00
|
|
|
|
CorrectDelayedTyposInExpr(E);
|
|
|
|
|
return ExprError();
|
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
|
2015-11-21 06:57:24 +08:00
|
|
|
|
if (E->getType()->isPlaceholderType()) {
|
|
|
|
|
ExprResult R = CheckPlaceholderExpr(E);
|
|
|
|
|
if (R.isInvalid()) return ExprError();
|
|
|
|
|
E = R.get();
|
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
ExprResult Lookup = buildOperatorCoawaitLookupExpr(*this, S, Loc);
|
|
|
|
|
if (Lookup.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
return BuildUnresolvedCoawaitExpr(Loc, E,
|
|
|
|
|
cast<UnresolvedLookupExpr>(Lookup.get()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExprResult Sema::BuildUnresolvedCoawaitExpr(SourceLocation Loc, Expr *E,
|
2017-03-11 10:35:37 +08:00
|
|
|
|
UnresolvedLookupExpr *Lookup) {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
auto *FSI = checkCoroutineContext(*this, Loc, "co_await");
|
|
|
|
|
if (!FSI)
|
|
|
|
|
return ExprError();
|
2015-11-21 06:57:24 +08:00
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (E->getType()->isPlaceholderType()) {
|
|
|
|
|
ExprResult R = CheckPlaceholderExpr(E);
|
|
|
|
|
if (R.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
E = R.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto *Promise = FSI->CoroutinePromise;
|
|
|
|
|
if (Promise->getType()->isDependentType()) {
|
|
|
|
|
Expr *Res =
|
|
|
|
|
new (Context) DependentCoawaitExpr(Loc, Context.DependentTy, E, Lookup);
|
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto *RD = Promise->getType()->getAsCXXRecordDecl();
|
|
|
|
|
if (lookupMember(*this, "await_transform", RD, Loc)) {
|
|
|
|
|
ExprResult R = buildPromiseCall(*this, Promise, Loc, "await_transform", E);
|
|
|
|
|
if (R.isInvalid()) {
|
|
|
|
|
Diag(Loc,
|
|
|
|
|
diag::note_coroutine_promise_implicit_await_transform_required_here)
|
|
|
|
|
<< E->getSourceRange();
|
|
|
|
|
return ExprError();
|
|
|
|
|
}
|
|
|
|
|
E = R.get();
|
|
|
|
|
}
|
|
|
|
|
ExprResult Awaitable = buildOperatorCoawaitCall(*this, Loc, E, Lookup);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
if (Awaitable.isInvalid())
|
|
|
|
|
return ExprError();
|
2016-09-30 05:47:39 +08:00
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
return BuildResolvedCoawaitExpr(Loc, Awaitable.get());
|
2015-10-27 14:02:45 +08:00
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
|
|
|
|
|
ExprResult Sema::BuildResolvedCoawaitExpr(SourceLocation Loc, Expr *E,
|
|
|
|
|
bool IsImplicit) {
|
2017-03-11 10:35:37 +08:00
|
|
|
|
auto *Coroutine = checkCoroutineContext(*this, Loc, "co_await", IsImplicit);
|
2015-11-20 10:54:01 +08:00
|
|
|
|
if (!Coroutine)
|
|
|
|
|
return ExprError();
|
2015-10-27 14:02:45 +08:00
|
|
|
|
|
|
|
|
|
if (E->getType()->isPlaceholderType()) {
|
|
|
|
|
ExprResult R = CheckPlaceholderExpr(E);
|
|
|
|
|
if (R.isInvalid()) return ExprError();
|
|
|
|
|
E = R.get();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-21 06:57:24 +08:00
|
|
|
|
if (E->getType()->isDependentType()) {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
Expr *Res = new (Context)
|
|
|
|
|
CoawaitExpr(Loc, Context.DependentTy, E, IsImplicit);
|
2015-11-21 06:57:24 +08:00
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 11:13:02 +08:00
|
|
|
|
// If the expression is a temporary, materialize it as an lvalue so that we
|
|
|
|
|
// can use it multiple times.
|
|
|
|
|
if (E->getValueKind() == VK_RValue)
|
2016-06-22 04:29:17 +08:00
|
|
|
|
E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
|
|
|
|
|
// Build the await_ready, await_suspend, await_resume calls.
|
2017-03-09 11:09:43 +08:00
|
|
|
|
ReadySuspendResumeResult RSS =
|
|
|
|
|
buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
if (RSS.IsInvalid)
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
2017-03-11 09:30:17 +08:00
|
|
|
|
Expr *Res =
|
|
|
|
|
new (Context) CoawaitExpr(Loc, E, RSS.Results[0], RSS.Results[1],
|
|
|
|
|
RSS.Results[2], RSS.OpaqueValue, IsImplicit);
|
2017-03-11 10:35:37 +08:00
|
|
|
|
|
2015-10-22 14:13:50 +08:00
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
ExprResult Sema::ActOnCoyieldExpr(Scope *S, SourceLocation Loc, Expr *E) {
|
2017-06-14 11:24:55 +08:00
|
|
|
|
if (!ActOnCoroutineBodyStart(S, Loc, "co_yield")) {
|
2016-09-30 05:47:39 +08:00
|
|
|
|
CorrectDelayedTyposInExpr(E);
|
2015-11-21 06:40:06 +08:00
|
|
|
|
return ExprError();
|
2016-09-30 05:47:39 +08:00
|
|
|
|
}
|
2015-11-21 06:40:06 +08:00
|
|
|
|
|
|
|
|
|
// Build yield_value call.
|
2017-03-07 07:38:15 +08:00
|
|
|
|
ExprResult Awaitable = buildPromiseCall(
|
|
|
|
|
*this, getCurFunction()->CoroutinePromise, Loc, "yield_value", E);
|
2015-10-27 14:02:45 +08:00
|
|
|
|
if (Awaitable.isInvalid())
|
|
|
|
|
return ExprError();
|
2015-11-21 06:40:06 +08:00
|
|
|
|
|
|
|
|
|
// Build 'operator co_await' call.
|
|
|
|
|
Awaitable = buildOperatorCoawaitCall(*this, S, Loc, Awaitable.get());
|
|
|
|
|
if (Awaitable.isInvalid())
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return BuildCoyieldExpr(Loc, Awaitable.get());
|
|
|
|
|
}
|
|
|
|
|
ExprResult Sema::BuildCoyieldExpr(SourceLocation Loc, Expr *E) {
|
|
|
|
|
auto *Coroutine = checkCoroutineContext(*this, Loc, "co_yield");
|
2015-11-20 10:54:01 +08:00
|
|
|
|
if (!Coroutine)
|
|
|
|
|
return ExprError();
|
2015-10-22 14:13:50 +08:00
|
|
|
|
|
2015-11-21 06:57:24 +08:00
|
|
|
|
if (E->getType()->isPlaceholderType()) {
|
|
|
|
|
ExprResult R = CheckPlaceholderExpr(E);
|
|
|
|
|
if (R.isInvalid()) return ExprError();
|
|
|
|
|
E = R.get();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 10:57:17 +08:00
|
|
|
|
if (E->getType()->isDependentType()) {
|
|
|
|
|
Expr *Res = new (Context) CoyieldExpr(Loc, Context.DependentTy, E);
|
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 11:13:02 +08:00
|
|
|
|
// If the expression is a temporary, materialize it as an lvalue so that we
|
|
|
|
|
// can use it multiple times.
|
|
|
|
|
if (E->getValueKind() == VK_RValue)
|
2016-06-22 04:29:17 +08:00
|
|
|
|
E = CreateMaterializeTemporaryExpr(E->getType(), E, true);
|
2015-11-22 10:57:17 +08:00
|
|
|
|
|
|
|
|
|
// Build the await_ready, await_suspend, await_resume calls.
|
2017-03-09 11:09:43 +08:00
|
|
|
|
ReadySuspendResumeResult RSS =
|
|
|
|
|
buildCoawaitCalls(*this, Coroutine->CoroutinePromise, Loc, E);
|
2015-11-22 10:57:17 +08:00
|
|
|
|
if (RSS.IsInvalid)
|
|
|
|
|
return ExprError();
|
|
|
|
|
|
2017-06-14 11:24:55 +08:00
|
|
|
|
Expr *Res =
|
|
|
|
|
new (Context) CoyieldExpr(Loc, E, RSS.Results[0], RSS.Results[1],
|
|
|
|
|
RSS.Results[2], RSS.OpaqueValue);
|
2017-03-11 10:35:37 +08:00
|
|
|
|
|
2015-10-22 14:13:50 +08:00
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
StmtResult Sema::ActOnCoreturnStmt(Scope *S, SourceLocation Loc, Expr *E) {
|
2017-06-14 11:24:55 +08:00
|
|
|
|
if (!ActOnCoroutineBodyStart(S, Loc, "co_return")) {
|
2016-09-30 05:47:39 +08:00
|
|
|
|
CorrectDelayedTyposInExpr(E);
|
|
|
|
|
return StmtError();
|
|
|
|
|
}
|
2015-10-27 14:02:45 +08:00
|
|
|
|
return BuildCoreturnStmt(Loc, E);
|
|
|
|
|
}
|
2016-10-04 08:31:16 +08:00
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
StmtResult Sema::BuildCoreturnStmt(SourceLocation Loc, Expr *E,
|
|
|
|
|
bool IsImplicit) {
|
2017-03-11 10:35:37 +08:00
|
|
|
|
auto *FSI = checkCoroutineContext(*this, Loc, "co_return", IsImplicit);
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (!FSI)
|
2015-11-22 15:33:28 +08:00
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
|
|
if (E && E->getType()->isPlaceholderType() &&
|
|
|
|
|
!E->getType()->isSpecificPlaceholderType(BuiltinType::Overload)) {
|
2015-11-21 06:57:24 +08:00
|
|
|
|
ExprResult R = CheckPlaceholderExpr(E);
|
|
|
|
|
if (R.isInvalid()) return StmtError();
|
|
|
|
|
E = R.get();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 15:05:16 +08:00
|
|
|
|
// FIXME: If the operand is a reference to a variable that's about to go out
|
2015-11-24 10:34:39 +08:00
|
|
|
|
// of scope, we should treat the operand as an xvalue for this overload
|
2015-11-22 15:05:16 +08:00
|
|
|
|
// resolution.
|
2017-03-07 07:38:15 +08:00
|
|
|
|
VarDecl *Promise = FSI->CoroutinePromise;
|
2015-11-22 15:05:16 +08:00
|
|
|
|
ExprResult PC;
|
2016-10-07 05:23:38 +08:00
|
|
|
|
if (E && (isa<InitListExpr>(E) || !E->getType()->isVoidType())) {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
PC = buildPromiseCall(*this, Promise, Loc, "return_value", E);
|
2015-11-22 15:05:16 +08:00
|
|
|
|
} else {
|
|
|
|
|
E = MakeFullDiscardedValueExpr(E).get();
|
2017-03-07 07:38:15 +08:00
|
|
|
|
PC = buildPromiseCall(*this, Promise, Loc, "return_void", None);
|
2015-11-22 15:05:16 +08:00
|
|
|
|
}
|
|
|
|
|
if (PC.isInvalid())
|
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
|
|
Expr *PCE = ActOnFinishFullExpr(PC.get()).get();
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
Stmt *Res = new (Context) CoreturnStmt(Loc, E, PCE, IsImplicit);
|
2015-10-22 14:13:50 +08:00
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 11:12:48 +08:00
|
|
|
|
/// Look up the std::nothrow object.
|
|
|
|
|
static Expr *buildStdNoThrowDeclRef(Sema &S, SourceLocation Loc) {
|
|
|
|
|
NamespaceDecl *Std = S.getStdNamespace();
|
|
|
|
|
assert(Std && "Should already be diagnosed");
|
|
|
|
|
|
|
|
|
|
LookupResult Result(S, &S.PP.getIdentifierTable().get("nothrow"), Loc,
|
|
|
|
|
Sema::LookupOrdinaryName);
|
|
|
|
|
if (!S.LookupQualifiedName(Result, Std)) {
|
|
|
|
|
// FIXME: <experimental/coroutine> should have been included already.
|
|
|
|
|
// If we require it to include <new> then this diagnostic is no longer
|
|
|
|
|
// needed.
|
|
|
|
|
S.Diag(Loc, diag::err_implicit_coroutine_std_nothrow_type_not_found);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto *VD = Result.getAsSingle<VarDecl>();
|
|
|
|
|
if (!VD) {
|
|
|
|
|
Result.suppressDiagnostics();
|
|
|
|
|
// We found something weird. Complain about the first thing we found.
|
|
|
|
|
NamedDecl *Found = *Result.begin();
|
|
|
|
|
S.Diag(Found->getLocation(), diag::err_malformed_std_nothrow);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExprResult DR = S.BuildDeclRefExpr(VD, VD->getType(), VK_LValue, Loc);
|
|
|
|
|
if (DR.isInvalid())
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
return DR.get();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 00:28:31 +08:00
|
|
|
|
// Find an appropriate delete for the promise.
|
|
|
|
|
static FunctionDecl *findDeleteForPromise(Sema &S, SourceLocation Loc,
|
|
|
|
|
QualType PromiseType) {
|
|
|
|
|
FunctionDecl *OperatorDelete = nullptr;
|
|
|
|
|
|
|
|
|
|
DeclarationName DeleteName =
|
|
|
|
|
S.Context.DeclarationNames.getCXXOperatorName(OO_Delete);
|
|
|
|
|
|
|
|
|
|
auto *PointeeRD = PromiseType->getAsCXXRecordDecl();
|
|
|
|
|
assert(PointeeRD && "PromiseType must be a CxxRecordDecl type");
|
|
|
|
|
|
|
|
|
|
if (S.FindDeallocationFunction(Loc, PointeeRD, DeleteName, OperatorDelete))
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
if (!OperatorDelete) {
|
|
|
|
|
// Look for a global declaration.
|
|
|
|
|
const bool CanProvideSize = S.isCompleteType(Loc, PromiseType);
|
|
|
|
|
const bool Overaligned = false;
|
|
|
|
|
OperatorDelete = S.FindUsualDeallocationFunction(Loc, CanProvideSize,
|
|
|
|
|
Overaligned, DeleteName);
|
|
|
|
|
}
|
|
|
|
|
S.MarkFunctionReferenced(Loc, OperatorDelete);
|
|
|
|
|
return OperatorDelete;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 13:05:02 +08:00
|
|
|
|
|
2015-11-24 10:34:39 +08:00
|
|
|
|
void Sema::CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body) {
|
2015-10-22 14:13:50 +08:00
|
|
|
|
FunctionScopeInfo *Fn = getCurFunction();
|
2017-05-25 10:16:53 +08:00
|
|
|
|
assert(Fn && Fn->isCoroutine() && "not a coroutine");
|
2017-03-09 11:09:43 +08:00
|
|
|
|
if (!Body) {
|
|
|
|
|
assert(FD->isInvalidDecl() &&
|
|
|
|
|
"a null body is only allowed for invalid declarations");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-25 10:16:53 +08:00
|
|
|
|
// We have a function that uses coroutine keywords, but we failed to build
|
|
|
|
|
// the promise type.
|
|
|
|
|
if (!Fn->CoroutinePromise)
|
|
|
|
|
return FD->setInvalidDecl();
|
2017-03-09 11:09:43 +08:00
|
|
|
|
|
|
|
|
|
if (isa<CoroutineBodyStmt>(Body)) {
|
2017-05-24 22:34:19 +08:00
|
|
|
|
// Nothing todo. the body is already a transformed coroutine body statement.
|
2017-03-09 11:09:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-22 14:13:50 +08:00
|
|
|
|
// Coroutines [stmt.return]p1:
|
|
|
|
|
// A return statement shall not appear in a coroutine.
|
2015-10-27 14:02:45 +08:00
|
|
|
|
if (Fn->FirstReturnLoc.isValid()) {
|
2017-03-11 10:35:37 +08:00
|
|
|
|
assert(Fn->FirstCoroutineStmtLoc.isValid() &&
|
|
|
|
|
"first coroutine location not set");
|
2015-10-27 14:02:45 +08:00
|
|
|
|
Diag(Fn->FirstReturnLoc, diag::err_return_in_coroutine);
|
2017-03-11 10:35:37 +08:00
|
|
|
|
Diag(Fn->FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
|
|
|
|
|
<< Fn->getFirstCoroutineStmtKeyword();
|
2015-10-22 14:13:50 +08:00
|
|
|
|
}
|
2017-04-04 03:21:00 +08:00
|
|
|
|
CoroutineStmtBuilder Builder(*this, *FD, *Fn, Body);
|
|
|
|
|
if (Builder.isInvalid() || !Builder.buildStatements())
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return FD->setInvalidDecl();
|
2015-10-22 14:13:50 +08:00
|
|
|
|
|
2017-02-13 13:05:02 +08:00
|
|
|
|
// Build body for the coroutine wrapper statement.
|
|
|
|
|
Body = CoroutineBodyStmt::Create(Context, Builder);
|
|
|
|
|
}
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
CoroutineStmtBuilder::CoroutineStmtBuilder(Sema &S, FunctionDecl &FD,
|
|
|
|
|
sema::FunctionScopeInfo &Fn,
|
|
|
|
|
Stmt *Body)
|
|
|
|
|
: S(S), FD(FD), Fn(Fn), Loc(FD.getLocation()),
|
|
|
|
|
IsPromiseDependentType(
|
|
|
|
|
!Fn.CoroutinePromise ||
|
|
|
|
|
Fn.CoroutinePromise->getType()->isDependentType()) {
|
|
|
|
|
this->Body = Body;
|
2018-01-25 06:15:42 +08:00
|
|
|
|
|
|
|
|
|
for (auto KV : Fn.CoroutineParameterMoves)
|
|
|
|
|
this->ParamMovesVector.push_back(KV.second);
|
|
|
|
|
this->ParamMoves = this->ParamMovesVector;
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
if (!IsPromiseDependentType) {
|
|
|
|
|
PromiseRecordDecl = Fn.CoroutinePromise->getType()->getAsCXXRecordDecl();
|
|
|
|
|
assert(PromiseRecordDecl && "Type should have already been checked");
|
|
|
|
|
}
|
|
|
|
|
this->IsValid = makePromiseStmt() && makeInitialAndFinalSuspend();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CoroutineStmtBuilder::buildStatements() {
|
|
|
|
|
assert(this->IsValid && "coroutine already invalid");
|
2018-01-25 06:15:42 +08:00
|
|
|
|
this->IsValid = makeReturnObject();
|
2017-04-04 03:21:00 +08:00
|
|
|
|
if (this->IsValid && !IsPromiseDependentType)
|
|
|
|
|
buildDependentStatements();
|
|
|
|
|
return this->IsValid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CoroutineStmtBuilder::buildDependentStatements() {
|
|
|
|
|
assert(this->IsValid && "coroutine already invalid");
|
|
|
|
|
assert(!this->IsPromiseDependentType &&
|
|
|
|
|
"coroutine cannot have a dependent promise type");
|
|
|
|
|
this->IsValid = makeOnException() && makeOnFallthrough() &&
|
2017-05-23 04:22:23 +08:00
|
|
|
|
makeGroDeclAndReturnStmt() && makeReturnOnAllocFailure() &&
|
|
|
|
|
makeNewAndDeleteExpr();
|
2017-04-04 03:21:00 +08:00
|
|
|
|
return this->IsValid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CoroutineStmtBuilder::makePromiseStmt() {
|
2015-11-24 10:34:39 +08:00
|
|
|
|
// Form a declaration statement for the promise declaration, so that AST
|
|
|
|
|
// visitors can more easily find it.
|
|
|
|
|
StmtResult PromiseStmt =
|
2017-02-13 13:05:02 +08:00
|
|
|
|
S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(Fn.CoroutinePromise), Loc, Loc);
|
2015-11-24 10:34:39 +08:00
|
|
|
|
if (PromiseStmt.isInvalid())
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
this->Promise = PromiseStmt.get();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
bool CoroutineStmtBuilder::makeInitialAndFinalSuspend() {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (Fn.hasInvalidCoroutineSuspends())
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return false;
|
2017-03-07 07:38:15 +08:00
|
|
|
|
this->InitialSuspend = cast<Expr>(Fn.CoroutineSuspends.first);
|
|
|
|
|
this->FinalSuspend = cast<Expr>(Fn.CoroutineSuspends.second);
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-03-28 07:36:59 +08:00
|
|
|
|
static bool diagReturnOnAllocFailure(Sema &S, Expr *E,
|
|
|
|
|
CXXRecordDecl *PromiseRecordDecl,
|
|
|
|
|
FunctionScopeInfo &Fn) {
|
|
|
|
|
auto Loc = E->getExprLoc();
|
|
|
|
|
if (auto *DeclRef = dyn_cast_or_null<DeclRefExpr>(E)) {
|
|
|
|
|
auto *Decl = DeclRef->getDecl();
|
|
|
|
|
if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(Decl)) {
|
|
|
|
|
if (Method->isStatic())
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
Loc = Decl->getLocation();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
S.Diag(
|
|
|
|
|
Loc,
|
|
|
|
|
diag::err_coroutine_promise_get_return_object_on_allocation_failure)
|
|
|
|
|
<< PromiseRecordDecl;
|
|
|
|
|
S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
|
|
|
|
|
<< Fn.getFirstCoroutineStmtKeyword();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
bool CoroutineStmtBuilder::makeReturnOnAllocFailure() {
|
|
|
|
|
assert(!IsPromiseDependentType &&
|
|
|
|
|
"cannot make statement while the promise type is dependent");
|
2017-03-28 07:36:59 +08:00
|
|
|
|
|
|
|
|
|
// [dcl.fct.def.coroutine]/8
|
|
|
|
|
// The unqualified-id get_return_object_on_allocation_failure is looked up in
|
|
|
|
|
// the scope of class P by class member access lookup (3.4.5). ...
|
|
|
|
|
// If an allocation function returns nullptr, ... the coroutine return value
|
|
|
|
|
// is obtained by a call to ... get_return_object_on_allocation_failure().
|
|
|
|
|
|
|
|
|
|
DeclarationName DN =
|
|
|
|
|
S.PP.getIdentifierInfo("get_return_object_on_allocation_failure");
|
|
|
|
|
LookupResult Found(S, DN, Loc, Sema::LookupMemberName);
|
2017-04-04 03:21:00 +08:00
|
|
|
|
if (!S.LookupQualifiedName(Found, PromiseRecordDecl))
|
|
|
|
|
return true;
|
2017-03-28 07:36:59 +08:00
|
|
|
|
|
|
|
|
|
CXXScopeSpec SS;
|
|
|
|
|
ExprResult DeclNameExpr =
|
|
|
|
|
S.BuildDeclarationNameExpr(SS, Found, /*NeedsADL=*/false);
|
2017-04-04 03:21:00 +08:00
|
|
|
|
if (DeclNameExpr.isInvalid())
|
|
|
|
|
return false;
|
2017-03-28 07:36:59 +08:00
|
|
|
|
|
|
|
|
|
if (!diagReturnOnAllocFailure(S, DeclNameExpr.get(), PromiseRecordDecl, Fn))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ExprResult ReturnObjectOnAllocationFailure =
|
|
|
|
|
S.ActOnCallExpr(nullptr, DeclNameExpr.get(), Loc, {}, Loc);
|
2017-04-04 03:21:00 +08:00
|
|
|
|
if (ReturnObjectOnAllocationFailure.isInvalid())
|
|
|
|
|
return false;
|
2017-03-28 07:36:59 +08:00
|
|
|
|
|
2017-03-28 10:51:45 +08:00
|
|
|
|
StmtResult ReturnStmt =
|
|
|
|
|
S.BuildReturnStmt(Loc, ReturnObjectOnAllocationFailure.get());
|
2017-05-23 04:22:23 +08:00
|
|
|
|
if (ReturnStmt.isInvalid()) {
|
2017-05-25 22:59:39 +08:00
|
|
|
|
S.Diag(Found.getFoundDecl()->getLocation(), diag::note_member_declared_here)
|
|
|
|
|
<< DN;
|
2017-05-23 04:22:23 +08:00
|
|
|
|
S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
|
|
|
|
|
<< Fn.getFirstCoroutineStmtKeyword();
|
2017-04-04 03:21:00 +08:00
|
|
|
|
return false;
|
2017-05-23 04:22:23 +08:00
|
|
|
|
}
|
2017-03-28 07:36:59 +08:00
|
|
|
|
|
|
|
|
|
this->ReturnStmtOnAllocFailure = ReturnStmt.get();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
bool CoroutineStmtBuilder::makeNewAndDeleteExpr() {
|
2017-03-07 07:38:15 +08:00
|
|
|
|
// Form and check allocation and deallocation calls.
|
2017-04-04 03:21:00 +08:00
|
|
|
|
assert(!IsPromiseDependentType &&
|
|
|
|
|
"cannot make statement while the promise type is dependent");
|
2017-03-07 07:38:15 +08:00
|
|
|
|
QualType PromiseType = Fn.CoroutinePromise->getType();
|
|
|
|
|
|
|
|
|
|
if (S.RequireCompleteType(Loc, PromiseType, diag::err_incomplete_type))
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return false;
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-04-18 11:12:48 +08:00
|
|
|
|
const bool RequiresNoThrowAlloc = ReturnStmtOnAllocFailure != nullptr;
|
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
// [dcl.fct.def.coroutine]/7
|
|
|
|
|
// Lookup allocation functions using a parameter list composed of the
|
|
|
|
|
// requested size of the coroutine state being allocated, followed by
|
|
|
|
|
// the coroutine function's arguments. If a matching allocation function
|
|
|
|
|
// exists, use it. Otherwise, use an allocation function that just takes
|
|
|
|
|
// the requested size.
|
2017-02-13 13:05:02 +08:00
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
FunctionDecl *OperatorNew = nullptr;
|
|
|
|
|
FunctionDecl *OperatorDelete = nullptr;
|
|
|
|
|
FunctionDecl *UnusedResult = nullptr;
|
|
|
|
|
bool PassAlignment = false;
|
2017-04-18 13:08:08 +08:00
|
|
|
|
SmallVector<Expr *, 1> PlacementArgs;
|
2017-03-07 07:38:15 +08:00
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
// [dcl.fct.def.coroutine]/7
|
|
|
|
|
// "The allocation function’s name is looked up in the scope of P.
|
|
|
|
|
// [...] If the lookup finds an allocation function in the scope of P,
|
|
|
|
|
// overload resolution is performed on a function call created by assembling
|
|
|
|
|
// an argument list. The first argument is the amount of space requested,
|
|
|
|
|
// and has type std::size_t. The lvalues p1 ... pn are the succeeding
|
|
|
|
|
// arguments."
|
|
|
|
|
//
|
|
|
|
|
// ...where "p1 ... pn" are defined earlier as:
|
|
|
|
|
//
|
|
|
|
|
// [dcl.fct.def.coroutine]/3
|
|
|
|
|
// "For a coroutine f that is a non-static member function, let P1 denote the
|
|
|
|
|
// type of the implicit object parameter (13.3.1) and P2 ... Pn be the types
|
|
|
|
|
// of the function parameters; otherwise let P1 ... Pn be the types of the
|
|
|
|
|
// function parameters. Let p1 ... pn be lvalues denoting those objects."
|
|
|
|
|
if (auto *MD = dyn_cast<CXXMethodDecl>(&FD)) {
|
|
|
|
|
if (MD->isInstance() && !isLambdaCallOperator(MD)) {
|
|
|
|
|
ExprResult ThisExpr = S.ActOnCXXThis(Loc);
|
|
|
|
|
if (ThisExpr.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
ThisExpr = S.CreateBuiltinUnaryOp(Loc, UO_Deref, ThisExpr.get());
|
|
|
|
|
if (ThisExpr.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
PlacementArgs.push_back(ThisExpr.get());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (auto *PD : FD.parameters()) {
|
|
|
|
|
if (PD->getType()->isDependentType())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Build a reference to the parameter.
|
|
|
|
|
auto PDLoc = PD->getLocation();
|
|
|
|
|
ExprResult PDRefExpr =
|
|
|
|
|
S.BuildDeclRefExpr(PD, PD->getOriginalType().getNonReferenceType(),
|
|
|
|
|
ExprValueKind::VK_LValue, PDLoc);
|
|
|
|
|
if (PDRefExpr.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
PlacementArgs.push_back(PDRefExpr.get());
|
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
S.FindAllocationFunctions(Loc, SourceRange(),
|
|
|
|
|
/*UseGlobal*/ false, PromiseType,
|
2017-04-18 11:12:48 +08:00
|
|
|
|
/*isArray*/ false, PassAlignment, PlacementArgs,
|
2018-02-16 04:37:22 +08:00
|
|
|
|
OperatorNew, UnusedResult, /*Diagnose*/ false);
|
|
|
|
|
|
|
|
|
|
// [dcl.fct.def.coroutine]/7
|
|
|
|
|
// "If no matching function is found, overload resolution is performed again
|
|
|
|
|
// on a function call created by passing just the amount of space required as
|
|
|
|
|
// an argument of type std::size_t."
|
|
|
|
|
if (!OperatorNew && !PlacementArgs.empty()) {
|
|
|
|
|
PlacementArgs.clear();
|
|
|
|
|
S.FindAllocationFunctions(Loc, SourceRange(),
|
|
|
|
|
/*UseGlobal*/ false, PromiseType,
|
|
|
|
|
/*isArray*/ false, PassAlignment,
|
|
|
|
|
PlacementArgs, OperatorNew, UnusedResult);
|
|
|
|
|
}
|
2017-04-18 11:12:48 +08:00
|
|
|
|
|
|
|
|
|
bool IsGlobalOverload =
|
|
|
|
|
OperatorNew && !isa<CXXRecordDecl>(OperatorNew->getDeclContext());
|
|
|
|
|
// If we didn't find a class-local new declaration and non-throwing new
|
|
|
|
|
// was is required then we need to lookup the non-throwing global operator
|
|
|
|
|
// instead.
|
|
|
|
|
if (RequiresNoThrowAlloc && (!OperatorNew || IsGlobalOverload)) {
|
|
|
|
|
auto *StdNoThrow = buildStdNoThrowDeclRef(S, Loc);
|
|
|
|
|
if (!StdNoThrow)
|
|
|
|
|
return false;
|
2017-04-18 13:08:08 +08:00
|
|
|
|
PlacementArgs = {StdNoThrow};
|
2017-04-18 11:12:48 +08:00
|
|
|
|
OperatorNew = nullptr;
|
|
|
|
|
S.FindAllocationFunctions(Loc, SourceRange(),
|
|
|
|
|
/*UseGlobal*/ true, PromiseType,
|
|
|
|
|
/*isArray*/ false, PassAlignment, PlacementArgs,
|
|
|
|
|
OperatorNew, UnusedResult);
|
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
if (!OperatorNew)
|
|
|
|
|
return false;
|
2017-04-18 13:30:39 +08:00
|
|
|
|
|
|
|
|
|
if (RequiresNoThrowAlloc) {
|
2017-04-18 11:12:48 +08:00
|
|
|
|
const auto *FT = OperatorNew->getType()->getAs<FunctionProtoType>();
|
|
|
|
|
if (!FT->isNothrow(S.Context, /*ResultIfDependent*/ false)) {
|
|
|
|
|
S.Diag(OperatorNew->getLocation(),
|
|
|
|
|
diag::err_coroutine_promise_new_requires_nothrow)
|
|
|
|
|
<< OperatorNew;
|
|
|
|
|
S.Diag(Loc, diag::note_coroutine_promise_call_implicitly_required)
|
|
|
|
|
<< OperatorNew;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
|
2017-04-18 11:12:48 +08:00
|
|
|
|
if ((OperatorDelete = findDeleteForPromise(S, Loc, PromiseType)) == nullptr)
|
2017-03-07 07:38:15 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Expr *FramePtr =
|
|
|
|
|
buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_frame, {});
|
|
|
|
|
|
|
|
|
|
Expr *FrameSize =
|
|
|
|
|
buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_size, {});
|
|
|
|
|
|
|
|
|
|
// Make new call.
|
|
|
|
|
|
|
|
|
|
ExprResult NewRef =
|
|
|
|
|
S.BuildDeclRefExpr(OperatorNew, OperatorNew->getType(), VK_LValue, Loc);
|
|
|
|
|
if (NewRef.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-04-18 13:08:08 +08:00
|
|
|
|
SmallVector<Expr *, 2> NewArgs(1, FrameSize);
|
2017-04-18 11:12:48 +08:00
|
|
|
|
for (auto Arg : PlacementArgs)
|
|
|
|
|
NewArgs.push_back(Arg);
|
|
|
|
|
|
2017-03-07 07:38:15 +08:00
|
|
|
|
ExprResult NewExpr =
|
2017-04-18 11:12:48 +08:00
|
|
|
|
S.ActOnCallExpr(S.getCurScope(), NewRef.get(), Loc, NewArgs, Loc);
|
|
|
|
|
NewExpr = S.ActOnFinishFullExpr(NewExpr.get());
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (NewExpr.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Make delete call.
|
|
|
|
|
|
|
|
|
|
QualType OpDeleteQualType = OperatorDelete->getType();
|
|
|
|
|
|
|
|
|
|
ExprResult DeleteRef =
|
|
|
|
|
S.BuildDeclRefExpr(OperatorDelete, OpDeleteQualType, VK_LValue, Loc);
|
|
|
|
|
if (DeleteRef.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Expr *CoroFree =
|
|
|
|
|
buildBuiltinCall(S, Loc, Builtin::BI__builtin_coro_free, {FramePtr});
|
|
|
|
|
|
|
|
|
|
SmallVector<Expr *, 2> DeleteArgs{CoroFree};
|
|
|
|
|
|
|
|
|
|
// Check if we need to pass the size.
|
|
|
|
|
const auto *OpDeleteType =
|
|
|
|
|
OpDeleteQualType.getTypePtr()->getAs<FunctionProtoType>();
|
|
|
|
|
if (OpDeleteType->getNumParams() > 1)
|
|
|
|
|
DeleteArgs.push_back(FrameSize);
|
|
|
|
|
|
|
|
|
|
ExprResult DeleteExpr =
|
|
|
|
|
S.ActOnCallExpr(S.getCurScope(), DeleteRef.get(), Loc, DeleteArgs, Loc);
|
2017-04-18 11:12:48 +08:00
|
|
|
|
DeleteExpr = S.ActOnFinishFullExpr(DeleteExpr.get());
|
2017-03-07 07:38:15 +08:00
|
|
|
|
if (DeleteExpr.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
this->Allocate = NewExpr.get();
|
|
|
|
|
this->Deallocate = DeleteExpr.get();
|
|
|
|
|
|
|
|
|
|
return true;
|
2017-02-13 13:05:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
bool CoroutineStmtBuilder::makeOnFallthrough() {
|
|
|
|
|
assert(!IsPromiseDependentType &&
|
|
|
|
|
"cannot make statement while the promise type is dependent");
|
2017-02-13 13:05:02 +08:00
|
|
|
|
|
|
|
|
|
// [dcl.fct.def.coroutine]/4
|
|
|
|
|
// The unqualified-ids 'return_void' and 'return_value' are looked up in
|
|
|
|
|
// the scope of class P. If both are found, the program is ill-formed.
|
2017-05-25 22:59:39 +08:00
|
|
|
|
bool HasRVoid, HasRValue;
|
|
|
|
|
LookupResult LRVoid =
|
|
|
|
|
lookupMember(S, "return_void", PromiseRecordDecl, Loc, HasRVoid);
|
|
|
|
|
LookupResult LRValue =
|
|
|
|
|
lookupMember(S, "return_value", PromiseRecordDecl, Loc, HasRValue);
|
2017-02-13 13:05:02 +08:00
|
|
|
|
|
|
|
|
|
StmtResult Fallthrough;
|
|
|
|
|
if (HasRVoid && HasRValue) {
|
|
|
|
|
// FIXME Improve this diagnostic
|
2017-05-25 22:59:39 +08:00
|
|
|
|
S.Diag(FD.getLocation(),
|
|
|
|
|
diag::err_coroutine_promise_incompatible_return_functions)
|
|
|
|
|
<< PromiseRecordDecl;
|
|
|
|
|
S.Diag(LRVoid.getRepresentativeDecl()->getLocation(),
|
|
|
|
|
diag::note_member_first_declared_here)
|
|
|
|
|
<< LRVoid.getLookupName();
|
|
|
|
|
S.Diag(LRValue.getRepresentativeDecl()->getLocation(),
|
|
|
|
|
diag::note_member_first_declared_here)
|
|
|
|
|
<< LRValue.getLookupName();
|
|
|
|
|
return false;
|
|
|
|
|
} else if (!HasRVoid && !HasRValue) {
|
|
|
|
|
// FIXME: The PDTS currently specifies this case as UB, not ill-formed.
|
|
|
|
|
// However we still diagnose this as an error since until the PDTS is fixed.
|
|
|
|
|
S.Diag(FD.getLocation(),
|
|
|
|
|
diag::err_coroutine_promise_requires_return_function)
|
|
|
|
|
<< PromiseRecordDecl;
|
|
|
|
|
S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
|
2017-02-13 13:05:02 +08:00
|
|
|
|
<< PromiseRecordDecl;
|
|
|
|
|
return false;
|
|
|
|
|
} else if (HasRVoid) {
|
|
|
|
|
// If the unqualified-id return_void is found, flowing off the end of a
|
|
|
|
|
// coroutine is equivalent to a co_return with no operand. Otherwise,
|
|
|
|
|
// flowing off the end of a coroutine results in undefined behavior.
|
2017-03-07 07:38:15 +08:00
|
|
|
|
Fallthrough = S.BuildCoreturnStmt(FD.getLocation(), nullptr,
|
|
|
|
|
/*IsImplicit*/false);
|
2017-02-13 13:05:02 +08:00
|
|
|
|
Fallthrough = S.ActOnFinishFullStmt(Fallthrough.get());
|
|
|
|
|
if (Fallthrough.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->OnFallthrough = Fallthrough.get();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
bool CoroutineStmtBuilder::makeOnException() {
|
2017-03-23 08:33:33 +08:00
|
|
|
|
// Try to form 'p.unhandled_exception();'
|
2017-04-04 03:21:00 +08:00
|
|
|
|
assert(!IsPromiseDependentType &&
|
|
|
|
|
"cannot make statement while the promise type is dependent");
|
2017-02-13 13:05:02 +08:00
|
|
|
|
|
2017-03-23 08:33:33 +08:00
|
|
|
|
const bool RequireUnhandledException = S.getLangOpts().CXXExceptions;
|
|
|
|
|
|
|
|
|
|
if (!lookupMember(S, "unhandled_exception", PromiseRecordDecl, Loc)) {
|
|
|
|
|
auto DiagID =
|
|
|
|
|
RequireUnhandledException
|
|
|
|
|
? diag::err_coroutine_promise_unhandled_exception_required
|
|
|
|
|
: diag::
|
|
|
|
|
warn_coroutine_promise_unhandled_exception_required_with_exceptions;
|
|
|
|
|
S.Diag(Loc, DiagID) << PromiseRecordDecl;
|
2017-05-24 22:34:19 +08:00
|
|
|
|
S.Diag(PromiseRecordDecl->getLocation(), diag::note_defined_here)
|
|
|
|
|
<< PromiseRecordDecl;
|
2017-03-23 08:33:33 +08:00
|
|
|
|
return !RequireUnhandledException;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 13:05:02 +08:00
|
|
|
|
// If exceptions are disabled, don't try to build OnException.
|
|
|
|
|
if (!S.getLangOpts().CXXExceptions)
|
|
|
|
|
return true;
|
|
|
|
|
|
2017-03-23 08:33:33 +08:00
|
|
|
|
ExprResult UnhandledException = buildPromiseCall(S, Fn.CoroutinePromise, Loc,
|
|
|
|
|
"unhandled_exception", None);
|
|
|
|
|
UnhandledException = S.ActOnFinishFullExpr(UnhandledException.get(), Loc);
|
|
|
|
|
if (UnhandledException.isInvalid())
|
|
|
|
|
return false;
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-05-23 06:33:17 +08:00
|
|
|
|
// Since the body of the coroutine will be wrapped in try-catch, it will
|
|
|
|
|
// be incompatible with SEH __try if present in a function.
|
|
|
|
|
if (!S.getLangOpts().Borland && Fn.FirstSEHTryLoc.isValid()) {
|
|
|
|
|
S.Diag(Fn.FirstSEHTryLoc, diag::err_seh_in_a_coroutine_with_cxx_exceptions);
|
|
|
|
|
S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
|
|
|
|
|
<< Fn.getFirstCoroutineStmtKeyword();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 08:33:33 +08:00
|
|
|
|
this->OnException = UnhandledException.get();
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-04 03:21:00 +08:00
|
|
|
|
bool CoroutineStmtBuilder::makeReturnObject() {
|
2015-11-24 10:34:39 +08:00
|
|
|
|
// Build implicit 'p.get_return_object()' expression and form initialization
|
|
|
|
|
// of return type from it.
|
|
|
|
|
ExprResult ReturnObject =
|
2017-03-07 07:38:15 +08:00
|
|
|
|
buildPromiseCall(S, Fn.CoroutinePromise, Loc, "get_return_object", None);
|
2015-11-24 10:34:39 +08:00
|
|
|
|
if (ReturnObject.isInvalid())
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return false;
|
2017-05-23 04:22:23 +08:00
|
|
|
|
|
|
|
|
|
this->ReturnValue = ReturnObject.get();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void noteMemberDeclaredHere(Sema &S, Expr *E, FunctionScopeInfo &Fn) {
|
|
|
|
|
if (auto *MbrRef = dyn_cast<CXXMemberCallExpr>(E)) {
|
|
|
|
|
auto *MethodDecl = MbrRef->getMethodDecl();
|
2017-05-25 22:59:39 +08:00
|
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_member_declared_here)
|
|
|
|
|
<< MethodDecl;
|
2017-05-23 04:22:23 +08:00
|
|
|
|
}
|
|
|
|
|
S.Diag(Fn.FirstCoroutineStmtLoc, diag::note_declared_coroutine_here)
|
|
|
|
|
<< Fn.getFirstCoroutineStmtKeyword();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CoroutineStmtBuilder::makeGroDeclAndReturnStmt() {
|
|
|
|
|
assert(!IsPromiseDependentType &&
|
|
|
|
|
"cannot make statement while the promise type is dependent");
|
|
|
|
|
assert(this->ReturnValue && "ReturnValue must be already formed");
|
|
|
|
|
|
|
|
|
|
QualType const GroType = this->ReturnValue->getType();
|
|
|
|
|
assert(!GroType->isDependentType() &&
|
|
|
|
|
"get_return_object type must no longer be dependent");
|
|
|
|
|
|
|
|
|
|
QualType const FnRetType = FD.getReturnType();
|
|
|
|
|
assert(!FnRetType->isDependentType() &&
|
|
|
|
|
"get_return_object type must no longer be dependent");
|
|
|
|
|
|
|
|
|
|
if (FnRetType->isVoidType()) {
|
|
|
|
|
ExprResult Res = S.ActOnFinishFullExpr(this->ReturnValue, Loc);
|
|
|
|
|
if (Res.isInvalid())
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return false;
|
2017-05-23 04:22:23 +08:00
|
|
|
|
|
|
|
|
|
this->ResultDecl = Res.get();
|
|
|
|
|
return true;
|
2015-11-24 10:34:39 +08:00
|
|
|
|
}
|
2017-05-23 04:22:23 +08:00
|
|
|
|
|
|
|
|
|
if (GroType->isVoidType()) {
|
|
|
|
|
// Trigger a nice error message.
|
|
|
|
|
InitializedEntity Entity =
|
|
|
|
|
InitializedEntity::InitializeResult(Loc, FnRetType, false);
|
|
|
|
|
S.PerformMoveOrCopyInitialization(Entity, nullptr, FnRetType, ReturnValue);
|
|
|
|
|
noteMemberDeclaredHere(S, ReturnValue, Fn);
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return false;
|
2017-05-23 04:22:23 +08:00
|
|
|
|
}
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-05-23 04:22:23 +08:00
|
|
|
|
auto *GroDecl = VarDecl::Create(
|
|
|
|
|
S.Context, &FD, FD.getLocation(), FD.getLocation(),
|
|
|
|
|
&S.PP.getIdentifierTable().get("__coro_gro"), GroType,
|
|
|
|
|
S.Context.getTrivialTypeSourceInfo(GroType, Loc), SC_None);
|
|
|
|
|
|
|
|
|
|
S.CheckVariableDeclarationType(GroDecl);
|
|
|
|
|
if (GroDecl->isInvalidDecl())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
InitializedEntity Entity = InitializedEntity::InitializeVariable(GroDecl);
|
|
|
|
|
ExprResult Res = S.PerformMoveOrCopyInitialization(Entity, nullptr, GroType,
|
|
|
|
|
this->ReturnValue);
|
|
|
|
|
if (Res.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Res = S.ActOnFinishFullExpr(Res.get());
|
|
|
|
|
if (Res.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
S.AddInitializerToDecl(GroDecl, Res.get(),
|
|
|
|
|
/*DirectInit=*/false);
|
|
|
|
|
|
|
|
|
|
S.FinalizeDeclaration(GroDecl);
|
|
|
|
|
|
|
|
|
|
// Form a declaration statement for the return declaration, so that AST
|
|
|
|
|
// visitors can more easily find it.
|
|
|
|
|
StmtResult GroDeclStmt =
|
|
|
|
|
S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(GroDecl), Loc, Loc);
|
|
|
|
|
if (GroDeclStmt.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
this->ResultDecl = GroDeclStmt.get();
|
|
|
|
|
|
|
|
|
|
ExprResult declRef = S.BuildDeclRefExpr(GroDecl, GroType, VK_LValue, Loc);
|
|
|
|
|
if (declRef.isInvalid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
StmtResult ReturnStmt = S.BuildReturnStmt(Loc, declRef.get());
|
|
|
|
|
if (ReturnStmt.isInvalid()) {
|
|
|
|
|
noteMemberDeclaredHere(S, ReturnValue, Fn);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-02-02 07:47:54 +08:00
|
|
|
|
if (cast<clang::ReturnStmt>(ReturnStmt.get())->getNRVOCandidate() == GroDecl)
|
|
|
|
|
GroDecl->setNRVOVariable(true);
|
2017-05-23 04:22:23 +08:00
|
|
|
|
|
|
|
|
|
this->ReturnStmt = ReturnStmt.get();
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-11-24 10:34:39 +08:00
|
|
|
|
|
2017-05-25 04:09:14 +08:00
|
|
|
|
// Create a static_cast\<T&&>(expr).
|
|
|
|
|
static Expr *castForMoving(Sema &S, Expr *E, QualType T = QualType()) {
|
|
|
|
|
if (T.isNull())
|
|
|
|
|
T = E->getType();
|
|
|
|
|
QualType TargetType = S.BuildReferenceType(
|
|
|
|
|
T, /*SpelledAsLValue*/ false, SourceLocation(), DeclarationName());
|
|
|
|
|
SourceLocation ExprLoc = E->getLocStart();
|
|
|
|
|
TypeSourceInfo *TargetLoc =
|
|
|
|
|
S.Context.getTrivialTypeSourceInfo(TargetType, ExprLoc);
|
|
|
|
|
|
|
|
|
|
return S
|
|
|
|
|
.BuildCXXNamedCast(ExprLoc, tok::kw_static_cast, TargetLoc, E,
|
|
|
|
|
SourceRange(ExprLoc, ExprLoc), E->getSourceRange())
|
|
|
|
|
.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Build a variable declaration for move parameter.
|
|
|
|
|
static VarDecl *buildVarDecl(Sema &S, SourceLocation Loc, QualType Type,
|
2017-06-03 08:22:18 +08:00
|
|
|
|
IdentifierInfo *II) {
|
2017-05-25 04:09:14 +08:00
|
|
|
|
TypeSourceInfo *TInfo = S.Context.getTrivialTypeSourceInfo(Type, Loc);
|
2018-01-25 06:15:42 +08:00
|
|
|
|
VarDecl *Decl = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, II, Type,
|
|
|
|
|
TInfo, SC_None);
|
2017-05-25 04:09:14 +08:00
|
|
|
|
Decl->setImplicit();
|
|
|
|
|
return Decl;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-25 06:15:42 +08:00
|
|
|
|
// Build statements that move coroutine function parameters to the coroutine
|
|
|
|
|
// frame, and store them on the function scope info.
|
|
|
|
|
bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) {
|
|
|
|
|
assert(isa<FunctionDecl>(CurContext) && "not in a function scope");
|
|
|
|
|
auto *FD = cast<FunctionDecl>(CurContext);
|
|
|
|
|
|
|
|
|
|
auto *ScopeInfo = getCurFunction();
|
|
|
|
|
assert(ScopeInfo->CoroutineParameterMoves.empty() &&
|
|
|
|
|
"Should not build parameter moves twice");
|
|
|
|
|
|
|
|
|
|
for (auto *PD : FD->parameters()) {
|
|
|
|
|
if (PD->getType()->isDependentType())
|
2017-05-25 04:09:14 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
ExprResult PDRefExpr =
|
|
|
|
|
BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(),
|
|
|
|
|
ExprValueKind::VK_LValue, Loc); // FIXME: scope?
|
|
|
|
|
if (PDRefExpr.isInvalid())
|
|
|
|
|
return false;
|
2017-05-25 04:09:14 +08:00
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
Expr *CExpr = nullptr;
|
|
|
|
|
if (PD->getType()->getAsCXXRecordDecl() ||
|
|
|
|
|
PD->getType()->isRValueReferenceType())
|
|
|
|
|
CExpr = castForMoving(*this, PDRefExpr.get());
|
|
|
|
|
else
|
|
|
|
|
CExpr = PDRefExpr.get();
|
2017-05-25 04:09:14 +08:00
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
auto D = buildVarDecl(*this, Loc, PD->getType(), PD->getIdentifier());
|
|
|
|
|
AddInitializerToDecl(D, CExpr, /*DirectInit=*/true);
|
2017-05-25 04:09:14 +08:00
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
// Convert decl to a statement.
|
|
|
|
|
StmtResult Stmt = ActOnDeclStmt(ConvertDeclToDeclGroup(D), Loc, Loc);
|
|
|
|
|
if (Stmt.isInvalid())
|
|
|
|
|
return false;
|
2017-05-25 04:09:14 +08:00
|
|
|
|
|
2018-02-16 04:37:22 +08:00
|
|
|
|
ScopeInfo->CoroutineParameterMoves.insert(std::make_pair(PD, Stmt.get()));
|
2017-05-25 04:09:14 +08:00
|
|
|
|
}
|
2017-02-13 13:05:02 +08:00
|
|
|
|
return true;
|
2015-10-22 14:13:50 +08:00
|
|
|
|
}
|
2017-03-07 07:38:15 +08:00
|
|
|
|
|
|
|
|
|
StmtResult Sema::BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs Args) {
|
|
|
|
|
CoroutineBodyStmt *Res = CoroutineBodyStmt::Create(Context, Args);
|
|
|
|
|
if (!Res)
|
|
|
|
|
return StmtError();
|
|
|
|
|
return Res;
|
|
|
|
|
}
|