diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-08 13:22:24 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-09 20:06:16 +0200 |
commit | aec162c77194898eb04fff83036e8157db3eb051 (patch) | |
tree | fab69617646a652fc104a952b2c4c110d1e366a1 /sources | |
parent | db615e81615ee993cac69a3477351427d798e31e (diff) |
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 <tismer@stackless.com>
Diffstat (limited to 'sources')
3 files changed, 64 insertions, 51 deletions
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 <class Type> // 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"; diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index 237d9be84..48b27715d 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -1968,9 +1968,8 @@ void ShibokenGenerator::replaceConverterTypeSystemVariable(TypeSystemConverterVa const QString conversionSignature = conversionType.cppSignature(); if (varType != QLatin1String("auto") && varType != conversionSignature) throw Exception(msgConversionTypesDiffer(varType, conversionSignature)); - c << getFullTypeName(conversionType) << ' ' << varName; - writeMinimalConstructorExpression(c, api(), conversionType); - c << ";\n"; + c << getFullTypeName(conversionType) << ' ' << varName + << minimalConstructorExpression(api(), conversionType) << ";\n"; } c << cpythonToCppConversionFunction(conversionType); QString prefix; @@ -2662,49 +2661,36 @@ bool ShibokenGenerator::pythonFunctionWrapperUsesListOfArguments(const AbstractM return od.pythonFunctionWrapperUsesListOfArguments(); } -void ShibokenGenerator::writeMinimalConstructorExpression(TextStream &s, - const ApiExtractorResult &api, - const AbstractMetaType &type, - const QString &defaultCtor) +QString ShibokenGenerator::minimalConstructorExpression(const ApiExtractorResult &api, + const AbstractMetaType &type) { - if (!defaultCtor.isEmpty()) { - s << " = " << defaultCtor; - return; - } if (type.isExtendedCppPrimitive() || type.isSmartPointer()) - return; + return {}; QString errorMessage; const auto ctor = minimalConstructor(api, type, &errorMessage); - if (ctor.has_value()) { - s << ctor->initialization(); - } else { - const QString message = - msgCouldNotFindMinimalConstructor(QLatin1String(__FUNCTION__), - type.cppSignature(), errorMessage); - qCWarning(lcShiboken()).noquote() << message; - s << ";\n#error " << message << '\n'; - } + if (ctor.has_value()) + return ctor->initialization(); + + const QString message = + msgCouldNotFindMinimalConstructor(QLatin1String(__FUNCTION__), + type.cppSignature(), errorMessage); + qCWarning(lcShiboken()).noquote() << message; + return u";\n#error "_qs + message + u'\n'; } -void ShibokenGenerator::writeMinimalConstructorExpression(TextStream &s, - const ApiExtractorResult &api, - const TypeEntry *type, - const QString &defaultCtor) +QString ShibokenGenerator::minimalConstructorExpression(const ApiExtractorResult &api, + const TypeEntry *type) { - if (!defaultCtor.isEmpty()) { - s << " = " << defaultCtor; - return; - } if (type->isExtendedCppPrimitive()) - return; + return {}; const auto ctor = minimalConstructor(api, type); - if (ctor.has_value()) { - s << ctor->initialization(); - } else { - const QString message = msgCouldNotFindMinimalConstructor(QLatin1String(__FUNCTION__), type->qualifiedCppName()); - qCWarning(lcShiboken()).noquote() << message; - s << ";\n#error " << message << '\n'; - } + if (ctor.has_value()) + return ctor->initialization(); + + const QString message = msgCouldNotFindMinimalConstructor(QLatin1String(__FUNCTION__), + type->qualifiedCppName()); + qCWarning(lcShiboken()).noquote() << message; + return u";\n#error "_qs + message + u'\n'; } QString ShibokenGenerator::pythonArgsAt(int i) diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h index 104bd5ab9..504420fdf 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.h +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h @@ -93,6 +93,11 @@ public: /// Returns true if the user enabled PySide extensions. bool usePySideExtensions() const; + static QString minimalConstructorExpression(const ApiExtractorResult &api, + const AbstractMetaType &type); + static QString minimalConstructorExpression(const ApiExtractorResult &api, + const TypeEntry *type); + protected: bool doSetup() override; @@ -362,13 +367,6 @@ protected: static AbstractMetaType buildAbstractMetaTypeFromAbstractMetaClass(const AbstractMetaClass *metaClass); - static void writeMinimalConstructorExpression(TextStream &s, const ApiExtractorResult &api, - const AbstractMetaType &type, - const QString &defaultCtor = QString()); - static void writeMinimalConstructorExpression(TextStream &s, const ApiExtractorResult &api, - const TypeEntry *type, - const QString &defaultCtor = QString()); - void collectContainerTypesFromConverterMacros(const QString &code, bool toPythonMacro); static void writeFunctionCall(TextStream &s, |