From d1a13a730406646029cedd018bb9806943209baa Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 17 Jan 2025 14:43:31 -0800 Subject: Allow __subscript syntax. (#6124) * Allow __subscript syntax. * Fix. --- source/slang/slang-parser.cpp | 44 +++++++++++++--------- .../generics/generic-subscript.slang | 24 ++++++++++++ 2 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 tests/language-feature/generics/generic-subscript.slang diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index 6ae41a2eb..98cfd121c 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -3932,28 +3932,38 @@ static void parseStorageDeclBody(Parser* parser, ContainerDecl* decl) static NodeBase* parseSubscriptDecl(Parser* parser, void* /*userData*/) { - SubscriptDecl* decl = parser->astBuilder->create(); - parser->FillPosition(decl); - parser->PushScope(decl); + return parseOptGenericDecl( + parser, + [&](GenericDecl* genericParent) + { + SubscriptDecl* decl = parser->astBuilder->create(); + parser->FillPosition(decl); + parser->PushScope(decl); - // TODO: the use of this name here is a bit magical... - decl->nameAndLoc.name = getName(parser, "operator[]"); + // TODO: the use of this name here is a bit magical... + decl->nameAndLoc.name = getName(parser, "operator[]"); - parseParameterList(parser, decl); + parseParameterList(parser, decl); - if (AdvanceIf(parser, TokenType::RightArrow)) - { - decl->returnType = parser->ParseTypeExp(); - } - else - { - decl->returnType.exp = parser->astBuilder->create(); - } + if (AdvanceIf(parser, TokenType::RightArrow)) + { + decl->returnType = parser->ParseTypeExp(); + } + else + { + decl->returnType.exp = parser->astBuilder->create(); + } - parseStorageDeclBody(parser, decl); + auto funcScope = parser->currentScope; + parser->PopScope(); + maybeParseGenericConstraints(parser, genericParent); + parser->PushScope(funcScope); - parser->PopScope(); - return decl; + parseStorageDeclBody(parser, decl); + + parser->PopScope(); + return decl; + }); } /// Peek in the token stream and return `true` if it looks like a modern-style variable declaration diff --git a/tests/language-feature/generics/generic-subscript.slang b/tests/language-feature/generics/generic-subscript.slang new file mode 100644 index 000000000..87eec3045 --- /dev/null +++ b/tests/language-feature/generics/generic-subscript.slang @@ -0,0 +1,24 @@ +//TEST:COMPARE_COMPUTE(filecheck-buffer=CHECK): -output-using-type + +//TEST_INPUT: set output = out ubuffer(data=[0 0 0 0], stride=4) +RWStructuredBuffer output; + +struct Tx +{ + float x; + __subscript(I index) -> float + where I:IInteger + { + get { return x + index.toInt(); } + set { x = newValue;} + } +} + +[numthreads(1,1,1)] +void computeMain() +{ + Tx obj; + obj[0] = 3.0; + // CHECK: 5.0 + output[0] = obj[2]; +} \ No newline at end of file -- cgit v1.2.3