From 42f1cff5c1471e6bc3988a9810c20b8bcc1c84dd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 30 Oct 2017 08:54:09 -0700 Subject: Support explicit `this` expressions This is the first step towards supporting traditional object-oriented method definitions; the second step will be to allow `this` expressions to be implicit. - Add a test case using explicit `this`, and expected output - Update parsing logic for expressions so that it handled identifiers similarly to the declaration and statement logic: first try to parse using a syntax declaration looked up in the curent scope, and otherwise fall back to the ordinary `VarExpr` case. * As long as I'm making that change: switch `true` and `false` to be parsed via the callback mechanism rather than be special-cased. * This change will also help out if we ever wanted to add `super`/`base` expressions, `new`, `sizeof`/`alignof` or any other expression keywords. - Add a `ThisExpr` node and register a parser callback for it. - Add semantic checks for `ThisExpr`: basically just look upwards through scopes until we find either an aggregate type declaration or an `extension` declaration, and then use that as the type of the expression. - TODO: eventually we need to guard against a `this` expression inside of a `static` member. - The IR generation logic already handled creation of `this` parameters in function signatures; the missing piece was to register the appropriate parameter in the context, so that we can use it as the lowering of a `this` expression. --- tests/compute/explicit-this-expr.slang | 34 ++++++++++++++++++++++ .../compute/explicit-this-expr.slang.expected.txt | 4 +++ 2 files changed, 38 insertions(+) create mode 100644 tests/compute/explicit-this-expr.slang create mode 100644 tests/compute/explicit-this-expr.slang.expected.txt (limited to 'tests') diff --git a/tests/compute/explicit-this-expr.slang b/tests/compute/explicit-this-expr.slang new file mode 100644 index 000000000..7bd8dff99 --- /dev/null +++ b/tests/compute/explicit-this-expr.slang @@ -0,0 +1,34 @@ +//TEST(smoke,compute):COMPARE_COMPUTE:-xslang -use-ir +//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out + +// Access fields of a `struct` type from within a "method" by +// using an explicit `this` expression. + +struct A +{ + float x; + + float addWith(float y) + { + return this.x + y; + } +}; + +RWStructuredBuffer outputBuffer : register(u0); + + +float test(float inVal) +{ + A a; + a.x = inVal; + return a.addWith(inVal*inVal); +} + +[numthreads(4, 1, 1)] +void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) +{ + uint tid = dispatchThreadID.x; + float inVal = float(tid); + float outVal = test(inVal); + outputBuffer[tid] = outVal; +} \ No newline at end of file diff --git a/tests/compute/explicit-this-expr.slang.expected.txt b/tests/compute/explicit-this-expr.slang.expected.txt new file mode 100644 index 000000000..f73cfe6c3 --- /dev/null +++ b/tests/compute/explicit-this-expr.slang.expected.txt @@ -0,0 +1,4 @@ +0 +40000000 +40C00000 +41400000 -- cgit v1.2.3