https://github.com/shader-slang/slang
Revision 89faa8a7d9b441b5dd92eec5fcf362eb3f38fa2b authored by T. Foley on 27 May 2021, 22:05:34 UTC, committed by GitHub on 27 May 2021, 22:05:34 UTC
If the user has a derived `struct` type:

```hlsl
struct Base { int b = 1; }
struct Derived : Base { int d = 2; }
```

Then it is still reasonable for them to want to use initializer lists when declaring variables using the `Derived` type:

```hlsl
Derived x = {};
Derived y = { 7, 8 };
```

This change implements two missing pieces of functionality in the Slang compiler to allow this case:

* First, when the front-end semantic checks are applied to an initializer list, if the type being initialized is a derived `struct` type it always expects to find initialization arguments for its base type before those for its fields.

* Second, when lowering an initializer-list expression from the AST to the IR, the compiler expects the first argument in the list to be the initial value for the base field (if any). This also applies to default-initialization of fields/variables.

This change slightly entangles front-end logic with the logic for how struct inheritance is lowered to the IR, but the behavior is unlikely to confuse users who expect C++-like layout.

It is worth noting that with this change it should be possible to initialize the base type using either a nested initializer list or flat arguments:

```hlsl
struct BigBase { int x; int y; int z; }
struct BigDerived : BigBase { int w; }

BigDerived a = { {1,2,3}, 4 };
BigDerived b = { 1, 2, 3, 4 };
```

This behavior should Just Work because of the existing C-like rules for initializer lists where an aggregate can be initialized by either a `{}`-enclosed block or distinct values for its leaf fields.
1 parent 63dcc7a
History
Tip revision: 89faa8a7d9b441b5dd92eec5fcf362eb3f38fa2b authored by T. Foley on 27 May 2021, 22:05:34 UTC
Fix initializer lists for derived structs (#1862)
Tip revision: 89faa8a
File Mode Size
.github
build
docs
examples
external
extras
prelude
source
tests
tools
.editorconfig -rw-r--r-- 937 bytes
.gitattributes -rw-r--r-- 95 bytes
.gitignore -rw-r--r-- 1.1 KB
.gitmodules -rw-r--r-- 951 bytes
CODE_OF_CONDUCT.md -rw-r--r-- 3.1 KB
LICENSE -rw-r--r-- 1.1 KB
README.md -rw-r--r-- 6.1 KB
github_build.sh -rw-r--r-- 495 bytes
github_test.sh -rw-r--r-- 546 bytes
premake.bat -rw-r--r-- 120 bytes
premake5.lua -rw-r--r-- 49.0 KB
slang-com-helper.h -rw-r--r-- 4.9 KB
slang-com-ptr.h -rw-r--r-- 4.9 KB
slang-gfx.h -rw-r--r-- 44.6 KB
slang-tag-version.h -rw-r--r-- 36 bytes
slang.h -rw-r--r-- 173.9 KB
slang.sln -rw-r--r-- 21.2 KB
test.bat -rw-r--r-- 1.4 KB

README.md

back to top