KhronosGroup / KhronosGroup/glslang
HLSL: access struct base fields (struct inheritance)
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 3.6k
- Forks
- 990
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 31
Description
It looks like glslang already supports structure inheritance (e.g. `struct B : A { /* ... */ };`).
However, accessing the fields of a base structure seems to be incomplete.
For example, the following HLSL code does not compile with glslang:
```hlsl
struct A {
float x;
};
struct B : A {
float y;
};
float main() : FOOBAR {
B b;
b.x = 1; // ERROR (but actually valid)
b.y = 2;
return b.x + b.y;
}
```
This produces the following error:
```
ERROR: .\MemberFunctions.hlsl:9: 'x' : no such field in structure
ERROR: .\MemberFunctions.hlsl:9: 'assignment expression' : Expected
.\MemberFunctions.hlsl(9): error at column 6, HLSL parsing failed.
ERROR: 3 compilation errors. No code generated.
```
In [my own compiler project](https://github.com/LukasBanana/XShaderCompiler), I originally inserted all members of the struct hierarchy into the respective struct (i.e. `struct B { float x; float y; };` in this example).
But this does not work, because HLSL allows shadowing fields, like this:
```hlsl
struct A {
float x;
};
struct B : A {
float x; // Shadows the declaration of A::x
};
float main() : FOOBAR {
B b;
b.x = 1;
return b.x;
}
```
So I decided to insert a `base` field to the respective structs, similar to the `_this` parameter in glslang for member functions.
I don't know if and how this is already implemented in glslang, but my compiler produces this output for the example above:
```glsl
#version 130
out float xsv_FOOBAR0;
struct A {
float x;
};
struct B {
A base;
float y;
};
void main() {
B b;
b.base.x = 1.0f;
b.y = 2.0f;
xsv_FOOBAR0 = b.base.x + b.y;
}
```
Greetings,
Lukas
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the HLSL examples with glslang and trace how structure inheritance and member lookup are handled. The work is done when base fields can be accessed through derived structures while a derived field with the same name still shadows the base field, and both examples compile successfully.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100