Skip to content

Commit 3761319

Browse files
adding root signature parser
1 parent 36ceb48 commit 3761319

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

clang/include/clang/Parse/Parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,8 +3052,12 @@ class Parser : public CodeCompletionHandler {
30523052
void ParseHLSLAnnotations(ParsedAttributes &Attrs,
30533053
SourceLocation *EndLoc = nullptr,
30543054
bool CouldBeBitField = false);
3055+
30553056
Decl *ParseHLSLBuffer(SourceLocation &DeclEnd);
30563057

3058+
Attr *ParseHLSLRootSignature(StringRef Signature, ParsedAttributes &Attrs,
3059+
SourceLocation *EndLoc = nullptr);
3060+
30573061
void MaybeParseMicrosoftAttributes(ParsedAttributes &Attrs) {
30583062
if ((getLangOpts().MicrosoftExt || getLangOpts().HLSL) &&
30593063
Tok.is(tok::l_square)) {

clang/lib/Parse/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_clang_library(clangParse
1414
ParseExpr.cpp
1515
ParseExprCXX.cpp
1616
ParseHLSL.cpp
17+
ParseHLSLRootSignature.cpp
1718
ParseInit.cpp
1819
ParseObjc.cpp
1920
ParseOpenMP.cpp
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include "clang/AST/Attr.h"
2+
#include "clang/Parse/Parser.h"
3+
#include "llvm/ADT/StringRef.h"
4+
#include "llvm/ADT/StringSwitch.h"
5+
#include "llvm/Support/ErrorHandling.h"
6+
#include <cassert>
7+
#include <memory>
8+
9+
using namespace clang;
10+
11+
struct RSTknInfo {
12+
enum RSTok {
13+
RootFlags,
14+
RootConstants,
15+
RootCBV,
16+
RootSRV,
17+
RootUAV,
18+
DescriptorTable,
19+
StaticSampler,
20+
Number,
21+
Character,
22+
StrConst,
23+
EoF
24+
};
25+
26+
RSTknInfo() {}
27+
28+
RSTok Kind = RSTok::EoF;
29+
StringRef Text;
30+
};
31+
32+
class RootSignaturParser {
33+
StringRef Signature;
34+
35+
public:
36+
RootSignaturParser(StringRef Signature) : Signature(Signature) {}
37+
38+
void ParseRootDefinition() {
39+
do {
40+
getNextToken();
41+
42+
switch (CurTok.Kind) {
43+
44+
case RSTknInfo::RootFlags:
45+
getNextToken();
46+
assert(CurTok.Kind == RSTknInfo::Character && CurTok.Text == "(" &&
47+
"Missing tkn in root signature");
48+
49+
ParseRootFlag();
50+
51+
break;
52+
default:
53+
llvm_unreachable("Root Element still not suported");
54+
}
55+
} while (CurTok.Kind != RSTknInfo::EoF);
56+
}
57+
58+
private:
59+
RSTknInfo CurTok;
60+
std::string IdentifierStr;
61+
62+
void consumeWhitespace() { Signature = Signature.ltrim(" \t\v\f\r"); }
63+
64+
RSTknInfo gettok() {
65+
char LastChar = ' ';
66+
RSTknInfo Response;
67+
68+
while (isspace(LastChar))
69+
LastChar = Signature.front();
70+
71+
if (isalpha(LastChar)) {
72+
IdentifierStr = LastChar;
73+
while (isalnum((LastChar = Signature.front())))
74+
IdentifierStr += LastChar;
75+
76+
RSTknInfo::RSTok Tok =
77+
llvm::StringSwitch<RSTknInfo::RSTok>(IdentifierStr)
78+
.Case("RootFlags", RSTknInfo::RootFlags)
79+
.Case("RootConstants", RSTknInfo::RootConstants)
80+
.Case("RootCBV", RSTknInfo::RootCBV)
81+
.Case("RootSRV", RSTknInfo::RootSRV)
82+
.Case("RootUAV", RSTknInfo::RootUAV)
83+
.Case("DescriptorTable", RSTknInfo::DescriptorTable)
84+
.Case("StaticSampler", RSTknInfo::StaticSampler)
85+
.Default(RSTknInfo::StrConst);
86+
87+
Response.Kind = Tok;
88+
Response.Text = StringRef(IdentifierStr);
89+
return Response;
90+
}
91+
92+
if (isdigit(LastChar)) {
93+
std::string NumStr;
94+
95+
do {
96+
NumStr += LastChar;
97+
LastChar = Signature.front();
98+
} while (isdigit(LastChar));
99+
100+
Response.Kind = RSTknInfo::Number;
101+
Response.Text = StringRef(IdentifierStr);
102+
return Response;
103+
}
104+
105+
if (LastChar == EOF) {
106+
Response.Kind = RSTknInfo::EoF;
107+
return Response;
108+
}
109+
110+
Response.Kind = RSTknInfo::Character;
111+
Response.Text = StringRef(std::string(1, LastChar));
112+
return Response;
113+
}
114+
115+
RSTknInfo getNextToken() { return CurTok = gettok(); }
116+
};
117+
118+
Attr *Parser::ParseHLSLRootSignature(StringRef Signature,
119+
ParsedAttributes &Attrs,
120+
SourceLocation *EndLoc) {
121+
RootSignaturParser RSParser(Signature);
122+
RSParser.ParseRootDefinition();
123+
return nullptr;
124+
}

0 commit comments

Comments
 (0)