diff options
| author | T. Foley <tfoleyNV@users.noreply.github.com> | 2021-05-27 15:05:34 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-27 15:05:34 -0700 |
| commit | 89faa8a7d9b441b5dd92eec5fcf362eb3f38fa2b (patch) | |
| tree | c47bfa07f908143493f08f1080c170d0506dc3e9 /source/slang/slang-lower-to-ir.cpp | |
| parent | 63dcc7acf4784d95407866730a70f5d37d391b88 (diff) | |
Fix initializer lists for derived structs (#1862)
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.
Diffstat (limited to 'source/slang/slang-lower-to-ir.cpp')
| -rw-r--r-- | source/slang/slang-lower-to-ir.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index ab488a41f..d2d15735c 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -2961,6 +2961,16 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo> if (auto aggTypeDeclRef = declRef.as<AggTypeDecl>()) { List<IRInst*> args; + + if (auto structTypeDeclRef = aggTypeDeclRef.as<StructDecl>()) + { + if (auto baseStructType = findBaseStructType(getASTBuilder(), structTypeDeclRef)) + { + auto irBaseVal = getSimpleVal(context, getDefaultVal(baseStructType)); + args.add(irBaseVal); + } + } + for (auto ff : getMembersOfType<VarDecl>(aggTypeDeclRef, MemberFilterStyle::Instance)) { auto irFieldVal = getSimpleVal(context, getDefaultVal(ff)); @@ -3082,6 +3092,30 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo> if (auto aggTypeDeclRef = declRef.as<AggTypeDecl>()) { UInt argCounter = 0; + + // If the type is a structure type that inherits from another + // structure type, then we need to treat the base type as + // an implicit first field. + // + if(auto structTypeDeclRef = aggTypeDeclRef.as<StructDecl>()) + { + if (auto baseStructType = findBaseStructType(getASTBuilder(), structTypeDeclRef)) + { + UInt argIndex = argCounter++; + if (argIndex < argCount) + { + auto argExpr = expr->args[argIndex]; + LoweredValInfo argVal = lowerRValueExpr(context, argExpr); + args.add(getSimpleVal(context, argVal)); + } + else + { + auto irDefaultValue = getSimpleVal(context, getDefaultVal(baseStructType)); + args.add(irDefaultValue); + } + } + } + for (auto ff : getMembersOfType<VarDecl>(aggTypeDeclRef, MemberFilterStyle::Instance)) { UInt argIndex = argCounter++; |
