From 89faa8a7d9b441b5dd92eec5fcf362eb3f38fa2b Mon Sep 17 00:00:00 2001 From: "T. Foley" Date: Thu, 27 May 2021 15:05:34 -0700 Subject: 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. --- source/slang/slang-lower-to-ir.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'source/slang/slang-lower-to-ir.cpp') 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 if (auto aggTypeDeclRef = declRef.as()) { List args; + + if (auto structTypeDeclRef = aggTypeDeclRef.as()) + { + if (auto baseStructType = findBaseStructType(getASTBuilder(), structTypeDeclRef)) + { + auto irBaseVal = getSimpleVal(context, getDefaultVal(baseStructType)); + args.add(irBaseVal); + } + } + for (auto ff : getMembersOfType(aggTypeDeclRef, MemberFilterStyle::Instance)) { auto irFieldVal = getSimpleVal(context, getDefaultVal(ff)); @@ -3082,6 +3092,30 @@ struct ExprLoweringVisitorBase : ExprVisitor if (auto aggTypeDeclRef = declRef.as()) { 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()) + { + 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(aggTypeDeclRef, MemberFilterStyle::Instance)) { UInt argIndex = argCounter++; -- cgit v1.2.3