Skip to content

[clang] Introduce SemaCoroutine #92645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 33 additions & 78 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class PseudoDestructorTypeStorage;
class PseudoObjectExpr;
class QualType;
class SemaCodeCompletion;
class SemaCoroutine;
class SemaCUDA;
class SemaHLSL;
class SemaObjC;
Expand Down Expand Up @@ -456,34 +457,33 @@ class Sema final : public SemaBase {
// 4. Availability Attribute Handling (SemaAvailability.cpp)
// 5. Casts (SemaCast.cpp)
// 6. Extra Semantic Checking (SemaChecking.cpp)
// 7. C++ Coroutines (SemaCoroutine.cpp)
// 8. C++ Scope Specifiers (SemaCXXScopeSpec.cpp)
// 9. Declarations (SemaDecl.cpp)
// 10. Declaration Attribute Handling (SemaDeclAttr.cpp)
// 11. C++ Declarations (SemaDeclCXX.cpp)
// 12. C++ Exception Specifications (SemaExceptionSpec.cpp)
// 13. Expressions (SemaExpr.cpp)
// 14. C++ Expressions (SemaExprCXX.cpp)
// 15. Member Access Expressions (SemaExprMember.cpp)
// 16. Initializers (SemaInit.cpp)
// 17. C++ Lambda Expressions (SemaLambda.cpp)
// 18. Name Lookup (SemaLookup.cpp)
// 19. Modules (SemaModule.cpp)
// 20. C++ Overloading (SemaOverload.cpp)
// 21. Pseudo-Object (SemaPseudoObject.cpp)
// 22. Statements (SemaStmt.cpp)
// 23. `inline asm` Statement (SemaStmtAsm.cpp)
// 24. Statement Attribute Handling (SemaStmtAttr.cpp)
// 25. C++ Templates (SemaTemplate.cpp)
// 26. C++ Template Argument Deduction (SemaTemplateDeduction.cpp)
// 27. C++ Template Instantiation (SemaTemplateInstantiate.cpp)
// 28. C++ Template Declaration Instantiation
// 7. C++ Scope Specifiers (SemaCXXScopeSpec.cpp)
// 8. Declarations (SemaDecl.cpp)
// 9. Declaration Attribute Handling (SemaDeclAttr.cpp)
// 10. C++ Declarations (SemaDeclCXX.cpp)
// 11. C++ Exception Specifications (SemaExceptionSpec.cpp)
// 12. Expressions (SemaExpr.cpp)
// 13. C++ Expressions (SemaExprCXX.cpp)
// 14. Member Access Expressions (SemaExprMember.cpp)
// 15. Initializers (SemaInit.cpp)
// 16. C++ Lambda Expressions (SemaLambda.cpp)
// 17. Name Lookup (SemaLookup.cpp)
// 18. Modules (SemaModule.cpp)
// 19. C++ Overloading (SemaOverload.cpp)
// 20. Pseudo-Object (SemaPseudoObject.cpp)
// 21. Statements (SemaStmt.cpp)
// 22. `inline asm` Statement (SemaStmtAsm.cpp)
// 23. Statement Attribute Handling (SemaStmtAttr.cpp)
// 24. C++ Templates (SemaTemplate.cpp)
// 25. C++ Template Argument Deduction (SemaTemplateDeduction.cpp)
// 26. C++ Template Instantiation (SemaTemplateInstantiate.cpp)
// 27. C++ Template Declaration Instantiation
// (SemaTemplateInstantiateDecl.cpp)
// 29. C++ Variadic Templates (SemaTemplateVariadic.cpp)
// 30. Constraints and Concepts (SemaConcept.cpp)
// 31. Types (SemaType.cpp)
// 32. FixIt Helpers (SemaFixItUtils.cpp)
// 33. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
// 28. C++ Variadic Templates (SemaTemplateVariadic.cpp)
// 29. Constraints and Concepts (SemaConcept.cpp)
// 30. Types (SemaType.cpp)
// 31. FixIt Helpers (SemaFixItUtils.cpp)
// 32. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)

/// \name Semantic Analysis
/// Implementations are in Sema.cpp
Expand Down Expand Up @@ -989,6 +989,11 @@ class Sema final : public SemaBase {
return *CodeCompletionPtr;
}

SemaCoroutine &Coroutine() {
assert(CoroutinePtr);
return *CoroutinePtr;
}

SemaCUDA &CUDA() {
assert(CUDAPtr);
return *CUDAPtr;
Expand Down Expand Up @@ -1050,6 +1055,7 @@ class Sema final : public SemaBase {
mutable IdentifierInfo *Ident_super;

std::unique_ptr<SemaCodeCompletion> CodeCompletionPtr;
std::unique_ptr<SemaCoroutine> CoroutinePtr;
std::unique_ptr<SemaCUDA> CUDAPtr;
std::unique_ptr<SemaHLSL> HLSLPtr;
std::unique_ptr<SemaObjC> ObjCPtr;
Expand Down Expand Up @@ -2267,57 +2273,6 @@ class Sema final : public SemaBase {
//
//

/// \name C++ Coroutines
/// Implementations are in SemaCoroutine.cpp
///@{

public:
/// The C++ "std::coroutine_traits" template, which is defined in
/// \<coroutine_traits>
ClassTemplateDecl *StdCoroutineTraitsCache;

bool ActOnCoroutineBodyStart(Scope *S, SourceLocation KwLoc,
StringRef Keyword);
ExprResult ActOnCoawaitExpr(Scope *S, SourceLocation KwLoc, Expr *E);
ExprResult ActOnCoyieldExpr(Scope *S, SourceLocation KwLoc, Expr *E);
StmtResult ActOnCoreturnStmt(Scope *S, SourceLocation KwLoc, Expr *E);

ExprResult BuildOperatorCoawaitLookupExpr(Scope *S, SourceLocation Loc);
ExprResult BuildOperatorCoawaitCall(SourceLocation Loc, Expr *E,
UnresolvedLookupExpr *Lookup);
ExprResult BuildResolvedCoawaitExpr(SourceLocation KwLoc, Expr *Operand,
Expr *Awaiter, bool IsImplicit = false);
ExprResult BuildUnresolvedCoawaitExpr(SourceLocation KwLoc, Expr *Operand,
UnresolvedLookupExpr *Lookup);
ExprResult BuildCoyieldExpr(SourceLocation KwLoc, Expr *E);
StmtResult BuildCoreturnStmt(SourceLocation KwLoc, Expr *E,
bool IsImplicit = false);
StmtResult BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs);
bool buildCoroutineParameterMoves(SourceLocation Loc);
VarDecl *buildCoroutinePromise(SourceLocation Loc);
void CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body);

// As a clang extension, enforces that a non-coroutine function must be marked
// with [[clang::coro_wrapper]] if it returns a type marked with
// [[clang::coro_return_type]].
// Expects that FD is not a coroutine.
void CheckCoroutineWrapper(FunctionDecl *FD);
/// Lookup 'coroutine_traits' in std namespace and std::experimental
/// namespace. The namespace found is recorded in Namespace.
ClassTemplateDecl *lookupCoroutineTraits(SourceLocation KwLoc,
SourceLocation FuncLoc);
/// Check that the expression co_await promise.final_suspend() shall not be
/// potentially-throwing.
bool checkFinalSuspendNoThrow(const Stmt *FinalSuspend);

///@}

//
//
// -------------------------------------------------------------------------
//
//

/// \name C++ Scope Specifiers
/// Implementations are in SemaCXXScopeSpec.cpp
///@{
Expand Down
73 changes: 73 additions & 0 deletions clang/include/clang/Sema/SemaCoroutine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//===----- SemaCUDA.h ----- Semantic Analysis for C++20 coroutines --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
/// This file declares semantic analysis for C++20 coroutines.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_SEMA_SEMACOROUTINE_H
#define LLVM_CLANG_SEMA_SEMACOROUTINE_H

#include "clang/AST/Decl.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/StmtCXX.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Sema/Ownership.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaBase.h"

namespace clang {
class SemaCoroutine : public SemaBase {
public:
SemaCoroutine(Sema &S);

/// The C++ "std::coroutine_traits" template, which is defined in
/// \<coroutine_traits>
ClassTemplateDecl *StdCoroutineTraitsCache;

bool ActOnCoroutineBodyStart(Scope *S, SourceLocation KwLoc,
StringRef Keyword);
ExprResult ActOnCoawaitExpr(Scope *S, SourceLocation KwLoc, Expr *E);
ExprResult ActOnCoyieldExpr(Scope *S, SourceLocation KwLoc, Expr *E);
StmtResult ActOnCoreturnStmt(Scope *S, SourceLocation KwLoc, Expr *E);

ExprResult BuildOperatorCoawaitLookupExpr(Scope *S, SourceLocation Loc);
ExprResult BuildOperatorCoawaitCall(SourceLocation Loc, Expr *E,
UnresolvedLookupExpr *Lookup);
ExprResult BuildResolvedCoawaitExpr(SourceLocation KwLoc, Expr *Operand,
Expr *Awaiter, bool IsImplicit = false);
ExprResult BuildUnresolvedCoawaitExpr(SourceLocation KwLoc, Expr *Operand,
UnresolvedLookupExpr *Lookup);
ExprResult BuildCoyieldExpr(SourceLocation KwLoc, Expr *E);
StmtResult BuildCoreturnStmt(SourceLocation KwLoc, Expr *E,
bool IsImplicit = false);
StmtResult BuildCoroutineBodyStmt(CoroutineBodyStmt::CtorArgs);
bool buildCoroutineParameterMoves(SourceLocation Loc);
VarDecl *buildCoroutinePromise(SourceLocation Loc);
void CheckCompletedCoroutineBody(FunctionDecl *FD, Stmt *&Body);

// As a clang extension, enforces that a non-coroutine function must be marked
// with [[clang::coro_wrapper]] if it returns a type marked with
// [[clang::coro_return_type]].
// Expects that FD is not a coroutine.
void CheckCoroutineWrapper(FunctionDecl *FD);
/// Lookup 'coroutine_traits' in std namespace and std::experimental
/// namespace. The namespace found is recorded in Namespace.
ClassTemplateDecl *lookupCoroutineTraits(SourceLocation KwLoc,
SourceLocation FuncLoc);
/// Check that the expression co_await promise.final_suspend() shall not be
/// potentially-throwing.
bool checkFinalSuspendNoThrow(const Stmt *FinalSuspend);
};
} // namespace clang

#endif // LLVM_CLANG_SEMA_SEMACOROUTINE_H
4 changes: 3 additions & 1 deletion clang/lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaCUDA.h"
#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaCoroutine.h"
#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/SemaOpenACC.h"
#include "clang/Sema/SemaOpenMP.h"
Expand Down Expand Up @@ -1465,7 +1466,8 @@ ExprResult Parser::ParseCastExpression(CastParseKind ParseKind,
SourceLocation CoawaitLoc = ConsumeToken();
Res = ParseCastExpression(AnyCastExpr);
if (!Res.isInvalid())
Res = Actions.ActOnCoawaitExpr(getCurScope(), CoawaitLoc, Res.get());
Res = Actions.Coroutine().ActOnCoawaitExpr(getCurScope(), CoawaitLoc,
Res.get());
return Res;
}

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Parse/ParseExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaCoroutine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include <numeric>
Expand Down Expand Up @@ -1977,7 +1978,7 @@ ExprResult Parser::ParseCoyieldExpression() {
ExprResult Expr = Tok.is(tok::l_brace) ? ParseBraceInitializer()
: ParseAssignmentExpression();
if (!Expr.isInvalid())
Expr = Actions.ActOnCoyieldExpr(getCurScope(), Loc, Expr.get());
Expr = Actions.Coroutine().ActOnCoyieldExpr(getCurScope(), Loc, Expr.get());
return Expr;
}

Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clang/Sema/EnterExpressionEvaluationContext.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaCoroutine.h"
#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/SemaOpenMP.h"
#include "clang/Sema/TypoCorrection.h"
Expand Down Expand Up @@ -2456,7 +2457,8 @@ StmtResult Parser::ParseReturnStatement() {
}
}
if (IsCoreturn)
return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
return Actions.Coroutine().ActOnCoreturnStmt(getCurScope(), ReturnLoc,
R.get());
return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
}

Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Sema/Sema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "clang/Sema/SemaCUDA.h"
#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaConsumer.h"
#include "clang/Sema/SemaCoroutine.h"
#include "clang/Sema/SemaHLSL.h"
#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/SemaObjC.h"
Expand Down Expand Up @@ -205,6 +206,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
CurScope(nullptr), Ident_super(nullptr),
CodeCompletionPtr(
std::make_unique<SemaCodeCompletion>(*this, CodeCompleter)),
CoroutinePtr(std::make_unique<SemaCoroutine>(*this)),
CUDAPtr(std::make_unique<SemaCUDA>(*this)),
HLSLPtr(std::make_unique<SemaHLSL>(*this)),
ObjCPtr(std::make_unique<SemaObjC>(*this)),
Expand All @@ -219,8 +221,8 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
CodeSegStack(nullptr), StrictGuardStackCheckStack(false),
FpPragmaStack(FPOptionsOverride()), CurInitSeg(nullptr),
VisContext(nullptr), PragmaAttributeCurrentTargetDecl(nullptr),
StdCoroutineTraitsCache(nullptr), IdResolver(pp),
OriginalLexicalContext(nullptr), StdInitializerList(nullptr),
IdResolver(pp), OriginalLexicalContext(nullptr),
StdInitializerList(nullptr),
FullyCheckedComparisonCategories(
static_cast<unsigned>(ComparisonCategoryType::Last) + 1),
StdSourceLocationImplDecl(nullptr), CXXTypeInfoDecl(nullptr),
Expand Down
Loading
Loading