From aec162c77194898eb04fff83036e8157db3eb051 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 8 Sep 2021 13:22:24 +0200 Subject: shiboken6: Refactor writeMinimalConstructorExpression() Change ShibokenGenerator::minimalConstructorExpression() to return a string and remove the defaultCtor parameter, which is only relevant for the argument conversion case where the argument has a default value. Put the default value code into a separate helper in CppGenerator and make it a bit smarter, trying to avoid assignments for class types. Task-number: PYSIDE-1605 Change-Id: I22594cedcf8710fc85ae255109ab4ead1effcfa1 Reviewed-by: Christian Tismer --- .../shiboken6/generator/shiboken/cppgenerator.cpp | 43 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'sources/shiboken6/generator/shiboken/cppgenerator.cpp') diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index d2237b973..aac949459 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -1602,9 +1602,8 @@ return result;)"; || sourceType.typeEntry()->isEnum() || sourceType.typeEntry()->isFlags()) { StringStream pc(TextStream::Language::Cpp); - pc << getFullTypeNameWithoutModifiers(sourceType) << " cppIn"; - writeMinimalConstructorExpression(pc, api(), sourceType); - pc << ";\n"; + pc << getFullTypeNameWithoutModifiers(sourceType) << " cppIn" + << minimalConstructorExpression(api(), sourceType) << ";\n"; writeToCppConversion(pc, sourceType, nullptr, QLatin1String("pyIn"), QLatin1String("cppIn")); pc << ';'; toCppPreConv = pc.toString(); @@ -2566,6 +2565,34 @@ static inline QString arrayHandleType(const AbstractMetaTypeList &nestedArrayTyp return QString(); } +// Helper to write argument initialization code for a function argument +// in case it has a default value. +template // AbstractMetaType/TypeEntry +static void writeMinimalConstructorExpression(TextStream &s, + const ApiExtractorResult &api, + Type type, + bool isPrimitive, + const QString &defaultValue) +{ + if (defaultValue.isEmpty()) { + s << ShibokenGenerator::minimalConstructorExpression(api, type); + return; + } + // Use assignment to avoid "Most vexing parse" if it looks like + // a function call, or for primitives/pointers + const bool isDefault = defaultValue == u"{}"; + if ((isPrimitive && !isDefault) + || defaultValue == u"nullptr" || defaultValue.contains(u'(')) { + s << " = " << defaultValue; + return; + } + if (isDefault) { + s << defaultValue; + return; + } + s << '(' << defaultValue << ')'; +} + void CppGenerator::writePythonToCppTypeConversion(TextStream &s, const AbstractMetaType &type, const QString &pyIn, @@ -2579,6 +2606,7 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, QString cppOutAux = cppOut + QLatin1String("_local"); + const bool isPrimitive = typeEntry->isPrimitive(); const bool isEnum = typeEntry->isEnum(); const bool isFlags = typeEntry->isFlags(); bool treatAsPointer = valueTypeWithCopyConstructorOnlyPassed(api(), type); @@ -2604,7 +2632,7 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, if (mayHaveImplicitConversion) { s << typeName << ' ' << cppOutAux; - writeMinimalConstructorExpression(s, api(), type, defaultValue); + writeMinimalConstructorExpression(s, api(), type, isPrimitive, defaultValue); s << ";\n"; } else if (avoidProtectedHack() && isEnum) { auto metaEnum = api().findAbstractMetaEnum(type.typeEntry()); @@ -2631,7 +2659,8 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, if (needsConstCast) s << ')'; } - } else if (type.referenceType() == LValueReference && !typeEntry->isPrimitive() && isNotContainerEnumOrFlags) { + } else if (type.referenceType() == LValueReference + && !isPrimitive && isNotContainerEnumOrFlags) { s << " *" << cppOut << " = &" << cppOutAux; } else { s << ' ' << cppOut; @@ -2642,9 +2671,9 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, else s << defaultValue; } else if (type.isUserPrimitive() || isEnum || isFlags) { - writeMinimalConstructorExpression(s, api(), typeEntry, defaultValue); + writeMinimalConstructorExpression(s, api(), typeEntry, isPrimitive, defaultValue); } else if (!type.isContainer() && !type.isSmartPointer()) { - writeMinimalConstructorExpression(s, api(), type, defaultValue); + writeMinimalConstructorExpression(s, api(), type, isPrimitive, defaultValue); } } s << ";\n"; -- cgit v1.2.3