From 6f681279d99e72e717bb2b91763b80e570ae725b Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 20 Dec 2017 17:35:10 -0800 Subject: IR: fixes for subscript accessors (#322) * IR: fixes for subscript accessors Fixes #320 This is a bunch of fixes for handling of `__subscript` operations on builtin types (notably `RWStructuredBuffer` and `StructuredBuffer` at this point). - Automatically add a `GetterDecl` to any subscript decalratio was declithout any accessors. This avoids hitting a null- dereference in the emit logic. - Add a notion of a `RefAccessor` (declared with `ref`) as a peer to getters and setters. The idea is that a `ref` accessor returns a pointer to the element data, so that it can be used for both getting and setting values. This is closer to the behavior of `RWStructuredBuffer` element access in HLSL. - Fixes for dealing with "access chains" where there might be a combination of a subscript (where the is a `get` and `set` but no `ref`) and member access, so that we have to read the base value into a temp, modify it, and then write it back. - This logic is still a bit of a mess, so we will eventually want to take a more consistent pass over this to deal with how we "materialize" values for setters. - Update `RWStructuredBuffer` to have a `ref` accessor, and then fix up the IR tests to handle the new opcode that I added for it. - Note: I didn't handle this as an intrinsic simply because the `tests/ir/*` tests aren't really set up to handle builtins with ugly mangled names. * Fixup: type error in VM for buffer element ref I was using the result type of the op as the element type for computing the element address, but the result type is a pointer to the real element type. This caused test failures on 64-bit platforms, where the stride of the buffer in the `ir/factorial` test needs to be 4. The fix is to assume the result type is a pointer, and extract the pointed-to type out of that. --- source/slang/check.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 4840ae30d..b981fb778 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -3575,6 +3575,32 @@ namespace Slang decl->SetCheckState(DeclCheckState::CheckedHeader); + // If we have a subscript declaration with no accessor declarations, + // then we should create a single `GetterDecl` to represent + // the implicit meaning of their declaration, so: + // + // subscript(uint index) -> T; + // + // becomes: + // + // subscript(uint index) -> T { get; } + // + + bool anyAccessors = false; + for(auto accessorDecl : decl->getMembersOfType()) + { + anyAccessors = true; + } + + if(!anyAccessors) + { + RefPtr getterDecl = new GetterDecl(); + getterDecl->loc = decl->loc; + + getterDecl->ParentDecl = decl; + decl->Members.Add(getterDecl); + } + for(auto mm : decl->Members) { checkDecl(mm); @@ -4662,6 +4688,10 @@ namespace Slang { callExpr->type.IsLeftValue = true; } + for(auto refAccessor : subscriptDeclRef.getDecl()->getMembersOfType()) + { + callExpr->type.IsLeftValue = true; + } } // TODO: there may be other cases that confer l-value-ness -- cgit v1.2.3