From 3072cfea95aad2a9ddab0f517c8f18f634442a27 Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Thu, 22 May 2025 21:29:06 -0500 Subject: Implement default initializer list for C-Style type member (#7079) * Implement default initializer list for C-Style type member Close #6189. Previsouly, for the C-Style member in a struct, if it doesn't have any initialize expression, when we synthesize the ctor, we will not associate the default value for the parameter corresponding to that member. This bring some trouble that existing slang users has to add '= {}' to every struct fields in order to make all the parameters in the synthesized ctor having a default value, so people can still use `Struct a = {}` to create a struct. To make this use case convenience, we will automatically associated a '= {}' as the default value for this case. This PR also add support for empty initializing link-time sized vector/matrix by "= {}". In addition, this PR also fix a bug in auto diff where we should not report error when proccessing transpose on an empty struct. --- source/slang/slang-check-decl.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-check-decl.cpp') diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index c03d8e985..1331839a4 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -2512,6 +2512,23 @@ static Expr* constructDefaultConstructorForType(SemanticsVisitor* visitor, Type* return invoke; } + // At the last, we will check if the type is a C-style type, if it is, we will use empty + // initializer list to construct the default constructor. + HashSet visitSet; + if (visitor->isCStyleType(type, visitSet)) + { + auto initListExpr = visitor->getASTBuilder()->create(); + initListExpr->type = visitor->getASTBuilder()->getInitializerListType(); + Expr* outExpr = nullptr; + auto fromType = type; + if (auto atomicType = as(fromType)) + { + fromType = atomicType->getElementType(); + } + if (visitor->_coerceInitializerList(fromType, &outExpr, initListExpr)) + return outExpr; + } + return nullptr; } @@ -13031,7 +13048,13 @@ static Expr* _getParamDefaultValue(SemanticsVisitor* visitor, VarDeclBase* varDe if (!isDefaultInitializable(varDecl)) return nullptr; - return constructDefaultConstructorForType(visitor, varDecl->type.type); + if (auto expr = constructDefaultConstructorForType(visitor, varDecl->type.type)) + { + expr->loc = varDecl->loc; + return expr; + } + + return nullptr; } bool SemanticsDeclAttributesVisitor::_synthesizeCtorSignature(StructDecl* structDecl) -- cgit v1.2.3