Skip to content

Commit 26fee0f

Browse files
authored
[OpenACC] Implement Sema work for OpenACC Clauses (#87821)
Now that we have AST nodes for OpenACC Clauses, this patch adds their creation to Sema and makes the Parser call all the required functions. This also redoes TreeTransform to work with the clauses/make sure they are transformed. Much of this is NFC, since there is no clause we can test this behavior with. However, there IS one noticable change; we are now no longer diagnosing that a clause is 'not implemented' unless it there was no errors parsing its parameters. This is because it cleans up how we create and diagnose clauses.
1 parent b439140 commit 26fee0f

File tree

9 files changed

+490
-500
lines changed

9 files changed

+490
-500
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12252,6 +12252,8 @@ def warn_acc_clause_unimplemented
1225212252
def err_acc_construct_appertainment
1225312253
: Error<"OpenACC construct '%0' cannot be used here; it can only "
1225412254
"be used in a statement context">;
12255+
def err_acc_clause_appertainment
12256+
: Error<"OpenACC '%1' clause is not valid on '%0' directive">;
1225512257
def err_acc_branch_in_out_compute_construct
1225612258
: Error<"invalid %select{branch|return|throw}0 %select{out of|into}1 "
1225712259
"OpenACC Compute Construct">;

clang/include/clang/Parse/Parser.h

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace clang {
4141
class InMessageExpressionRAIIObject;
4242
class PoisonSEHIdentifiersRAIIObject;
4343
class OMPClause;
44+
class OpenACCClause;
4445
class ObjCTypeParamList;
4546
struct OMPTraitProperty;
4647
struct OMPTraitSelector;
@@ -3594,11 +3595,26 @@ class Parser : public CodeCompletionHandler {
35943595
OpenACCDirectiveKind DirKind;
35953596
SourceLocation StartLoc;
35963597
SourceLocation EndLoc;
3597-
// TODO OpenACC: Add Clause list here once we have a type for that.
3598+
SmallVector<OpenACCClause *> Clauses;
35983599
// TODO OpenACC: As we implement support for the Atomic, Routine, Cache, and
35993600
// Wait constructs, we likely want to put that information in here as well.
36003601
};
36013602

3603+
/// Represents the 'error' state of parsing an OpenACC Clause, and stores
3604+
/// whether we can continue parsing, or should give up on the directive.
3605+
enum class OpenACCParseCanContinue { Cannot = 0, Can = 1 };
3606+
3607+
/// A type to represent the state of parsing an OpenACC Clause. Situations
3608+
/// that result in an OpenACCClause pointer are a success and can continue
3609+
/// parsing, however some other situations can also continue.
3610+
/// FIXME: This is better represented as a std::expected when we get C++23.
3611+
using OpenACCClauseParseResult =
3612+
llvm::PointerIntPair<OpenACCClause *, 1, OpenACCParseCanContinue>;
3613+
3614+
OpenACCClauseParseResult OpenACCCanContinue();
3615+
OpenACCClauseParseResult OpenACCCannotContinue();
3616+
OpenACCClauseParseResult OpenACCSuccess(OpenACCClause *Clause);
3617+
36023618
/// Parses the OpenACC directive (the entire pragma) including the clause
36033619
/// list, but does not produce the main AST node.
36043620
OpenACCDirectiveParseInfo ParseOpenACCDirective();
@@ -3613,12 +3629,18 @@ class Parser : public CodeCompletionHandler {
36133629
bool ParseOpenACCClauseVarList(OpenACCClauseKind Kind);
36143630
/// Parses any parameters for an OpenACC Clause, including required/optional
36153631
/// parens.
3616-
bool ParseOpenACCClauseParams(OpenACCDirectiveKind DirKind,
3617-
OpenACCClauseKind Kind);
3618-
/// Parses a single clause in a clause-list for OpenACC.
3619-
bool ParseOpenACCClause(OpenACCDirectiveKind DirKind);
3632+
OpenACCClauseParseResult
3633+
ParseOpenACCClauseParams(ArrayRef<const OpenACCClause *> ExistingClauses,
3634+
OpenACCDirectiveKind DirKind, OpenACCClauseKind Kind,
3635+
SourceLocation ClauseLoc);
3636+
/// Parses a single clause in a clause-list for OpenACC. Returns nullptr on
3637+
/// error.
3638+
OpenACCClauseParseResult
3639+
ParseOpenACCClause(ArrayRef<const OpenACCClause *> ExistingClauses,
3640+
OpenACCDirectiveKind DirKind);
36203641
/// Parses the clause-list for an OpenACC directive.
3621-
void ParseOpenACCClauseList(OpenACCDirectiveKind DirKind);
3642+
SmallVector<OpenACCClause *>
3643+
ParseOpenACCClauseList(OpenACCDirectiveKind DirKind);
36223644
bool ParseOpenACCWaitArgument();
36233645
/// Parses the clause of the 'bind' argument, which can be a string literal or
36243646
/// an ID expression.

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,46 @@
2121
#include "clang/Sema/SemaBase.h"
2222

2323
namespace clang {
24+
class OpenACCClause;
2425

2526
class SemaOpenACC : public SemaBase {
2627
public:
28+
/// A type to represent all the data for an OpenACC Clause that has been
29+
/// parsed, but not yet created/semantically analyzed. This is effectively a
30+
/// discriminated union on the 'Clause Kind', with all of the individual
31+
/// clause details stored in a std::variant.
32+
class OpenACCParsedClause {
33+
OpenACCDirectiveKind DirKind;
34+
OpenACCClauseKind ClauseKind;
35+
SourceRange ClauseRange;
36+
SourceLocation LParenLoc;
37+
38+
// TODO OpenACC: Add variant here to store details of individual clauses.
39+
40+
public:
41+
OpenACCParsedClause(OpenACCDirectiveKind DirKind,
42+
OpenACCClauseKind ClauseKind, SourceLocation BeginLoc)
43+
: DirKind(DirKind), ClauseKind(ClauseKind), ClauseRange(BeginLoc, {}) {}
44+
45+
OpenACCDirectiveKind getDirectiveKind() const { return DirKind; }
46+
47+
OpenACCClauseKind getClauseKind() const { return ClauseKind; }
48+
49+
SourceLocation getBeginLoc() const { return ClauseRange.getBegin(); }
50+
51+
SourceLocation getLParenLoc() const { return LParenLoc; }
52+
53+
SourceLocation getEndLoc() const { return ClauseRange.getEnd(); }
54+
55+
void setLParenLoc(SourceLocation EndLoc) { LParenLoc = EndLoc; }
56+
void setEndLoc(SourceLocation EndLoc) { ClauseRange.setEnd(EndLoc); }
57+
};
58+
2759
SemaOpenACC(Sema &S);
2860

2961
/// Called after parsing an OpenACC Clause so that it can be checked.
30-
bool ActOnClause(OpenACCClauseKind ClauseKind, SourceLocation StartLoc);
62+
OpenACCClause *ActOnClause(ArrayRef<const OpenACCClause *> ExistingClauses,
63+
OpenACCParsedClause &Clause);
3164

3265
/// Called after the construct has been parsed, but clauses haven't been
3366
/// parsed. This allows us to diagnose not-implemented, as well as set up any
@@ -53,7 +86,10 @@ class SemaOpenACC : public SemaBase {
5386
/// declaration group or associated statement.
5487
StmtResult ActOnEndStmtDirective(OpenACCDirectiveKind K,
5588
SourceLocation StartLoc,
56-
SourceLocation EndLoc, StmtResult AssocStmt);
89+
SourceLocation EndLoc,
90+
ArrayRef<OpenACCClause *> Clauses,
91+
StmtResult AssocStmt);
92+
5793
/// Called after the directive has been completely parsed, including the
5894
/// declaration group or associated statement.
5995
DeclGroupRef ActOnEndDeclDirective();

0 commit comments

Comments
 (0)