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/shiboken6/generator/shiboken/cppgenerator.cpp | |
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/shiboken6/generator/shiboken/cppgenerator.cpp')
-rw-r--r-- | sources/shiboken6/generator/shiboken/cppgenerator.cpp | 43 |
1 files changed, 36 insertions, 7 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"; |