diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-09-11 08:53:32 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-09-12 06:47:40 +0000 |
commit | 8a62536b1071ac86c86c82089a53b7a4cd172f90 (patch) | |
tree | df595b8c7b750ae7d50b09adb66607da7a4b4709 /sources/shiboken2/generator/shiboken2 | |
parent | dbb78237ef6622a2daae32fa3f5fa0cd9b1ffda5 (diff) |
shiboken: Introduce DefaultValue
Introduce a small class storing the return value of
Generator::minimalConstructor() consisting of a type enumeration and a
value, offering formatting for the use cases variable initializations,
return values and constructor arguments lists.
Having distinct formatting for the different use cases has some
advantages:
- Can use nullptr without casts (except in constructor arguments
lists, where the type is needed for disambiguation).
- In the previous implementation using a string, "" indicated an
error; so, it was not possible to use it for default-constructors.
It is now possible to handle default-constructors for
initialization ("Foo f" instead of "Foo f = Foo()".
- Can use {} for return values.
Task-number: PYSIDE-62
Change-Id: I73229cb957d4b92b43de4cdbc3c66703f48faa61
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2/generator/shiboken2')
-rw-r--r-- | sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 28 | ||||
-rw-r--r-- | sources/shiboken2/generator/shiboken2/shibokengenerator.cpp | 29 |
2 files changed, 34 insertions, 23 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index b992a5ca8..4bb1e02b6 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -733,7 +733,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun Indentation indentation(INDENT); - QString defaultReturnExpr; + DefaultValue defaultReturnExpr; if (retType) { const FunctionModificationList &mods = func->modifications(); for (const FunctionModification &mod : mods) { @@ -741,9 +741,9 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun if (argMod.index == 0 && !argMod.replacedDefaultExpression.isEmpty()) { static const QRegularExpression regex(QStringLiteral("%(\\d+)")); Q_ASSERT(regex.isValid()); - defaultReturnExpr = argMod.replacedDefaultExpression; + QString expr = argMod.replacedDefaultExpression; for (int offset = 0; ; ) { - const QRegularExpressionMatch match = regex.match(defaultReturnExpr, offset); + const QRegularExpressionMatch match = regex.match(expr, offset); if (!match.hasMatch()) break; const int argId = match.capturedRef(1).toInt() - 1; @@ -751,15 +751,17 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun qCWarning(lcShiboken) << "The expression used in return value contains an invalid index."; break; } - defaultReturnExpr.replace(match.captured(0), func->arguments().at(argId)->name()); + expr.replace(match.captured(0), func->arguments().at(argId)->name()); offset = match.capturedStart(1); } + defaultReturnExpr.setType(DefaultValue::Custom); + defaultReturnExpr.setValue(expr); } } } - if (defaultReturnExpr.isEmpty()) + if (!defaultReturnExpr.isValid()) defaultReturnExpr = minimalConstructor(func->type()); - if (defaultReturnExpr.isEmpty()) { + if (!defaultReturnExpr.isValid()) { QString errorMsg = QLatin1String(__FUNCTION__) + QLatin1String(": "); if (const AbstractMetaClass *c = func->implementingClass()) errorMsg += c->qualifiedCppName() + QLatin1String("::"); @@ -768,6 +770,8 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun qCWarning(lcShiboken).noquote().nospace() << errorMsg; s << endl << INDENT << "#error " << errorMsg << endl; } + } else { + defaultReturnExpr.setType(DefaultValue::Void); } if (func->isAbstract() && func->isModifiedRemoved()) { @@ -775,7 +779,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun << QString::fromLatin1("Pure virtual method '%1::%2' must be implement but was "\ "completely removed on type system.") .arg(func->ownerClass()->name(), func->minimalSignature()); - s << INDENT << returnStatement(defaultReturnExpr) << endl; + s << INDENT << returnStatement(defaultReturnExpr.returnValue()) << endl; s << '}' << endl << endl; return; } @@ -794,7 +798,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun s << INDENT << "if (PyErr_Occurred())" << endl; { Indentation indentation(INDENT); - s << INDENT << returnStatement(defaultReturnExpr) << endl; + s << INDENT << returnStatement(defaultReturnExpr.returnValue()) << endl; } s << INDENT << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR << "(Shiboken::BindingManager::instance().getOverride(this, \""; @@ -817,7 +821,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun s << "()' not implemented.\");" << endl; s << INDENT << "return"; if (retType) - s << ' ' << defaultReturnExpr; + s << ' ' << defaultReturnExpr.returnValue(); } else { s << INDENT << "gil.release();" << endl; s << INDENT; @@ -922,7 +926,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun { Indentation indent(INDENT); s << INDENT << "PyErr_Print();" << endl; - s << INDENT << returnStatement(defaultReturnExpr) << endl; + s << INDENT << returnStatement(defaultReturnExpr.returnValue()) << endl; } s << INDENT << '}' << endl; @@ -944,7 +948,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun "\"Invalid return value in function %s, expected %s, got %s.\", \""; s << func->ownerClass()->name() << '.' << funcName << "\", " << getVirtualFunctionReturnTypeName(func); s << ", Py_TYPE(" << PYTHON_RETURN_VAR << ")->tp_name);" << endl; - s << INDENT << returnStatement(defaultReturnExpr) << endl; + s << INDENT << returnStatement(defaultReturnExpr.returnValue()) << endl; } s << INDENT << '}' << endl; @@ -965,7 +969,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream&s, const AbstractMetaFun "\"Invalid return value in function %s, expected %s, got %s.\", \""; s << func->ownerClass()->name() << '.' << funcName << "\", " << getVirtualFunctionReturnTypeName(func); s << ", Py_TYPE(" << PYTHON_RETURN_VAR << ")->tp_name);" << endl; - s << INDENT << returnStatement(defaultReturnExpr) << endl; + s << INDENT << returnStatement(defaultReturnExpr.returnValue()) << endl; } s << INDENT << '}' << endl; diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp index 6a7cef6ef..6d263dd01 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp @@ -2653,30 +2653,37 @@ QString ShibokenGenerator::getDefaultValue(const AbstractMetaFunction* func, co void ShibokenGenerator::writeMinimalConstructorExpression(QTextStream& s, const AbstractMetaType* type, const QString& defaultCtor) { - if (defaultCtor.isEmpty() && isCppPrimitive(type)) + if (!defaultCtor.isEmpty()) { + s << " = " << defaultCtor; + return; + } + if (isCppPrimitive(type)) return; - QString ctor = defaultCtor.isEmpty() ? minimalConstructor(type) : defaultCtor; - if (ctor.isEmpty()) { + const auto ctor = minimalConstructor(type); + if (ctor.isValid()) { + s << ctor.initialization(); + } else { const QString message = msgCouldNotFindMinimalConstructor(QLatin1String(__FUNCTION__), type->cppSignature()); qCWarning(lcShiboken()).noquote() << message; s << ";\n#error " << message << '\n'; - } else { - s << " = " << ctor; } } void ShibokenGenerator::writeMinimalConstructorExpression(QTextStream& s, const TypeEntry* type, const QString& defaultCtor) { - if (defaultCtor.isEmpty() && isCppPrimitive(type)) + if (!defaultCtor.isEmpty()) { + s << " = " << defaultCtor; + return; + } + if (isCppPrimitive(type)) return; - QString ctor = defaultCtor.isEmpty() ? minimalConstructor(type) : defaultCtor; - - if (ctor.isEmpty()) { + const auto ctor = minimalConstructor(type); + if (ctor.isValid()) { + s << ctor.initialization(); + } else { const QString message = msgCouldNotFindMinimalConstructor(QLatin1String(__FUNCTION__), type->qualifiedCppName()); qCWarning(lcShiboken()).noquote() << message; s << ";\n#error " << message << endl; - } else { - s << " = " << ctor; } } |