From a4a23da2df6af79f03f0d10bd10a18b3cb437396 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 10 Sep 2021 14:08:37 +0200 Subject: shiboken6: Streamline code for argument conversion generation - Use AbstractMetaType::shouldDereferencePointer() instead of spelling out the condition. - Rearange CppGenerator::writePythonToCppTypeConversion() for clarity. Task-number: PYSIDE-1605 Change-Id: I2f93e3344ee7788811e58ff82b00af24f0e04f7c Reviewed-by: Christian Tismer Reviewed-by: Qt CI Bot --- .../shiboken6/generator/shiboken/cppgenerator.cpp | 34 ++++++++++++---------- 1 file changed, 18 insertions(+), 16 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 e85a3ceef..a0fe11cc7 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -1266,7 +1266,7 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s, s << '(' << typeCast << ')'; } } - if (func->type().referenceType() == LValueReference && !func->type().isPointer()) + if (func->type().shouldDereferencePointer()) s << " *"; s << CPP_RETURN_VAR << ";\n"; } @@ -2616,12 +2616,17 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, && !isEnum && !isFlags; const bool isNotContainerEnumOrFlags = !typeEntry->isContainer() && !isEnum && !isFlags; - bool mayHaveImplicitConversion = type.referenceType() == LValueReference + const bool mayHaveImplicitConversion = type.referenceType() == LValueReference && !type.isUserPrimitive() && !type.isExtendedCppPrimitive() && isNotContainerEnumOrFlags && !(treatAsPointer || isPointerOrObjectType); + // For implicit conversions or containers, either value or pointer conversion + // may occur. An implicit conversion uses value conversion whereas the object + // itself uses pointer conversion. + const bool valueOrPointer = mayHaveImplicitConversion; + const AbstractMetaTypeList &nestedArrayTypes = type.nestedArrayTypes(); const bool isCppPrimitiveArray = !nestedArrayTypes.isEmpty() && nestedArrayTypes.constLast().isCppPrimitive(); @@ -2630,12 +2635,7 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, : getFullTypeNameWithoutModifiers(type); bool isProtectedEnum = false; - - if (mayHaveImplicitConversion) { - s << typeName << ' ' << cppOutAux; - writeMinimalConstructorExpression(s, api(), type, isPrimitive, defaultValue); - s << ";\n"; - } else if (avoidProtectedHack() && isEnum) { + if (isEnum && avoidProtectedHack()) { auto metaEnum = api().findAbstractMetaEnum(type.typeEntry()); if (metaEnum.has_value() && metaEnum->isProtected()) { typeName = wrapperName(context) + QLatin1String("::") @@ -2647,6 +2647,12 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, s << typeName; if (isCppPrimitiveArray) { s << ' ' << cppOut; + } else if (valueOrPointer) { + // Generate either value conversion for &cppOutAux or pointer + // conversion for &cppOut + s << ' ' << cppOutAux; + writeMinimalConstructorExpression(s, api(), type, isPrimitive, defaultValue); + s << ";\n" << typeName << " *" << cppOut << " = &" << cppOutAux; } else if (treatAsPointer || isPointerOrObjectType) { s << " *" << cppOut; if (!defaultValue.isEmpty()) { @@ -2660,9 +2666,6 @@ void CppGenerator::writePythonToCppTypeConversion(TextStream &s, if (needsConstCast) s << ')'; } - } else if (type.referenceType() == LValueReference - && !isPrimitive && isNotContainerEnumOrFlags) { - s << " *" << cppOut << " = &" << cppOutAux; } else { s << ' ' << cppOut; if (isProtectedEnum && avoidProtectedHack()) { @@ -3590,14 +3593,13 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr else if (!arg.defaultValueExpression().isEmpty()) userArgs.append(QLatin1String(CPP_ARG_REMOVED) + QString::number(i)); } else { - int idx = arg.argumentIndex() - removedArgs; - bool deRef = valueTypeWithCopyConstructorOnlyPassed(api(), arg.type()) - || arg.type().isObjectTypeUsedAsValueType() - || (arg.type().referenceType() == LValueReference - && arg.type().isWrapperType() && !arg.type().isPointer()); if (hasConversionRule) { userArgs.append(arg.name() + QLatin1String(CONV_RULE_OUT_VAR_SUFFIX)); } else { + const int idx = arg.argumentIndex() - removedArgs; + const bool deRef = valueTypeWithCopyConstructorOnlyPassed(api(), arg.type()) + || arg.type().isObjectTypeUsedAsValueType() + || arg.type().shouldDereferencePointer(); QString argName; if (deRef) argName += QLatin1Char('*'); -- cgit v1.2.3