Skip to content

Commit d345f6a

Browse files
authored
[clang] Introduce SemaHLSL (#87912)
This patch introduces `SemaHLSL` class, and moves some HLSL-related functions there. No functional changes intended. Removing "HLSL" from function names inside `SemaHLSL` is left for a subsequent PR by HLSL contributors, if they deem that desirable. This is a part of the effort to split `Sema` into smaller manageable parts, and follows the example of OpenACC. See #82217, #84184, #87634 for additional context.
1 parent 39f6d01 commit d345f6a

File tree

5 files changed

+73
-41
lines changed

5 files changed

+73
-41
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class Preprocessor;
182182
class PseudoDestructorTypeStorage;
183183
class PseudoObjectExpr;
184184
class QualType;
185+
class SemaHLSL;
185186
class SemaOpenACC;
186187
class StandardConversionSequence;
187188
class Stmt;
@@ -465,9 +466,8 @@ class Sema final : public SemaBase {
465466
// 36. FixIt Helpers (SemaFixItUtils.cpp)
466467
// 37. Name Lookup for RISC-V Vector Intrinsic (SemaRISCVVectorLookup.cpp)
467468
// 38. CUDA (SemaCUDA.cpp)
468-
// 39. HLSL Constructs (SemaHLSL.cpp)
469-
// 40. OpenMP Directives and Clauses (SemaOpenMP.cpp)
470-
// 41. SYCL Constructs (SemaSYCL.cpp)
469+
// 39. OpenMP Directives and Clauses (SemaOpenMP.cpp)
470+
// 40. SYCL Constructs (SemaSYCL.cpp)
471471

472472
/// \name Semantic Analysis
473473
/// Implementations are in Sema.cpp
@@ -964,6 +964,11 @@ class Sema final : public SemaBase {
964964
/// CurContext - This is the current declaration context of parsing.
965965
DeclContext *CurContext;
966966

967+
SemaHLSL &HLSL() {
968+
assert(HLSLPtr);
969+
return *HLSLPtr;
970+
}
971+
967972
SemaOpenACC &OpenACC() {
968973
assert(OpenACCPtr);
969974
return *OpenACCPtr;
@@ -999,6 +1004,7 @@ class Sema final : public SemaBase {
9991004

10001005
mutable IdentifierInfo *Ident_super;
10011006

1007+
std::unique_ptr<SemaHLSL> HLSLPtr;
10021008
std::unique_ptr<SemaOpenACC> OpenACCPtr;
10031009

10041010
///@}
@@ -1967,6 +1973,11 @@ class Sema final : public SemaBase {
19671973
bool CheckFunctionCall(FunctionDecl *FDecl, CallExpr *TheCall,
19681974
const FunctionProtoType *Proto);
19691975

1976+
bool BuiltinVectorMath(CallExpr *TheCall, QualType &Res);
1977+
bool BuiltinVectorToScalarMath(CallExpr *TheCall);
1978+
1979+
bool CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
1980+
19701981
private:
19711982
void CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
19721983
const ArraySubscriptExpr *ASE = nullptr,
@@ -13145,29 +13156,6 @@ class Sema final : public SemaBase {
1314513156
//
1314613157
//
1314713158

13148-
/// \name HLSL Constructs
13149-
/// Implementations are in SemaHLSL.cpp
13150-
///@{
13151-
13152-
public:
13153-
Decl *ActOnStartHLSLBuffer(Scope *BufferScope, bool CBuffer,
13154-
SourceLocation KwLoc, IdentifierInfo *Ident,
13155-
SourceLocation IdentLoc, SourceLocation LBrace);
13156-
void ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace);
13157-
13158-
bool CheckHLSLBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
13159-
13160-
bool BuiltinVectorMath(CallExpr *TheCall, QualType &Res);
13161-
bool BuiltinVectorToScalarMath(CallExpr *TheCall);
13162-
13163-
///@}
13164-
13165-
//
13166-
//
13167-
// -------------------------------------------------------------------------
13168-
//
13169-
//
13170-
1317113159
/// \name OpenMP Directives and Clauses
1317213160
/// Implementations are in SemaOpenMP.cpp
1317313161
///@{

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----- SemaHLSL.h ----- Semantic Analysis for HLSL 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 declares semantic analysis for HLSL constructs.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CLANG_SEMA_SEMAHLSL_H
14+
#define LLVM_CLANG_SEMA_SEMAHLSL_H
15+
16+
#include "clang/AST/DeclBase.h"
17+
#include "clang/AST/Expr.h"
18+
#include "clang/Basic/IdentifierTable.h"
19+
#include "clang/Basic/SourceLocation.h"
20+
#include "clang/Sema/Scope.h"
21+
#include "clang/Sema/SemaBase.h"
22+
23+
namespace clang {
24+
25+
class SemaHLSL : public SemaBase {
26+
public:
27+
SemaHLSL(Sema &S);
28+
29+
Decl *ActOnStartHLSLBuffer(Scope *BufferScope, bool CBuffer,
30+
SourceLocation KwLoc, IdentifierInfo *Ident,
31+
SourceLocation IdentLoc, SourceLocation LBrace);
32+
void ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace);
33+
};
34+
35+
} // namespace clang
36+
37+
#endif // LLVM_CLANG_SEMA_SEMAHLSL_H

clang/lib/Parse/ParseHLSL.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/Parse/ParseDiagnostic.h"
1616
#include "clang/Parse/Parser.h"
1717
#include "clang/Parse/RAIIObjectsForParser.h"
18+
#include "clang/Sema/SemaHLSL.h"
1819

1920
using namespace clang;
2021

@@ -71,9 +72,9 @@ Decl *Parser::ParseHLSLBuffer(SourceLocation &DeclEnd) {
7172
return nullptr;
7273
}
7374

74-
Decl *D = Actions.ActOnStartHLSLBuffer(getCurScope(), IsCBuffer, BufferLoc,
75-
Identifier, IdentifierLoc,
76-
T.getOpenLocation());
75+
Decl *D = Actions.HLSL().ActOnStartHLSLBuffer(
76+
getCurScope(), IsCBuffer, BufferLoc, Identifier, IdentifierLoc,
77+
T.getOpenLocation());
7778

7879
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
7980
// FIXME: support attribute on constants inside cbuffer/tbuffer.
@@ -87,15 +88,15 @@ Decl *Parser::ParseHLSLBuffer(SourceLocation &DeclEnd) {
8788
T.skipToEnd();
8889
DeclEnd = T.getCloseLocation();
8990
BufferScope.Exit();
90-
Actions.ActOnFinishHLSLBuffer(D, DeclEnd);
91+
Actions.HLSL().ActOnFinishHLSLBuffer(D, DeclEnd);
9192
return nullptr;
9293
}
9394
}
9495

9596
T.consumeClose();
9697
DeclEnd = T.getCloseLocation();
9798
BufferScope.Exit();
98-
Actions.ActOnFinishHLSLBuffer(D, DeclEnd);
99+
Actions.HLSL().ActOnFinishHLSLBuffer(D, DeclEnd);
99100

100101
Actions.ProcessDeclAttributeList(Actions.CurScope, D, Attrs);
101102
return D;

clang/lib/Sema/Sema.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "clang/Sema/Scope.h"
4343
#include "clang/Sema/ScopeInfo.h"
4444
#include "clang/Sema/SemaConsumer.h"
45+
#include "clang/Sema/SemaHLSL.h"
4546
#include "clang/Sema/SemaInternal.h"
4647
#include "clang/Sema/SemaOpenACC.h"
4748
#include "clang/Sema/TemplateDeduction.h"
@@ -198,6 +199,7 @@ Sema::Sema(Preprocessor &pp, ASTContext &ctxt, ASTConsumer &consumer,
198199
LateTemplateParser(nullptr), LateTemplateParserCleanup(nullptr),
199200
OpaqueParser(nullptr), CurContext(nullptr), ExternalSource(nullptr),
200201
CurScope(nullptr), Ident_super(nullptr),
202+
HLSLPtr(std::make_unique<SemaHLSL>(*this)),
201203
OpenACCPtr(std::make_unique<SemaOpenACC>(*this)),
202204
MSPointerToMemberRepresentationMethod(
203205
LangOpts.getMSPointerToMemberRepresentationMethod()),

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
// This implements Semantic Analysis for HLSL constructs.
99
//===----------------------------------------------------------------------===//
1010

11+
#include "clang/Sema/SemaHLSL.h"
1112
#include "clang/Sema/Sema.h"
1213

1314
using namespace clang;
1415

15-
Decl *Sema::ActOnStartHLSLBuffer(Scope *BufferScope, bool CBuffer,
16-
SourceLocation KwLoc, IdentifierInfo *Ident,
17-
SourceLocation IdentLoc,
18-
SourceLocation LBrace) {
16+
SemaHLSL::SemaHLSL(Sema &S) : SemaBase(S) {}
17+
18+
Decl *SemaHLSL::ActOnStartHLSLBuffer(Scope *BufferScope, bool CBuffer,
19+
SourceLocation KwLoc,
20+
IdentifierInfo *Ident,
21+
SourceLocation IdentLoc,
22+
SourceLocation LBrace) {
1923
// For anonymous namespace, take the location of the left brace.
20-
DeclContext *LexicalParent = getCurLexicalContext();
24+
DeclContext *LexicalParent = SemaRef.getCurLexicalContext();
2125
HLSLBufferDecl *Result = HLSLBufferDecl::Create(
22-
Context, LexicalParent, CBuffer, KwLoc, Ident, IdentLoc, LBrace);
26+
getASTContext(), LexicalParent, CBuffer, KwLoc, Ident, IdentLoc, LBrace);
2327

24-
PushOnScopeChains(Result, BufferScope);
25-
PushDeclContext(BufferScope, Result);
28+
SemaRef.PushOnScopeChains(Result, BufferScope);
29+
SemaRef.PushDeclContext(BufferScope, Result);
2630

2731
return Result;
2832
}
2933

30-
void Sema::ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace) {
34+
void SemaHLSL::ActOnFinishHLSLBuffer(Decl *Dcl, SourceLocation RBrace) {
3135
auto *BufDecl = cast<HLSLBufferDecl>(Dcl);
3236
BufDecl->setRBraceLoc(RBrace);
33-
PopDeclContext();
37+
SemaRef.PopDeclContext();
3438
}

0 commit comments

Comments
 (0)