summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-08-09 21:52:13 -0700
committerGitHub <noreply@github.com>2023-08-09 21:52:13 -0700
commitd4e72a93e13ab880b2f355fb7fb5d55cf0113c3d (patch)
treee252c9164da2aa2632350abab9cb73d2a524533c
parentf875d3f5ba9c1ddc6aa9a0960efd5ab27ae4e4c9 (diff)
Support nested structs in function bodies. (#3078)
-rw-r--r--source/slang/slang-parser.cpp19
-rw-r--r--tests/language-feature/struct-in-func.slang24
-rw-r--r--tests/language-feature/struct-in-func.slang.expected.txt1
3 files changed, 44 insertions, 0 deletions
diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp
index 1836fd550..80d14795c 100644
--- a/source/slang/slang-parser.cpp
+++ b/source/slang/slang-parser.cpp
@@ -4581,6 +4581,25 @@ namespace Slang
Token closingBraceToken;
while (!AdvanceIfMatch(this, MatchedTokenType::CurlyBraces, &closingBraceToken))
{
+ if (LookAheadToken("struct"))
+ {
+ auto structDecl = ParseStruct();
+ AddMember(scopeDecl, structDecl);
+ continue;
+ }
+ else if (AdvanceIf(this, "typedef"))
+ {
+ auto typeDefDecl = parseTypeDef(this, nullptr);
+ AddMember(scopeDecl, (Decl*)typeDefDecl);
+ continue;
+ }
+ else if (AdvanceIf(this, "typealias"))
+ {
+ auto typeDefDecl = parseTypeAliasDecl(this, nullptr);
+ AddMember(scopeDecl, (Decl*)typeDefDecl);
+ continue;
+ }
+
auto stmt = ParseStatement();
if(stmt)
{
diff --git a/tests/language-feature/struct-in-func.slang b/tests/language-feature/struct-in-func.slang
new file mode 100644
index 000000000..d3bd046f0
--- /dev/null
+++ b/tests/language-feature/struct-in-func.slang
@@ -0,0 +1,24 @@
+//TEST(compute):COMPARE_COMPUTE: -shaderobj
+//TEST(compute):COMPARE_COMPUTE: -vk -shaderobj
+
+int test(int r)
+{
+ struct Val
+ {
+ int a;
+ int b;
+ }
+ Val a;
+ a.a = 1;
+ a.b = 2;
+ return a.a + a.b;
+}
+
+//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer;
+
+[numthreads(1, 1, 1)]
+void computeMain(int3 dispatchThreadID: SV_DispatchThreadID)
+{
+ outputBuffer[0] = test(dispatchThreadID.x);
+}
diff --git a/tests/language-feature/struct-in-func.slang.expected.txt b/tests/language-feature/struct-in-func.slang.expected.txt
new file mode 100644
index 000000000..e440e5c84
--- /dev/null
+++ b/tests/language-feature/struct-in-func.slang.expected.txt
@@ -0,0 +1 @@
+3 \ No newline at end of file