Skip to content

Commit 24144d7

Browse files
authored
[OpenACC][NFC] Implement basic OpenACC Sema infrastructure (#81874)
This patch is split off from #81659, and contains just the Sema infrastructure that we can later use to implement semantic analysis of OpenACC constructs.
1 parent f872706 commit 24144d7

File tree

5 files changed

+142
-9
lines changed

5 files changed

+142
-9
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3572,7 +3572,21 @@ class Parser : public CodeCompletionHandler {
35723572
StmtResult ParseOpenACCDirectiveStmt();
35733573

35743574
private:
3575-
void ParseOpenACCDirective();
3575+
/// A struct to hold the information that got parsed by ParseOpenACCDirective,
3576+
/// so that the callers of it can use that to construct the appropriate AST
3577+
/// nodes.
3578+
struct OpenACCDirectiveParseInfo {
3579+
OpenACCDirectiveKind DirKind;
3580+
SourceLocation StartLoc;
3581+
SourceLocation EndLoc;
3582+
// TODO OpenACC: Add Clause list here once we have a type for that.
3583+
// TODO OpenACC: As we implement support for the Atomic, Routine, Cache, and
3584+
// Wait constructs, we likely want to put that information in here as well.
3585+
};
3586+
3587+
/// Parses the OpenACC directive (the entire pragma) including the clause
3588+
/// list, but does not produce the main AST node.
3589+
OpenACCDirectiveParseInfo ParseOpenACCDirective();
35763590
/// Helper that parses an ID Expression based on the language options.
35773591
ExprResult ParseOpenACCIDExpression();
35783592
/// Parses the variable list for the `cache` construct.

clang/include/clang/Sema/Sema.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "clang/Basic/DarwinSDKInfo.h"
4242
#include "clang/Basic/ExpressionTraits.h"
4343
#include "clang/Basic/Module.h"
44+
#include "clang/Basic/OpenACCKinds.h"
4445
#include "clang/Basic/OpenCLOptions.h"
4546
#include "clang/Basic/OpenMPKinds.h"
4647
#include "clang/Basic/PragmaKinds.h"
@@ -12701,6 +12702,46 @@ class Sema final {
1270112702
OMPClause *ActOnOpenMPXBareClause(SourceLocation StartLoc,
1270212703
SourceLocation EndLoc);
1270312704

12705+
//===--------------------------------------------------------------------===//
12706+
// OpenACC directives and clauses.
12707+
12708+
/// Called after parsing an OpenACC Clause so that it can be checked.
12709+
bool ActOnOpenACCClause(OpenACCClauseKind ClauseKind,
12710+
SourceLocation StartLoc);
12711+
12712+
/// Called after the construct has been parsed, but clauses haven't been
12713+
/// parsed. This allows us to diagnose not-implemented, as well as set up any
12714+
/// state required for parsing the clauses.
12715+
void ActOnOpenACCConstruct(OpenACCDirectiveKind K, SourceLocation StartLoc);
12716+
12717+
/// Called after the directive, including its clauses, have been parsed and
12718+
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
12719+
/// happen before any associated declarations or statements have been parsed.
12720+
/// This function is only called when we are parsing a 'statement' context.
12721+
bool ActOnStartOpenACCStmtDirective(OpenACCDirectiveKind K,
12722+
SourceLocation StartLoc);
12723+
12724+
/// Called after the directive, including its clauses, have been parsed and
12725+
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
12726+
/// happen before any associated declarations or statements have been parsed.
12727+
/// This function is only called when we are parsing a 'Decl' context.
12728+
bool ActOnStartOpenACCDeclDirective(OpenACCDirectiveKind K,
12729+
SourceLocation StartLoc);
12730+
/// Called when we encounter an associated statement for our construct, this
12731+
/// should check legality of the statement as it appertains to this Construct.
12732+
StmtResult ActOnOpenACCAssociatedStmt(OpenACCDirectiveKind K,
12733+
StmtResult AssocStmt);
12734+
12735+
/// Called after the directive has been completely parsed, including the
12736+
/// declaration group or associated statement.
12737+
StmtResult ActOnEndOpenACCStmtDirective(OpenACCDirectiveKind K,
12738+
SourceLocation StartLoc,
12739+
SourceLocation EndLoc,
12740+
StmtResult AssocStmt);
12741+
/// Called after the directive has been completely parsed, including the
12742+
/// declaration group or associated statement.
12743+
DeclGroupRef ActOnEndOpenACCDeclDirective();
12744+
1270412745
/// The kind of conversion being performed.
1270512746
enum CheckedConversionKind {
1270612747
/// An implicit conversion.

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@ void SkipUntilEndOfDirective(Parser &P) {
550550
P.ConsumeAnyToken();
551551
}
552552

553+
bool doesDirectiveHaveAssociatedStmt(OpenACCDirectiveKind DirKind) {
554+
// TODO OpenACC: Implement.
555+
return false;
556+
}
557+
553558
} // namespace
554559

555560
// OpenACC 3.3, section 1.7:
@@ -745,9 +750,11 @@ bool Parser::ParseOpenACCClause(OpenACCDirectiveKind DirKind) {
745750
<< getCurToken().getIdentifierInfo();
746751

747752
// Consume the clause name.
748-
ConsumeToken();
753+
SourceLocation ClauseLoc = ConsumeToken();
749754

750-
return ParseOpenACCClauseParams(DirKind, Kind);
755+
bool Result = ParseOpenACCClauseParams(DirKind, Kind);
756+
getActions().ActOnOpenACCClause(Kind, ClauseLoc);
757+
return Result;
751758
}
752759

753760
bool Parser::ParseOpenACCClauseParams(OpenACCDirectiveKind DirKind,
@@ -1116,9 +1123,12 @@ void Parser::ParseOpenACCCacheVarList() {
11161123
}
11171124
}
11181125

1119-
void Parser::ParseOpenACCDirective() {
1126+
Parser::OpenACCDirectiveParseInfo Parser::ParseOpenACCDirective() {
1127+
SourceLocation StartLoc = getCurToken().getLocation();
11201128
OpenACCDirectiveKind DirKind = ParseOpenACCDirectiveKind(*this);
11211129

1130+
getActions().ActOnOpenACCConstruct(DirKind, StartLoc);
1131+
11221132
// Once we've parsed the construct/directive name, some have additional
11231133
// specifiers that need to be taken care of. Atomic has an 'atomic-clause'
11241134
// that needs to be parsed.
@@ -1175,7 +1185,10 @@ void Parser::ParseOpenACCDirective() {
11751185
Diag(getCurToken(), diag::warn_pragma_acc_unimplemented);
11761186
assert(Tok.is(tok::annot_pragma_openacc_end) &&
11771187
"Didn't parse all OpenACC Clauses");
1178-
ConsumeAnnotationToken();
1188+
SourceLocation EndLoc = ConsumeAnnotationToken();
1189+
assert(EndLoc.isValid());
1190+
1191+
return OpenACCDirectiveParseInfo{DirKind, StartLoc, EndLoc};
11791192
}
11801193

11811194
// Parse OpenACC directive on a declaration.
@@ -1185,9 +1198,14 @@ Parser::DeclGroupPtrTy Parser::ParseOpenACCDirectiveDecl() {
11851198
ParsingOpenACCDirectiveRAII DirScope(*this);
11861199
ConsumeAnnotationToken();
11871200

1188-
ParseOpenACCDirective();
1201+
OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
1202+
1203+
if (getActions().ActOnStartOpenACCDeclDirective(DirInfo.DirKind,
1204+
DirInfo.StartLoc))
1205+
return nullptr;
11891206

1190-
return nullptr;
1207+
// TODO OpenACC: Do whatever decl parsing is required here.
1208+
return DeclGroupPtrTy::make(getActions().ActOnEndOpenACCDeclDirective());
11911209
}
11921210

11931211
// Parse OpenACC Directive on a Statement.
@@ -1197,7 +1215,19 @@ StmtResult Parser::ParseOpenACCDirectiveStmt() {
11971215
ParsingOpenACCDirectiveRAII DirScope(*this);
11981216
ConsumeAnnotationToken();
11991217

1200-
ParseOpenACCDirective();
1218+
OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
1219+
if (getActions().ActOnStartOpenACCDeclDirective(DirInfo.DirKind,
1220+
DirInfo.StartLoc))
1221+
return StmtError();
1222+
1223+
StmtResult AssocStmt;
1224+
1225+
if (doesDirectiveHaveAssociatedStmt(DirInfo.DirKind)) {
1226+
ParsingOpenACCDirectiveRAII DirScope(*this, /*Value=*/false);
1227+
AssocStmt = getActions().ActOnOpenACCAssociatedStmt(DirInfo.DirKind,
1228+
ParseStatement());
1229+
}
12011230

1202-
return StmtEmpty();
1231+
return getActions().ActOnEndOpenACCStmtDirective(
1232+
DirInfo.DirKind, DirInfo.StartLoc, DirInfo.EndLoc, AssocStmt);
12031233
}

clang/lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ add_clang_library(clangSema
5252
SemaLookup.cpp
5353
SemaModule.cpp
5454
SemaObjCProperty.cpp
55+
SemaOpenACC.cpp
5556
SemaOpenMP.cpp
5657
SemaOverload.cpp
5758
SemaPseudoObject.cpp

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- SemaOpenACC.cpp - Semantic Analysis for OpenACC constructs -------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file implements semantic analysis for OpenACC constructs and
10+
/// clauses.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "clang/Basic/OpenACCKinds.h"
15+
#include "clang/Sema/Sema.h"
16+
17+
using namespace clang;
18+
bool Sema::ActOnOpenACCClause(OpenACCClauseKind ClauseKind,
19+
SourceLocation StartLoc) {
20+
return true;
21+
}
22+
void Sema::ActOnOpenACCConstruct(OpenACCDirectiveKind K,
23+
SourceLocation StartLoc) {}
24+
25+
bool Sema::ActOnStartOpenACCStmtDirective(OpenACCDirectiveKind K,
26+
SourceLocation StartLoc) {
27+
return true;
28+
}
29+
30+
StmtResult Sema::ActOnEndOpenACCStmtDirective(OpenACCDirectiveKind K,
31+
SourceLocation StartLoc,
32+
SourceLocation EndLoc,
33+
StmtResult AssocStmt) {
34+
return StmtEmpty();
35+
}
36+
37+
StmtResult Sema::ActOnOpenACCAssociatedStmt(OpenACCDirectiveKind K,
38+
StmtResult AssocStmt) {
39+
return AssocStmt;
40+
}
41+
42+
bool Sema::ActOnStartOpenACCDeclDirective(OpenACCDirectiveKind K,
43+
SourceLocation StartLoc) {
44+
return true;
45+
}
46+
47+
DeclGroupRef Sema::ActOnEndOpenACCDeclDirective() { return DeclGroupRef{}; }

0 commit comments

Comments
 (0)