summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-check-conversion.cpp
diff options
context:
space:
mode:
authorT. Foley <tfoleyNV@users.noreply.github.com>2021-05-27 15:05:34 -0700
committerGitHub <noreply@github.com>2021-05-27 15:05:34 -0700
commit89faa8a7d9b441b5dd92eec5fcf362eb3f38fa2b (patch)
treec47bfa07f908143493f08f1080c170d0506dc3e9 /source/slang/slang-check-conversion.cpp
parent63dcc7acf4784d95407866730a70f5d37d391b88 (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-check-conversion.cpp')
-rw-r--r--source/slang/slang-check-conversion.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/source/slang/slang-check-conversion.cpp b/source/slang/slang-check-conversion.cpp
index 8b60b2725..b6c7069a2 100644
--- a/source/slang/slang-check-conversion.cpp
+++ b/source/slang/slang-check-conversion.cpp
@@ -151,6 +151,44 @@ namespace Slang
ioInitArgIndex);
}
+ DeclRefType* findBaseStructType(ASTBuilder* astBuilder, DeclRef<StructDecl> const& structTypeDeclRef)
+ {
+ auto inheritanceDecl = getMembersOfType<InheritanceDecl>(structTypeDeclRef).getFirstOrNull();
+ if(!inheritanceDecl)
+ return nullptr;
+
+ auto baseType = getBaseType(astBuilder, inheritanceDecl);
+ auto baseDeclRefType = as<DeclRefType>(baseType);
+ if(!baseDeclRefType)
+ return nullptr;
+
+ auto baseDeclRef = baseDeclRefType->declRef;
+ auto baseStructDeclRef = baseDeclRef.as<StructDecl>();
+ if(!baseStructDeclRef)
+ return nullptr;
+
+ return baseDeclRefType;
+ }
+
+ DeclRef<StructDecl> findBaseStructDeclRef(ASTBuilder* astBuilder, DeclRef<StructDecl> const& structTypeDeclRef)
+ {
+ auto inheritanceDecl = getMembersOfType<InheritanceDecl>(structTypeDeclRef).getFirstOrNull();
+ if (!inheritanceDecl)
+ return DeclRef<StructDecl>();
+
+ auto baseType = getBaseType(astBuilder, inheritanceDecl);
+ auto baseDeclRefType = as<DeclRefType>(baseType);
+ if (!baseDeclRefType)
+ return DeclRef<StructDecl>();
+
+ auto baseDeclRef = baseDeclRefType->declRef;
+ auto baseStructDeclRef = baseDeclRef.as<StructDecl>();
+ if (!baseStructDeclRef)
+ return DeclRef<StructDecl>();
+
+ return baseStructDeclRef;
+ }
+
bool SemanticsVisitor::_readAggregateValueFromInitializerList(
Type* inToType,
Expr** outToExpr,
@@ -375,6 +413,30 @@ namespace Slang
if(auto toStructDeclRef = toTypeDeclRef.as<StructDecl>())
{
// Trying to initialize a `struct` type given an initializer list.
+ //
+ // Before we iterate over the fields, we want to check if this struct
+ // inherits from another `struct` type. If so, we want to read
+ // an initializer for that base type first.
+ //
+ if (auto baseStructType = findBaseStructType(m_astBuilder, toStructDeclRef))
+ {
+ Expr* coercedArg = nullptr;
+ bool argResult = _readValueFromInitializerList(
+ baseStructType,
+ outToExpr ? &coercedArg : nullptr,
+ fromInitializerListExpr,
+ ioArgIndex);
+
+ // No point in trying further if any argument fails
+ if (!argResult)
+ return false;
+
+ if (coercedArg)
+ {
+ coercedArgs.add(coercedArg);
+ }
+ }
+
// We will go through the fields in order and try to match them
// up with initializer arguments.
//