Skip to content

Commit

Permalink
Allow a enum case to reference a previously defined value. (#4768)
Browse files Browse the repository at this point in the history
  • Loading branch information
csyonghe authored Aug 1, 2024
1 parent 69dd7f4 commit d63f5e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ namespace Slang
if(as<ConstructorDecl>(decl))
return true;

if (as<EnumCaseDecl>(decl))
return true;

// Things nested inside functions may have dependencies
// on values from the enclosing scope, but this needs to
// be dealt with via "capture" so they are also effectively
Expand Down
2 changes: 2 additions & 0 deletions source/slang/slang-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4945,6 +4945,7 @@ namespace Slang
parseOptionalInheritanceClause(parser, decl);
parser->ReadToken(TokenType::LBrace);
Token closingToken;
parser->pushScopeAndSetParent(decl);
while (!AdvanceIfMatch(parser, MatchedTokenType::CurlyBraces, &closingToken))
{
EnumCaseDecl* caseDecl = parseEnumCaseDecl(parser);
Expand All @@ -4955,6 +4956,7 @@ namespace Slang

parser->ReadToken(TokenType::Comma);
}
parser->PopScope();
decl->closingSourceLoc = closingToken.loc;
return decl;
});
Expand Down
15 changes: 15 additions & 0 deletions tests/language-feature/enums/reference-previous.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj

// Test that a enum case can reference a previously defined case.

enum Check { V = 1, X = V+2 };

//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)
{
// CHECK: 3
outputBuffer[0] = Check.X;
}

0 comments on commit d63f5e2

Please sign in to comment.