aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/generator/shiboken/cppgenerator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/generator/shiboken/cppgenerator.cpp')
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp4426
1 files changed, 2067 insertions, 2359 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 884a531f6..6c9cc5fec 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -2,9 +2,14 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "cppgenerator.h"
+#include "configurablescope.h"
+#include "generatorargument.h"
+#include "generatorstrings.h"
+#include "defaultvalue.h"
+#include "generatorcontext.h"
#include "codesnip.h"
+#include "customconversion.h"
#include "headergenerator.h"
-#include "apiextractor.h"
#include "apiextractorresult.h"
#include "ctypenames.h"
#include <exception.h>
@@ -32,6 +37,7 @@
#include <primitivetypeentry.h>
#include <smartpointertypeentry.h>
#include <typesystemtypeentry.h>
+#include <valuetypeentry.h>
#include <parser/enumvalue.h>
#include "qtcompat.h"
@@ -46,38 +52,54 @@
#include <algorithm>
#include <cstring>
#include <memory>
+#include <set>
using namespace Qt::StringLiterals;
-static const QString CPP_ARG0 = u"cppArg0"_s;
-const char *CppGenerator::PYTHON_TO_CPPCONVERSION_STRUCT = "Shiboken::Conversions::PythonToCppConversion";
+static const char shibokenErrorsOccurred[] = "Shiboken::Errors::occurred() != nullptr";
-static inline QString reprFunction() { return QStringLiteral("__repr__"); }
+static constexpr auto virtualMethodStaticReturnVar = "result"_L1;
-static const char typeNameFunc[] = R"CPP(template <class T>
-static const char *typeNameOf(const T &t)
+static constexpr auto sbkObjectTypeF = "SbkObject_TypeF()"_L1;
+static const char initInheritanceFunction[] = "initInheritance";
+
+static QString mangleName(QString name)
{
- const char *typeName = typeid(t).name();
- auto size = std::strlen(typeName);
-#if defined(Q_CC_MSVC) // MSVC: "class QPaintDevice * __ptr64"
- if (auto lastStar = strchr(typeName, '*')) {
- // MSVC: "class QPaintDevice * __ptr64"
- while (*--lastStar == ' ') {
- }
- size = lastStar - typeName + 1;
- }
-#else // g++, Clang: "QPaintDevice *" -> "P12QPaintDevice"
- if (size > 2 && typeName[0] == 'P' && std::isdigit(typeName[1])) {
- ++typeName;
- --size;
- }
-#endif
- char *result = new char[size + 1];
- result[size] = '\0';
- memcpy(result, typeName, size);
- return result;
+ if (name == u"None" || name == u"False" || name == u"True" || name == u"from")
+ name += u'_';
+ return name;
+}
+
+struct sbkUnusedVariableCast
+{
+ explicit sbkUnusedVariableCast(QAnyStringView name) : m_name(name) {}
+
+ const QAnyStringView m_name;
+};
+
+TextStream &operator<<(TextStream &str, const sbkUnusedVariableCast &c)
+{
+ str << "SBK_UNUSED(" << c.m_name << ")\n";
+ return str;
+}
+
+struct pyTypeGetSlot
+{
+ explicit pyTypeGetSlot(QAnyStringView funcType, QAnyStringView typeObject,
+ QAnyStringView aSlot) :
+ m_funcType(funcType), m_typeObject(typeObject), m_slot(aSlot) {}
+
+ const QAnyStringView m_funcType;
+ const QAnyStringView m_typeObject;
+ const QAnyStringView m_slot;
+};
+
+TextStream &operator<<(TextStream &str, const pyTypeGetSlot &p)
+{
+ str << "reinterpret_cast<" << p.m_funcType << ">(PepType_GetSlot("
+ << p.m_typeObject << ", " << p.m_slot << "));\n";
+ return str;
}
-)CPP";
TextStream &operator<<(TextStream &s, CppGenerator::ErrorReturn r)
{
@@ -99,6 +121,25 @@ TextStream &operator<<(TextStream &s, CppGenerator::ErrorReturn r)
return s;
}
+static constexpr auto converterVar = "converter"_L1;
+
+struct registerConverterName
+{
+ explicit registerConverterName(QAnyStringView typeName,
+ QAnyStringView varName = converterVar) :
+ m_typeName(typeName), m_varName(varName) {}
+
+ QAnyStringView m_typeName;
+ QAnyStringView m_varName;
+};
+
+TextStream &operator<<(TextStream &s, const registerConverterName &r)
+{
+ s << "Shiboken::Conversions::registerConverterName(" << r.m_varName
+ << ", \"" << r.m_typeName << "\");\n";
+ return s;
+}
+
// Protocol function name / function parameters / return type
struct ProtocolEntry
{
@@ -131,7 +172,7 @@ const ProtocolEntries &mappingProtocols()
u"PyObject*"_s},
{u"__msetitem__"_s,
u"PyObject *self, PyObject *_key, PyObject *_value"_s,
- intT()}};
+ intT}};
return result;
}
@@ -148,16 +189,16 @@ const ProtocolEntries &sequenceProtocols()
u"PyObject*"_s},
{u"__setitem__"_s,
u"PyObject *self, Py_ssize_t _i, PyObject *_value"_s,
- intT()},
+ intT},
{u"__getslice__"_s,
u"PyObject *self, Py_ssize_t _i1, Py_ssize_t _i2"_s,
u"PyObject*"_s},
{u"__setslice__"_s,
u"PyObject *self, Py_ssize_t _i1, Py_ssize_t _i2, PyObject *_value"_s,
- intT()},
+ intT},
{u"__contains__"_s,
u"PyObject *self, PyObject *_value"_s,
- intT()},
+ intT},
{u"__concat__"_s,
u"PyObject *self, PyObject *_other"_s,
u"PyObject*"_s}
@@ -168,14 +209,14 @@ const ProtocolEntries &sequenceProtocols()
// Return name of function to create PyObject wrapping a container
static QString opaqueContainerCreationFunc(const AbstractMetaType &type)
{
- const auto *containerTypeEntry =
- static_cast<const ContainerTypeEntry *>(type.typeEntry());
- const auto *instantiationTypeEntry =
+ const auto containerTypeEntry =
+ std::static_pointer_cast<const ContainerTypeEntry>(type.typeEntry());
+ const auto instantiationTypeEntry =
type.instantiations().constFirst().typeEntry();
QString result = u"create"_s;
if (type.isConstant())
result += u"Const"_s;
- result += containerTypeEntry->opaqueContainerName(instantiationTypeEntry->name());
+ result += containerTypeEntry->opaqueContainerName(type.instantiationCppSignatures());
return result;
}
@@ -195,135 +236,16 @@ QString CppGenerator::fileNameForContext(const GeneratorContext &context) const
return fileNameForContextHelper(context, u"_wrapper.cpp"_s);
}
-static bool isInplaceAdd(const AbstractMetaFunctionCPtr &func)
-{
- return func->name() == u"operator+=";
-}
-
-static bool isIncrementOperator(const AbstractMetaFunctionCPtr &func)
-{
- return func->functionType() == AbstractMetaFunction::IncrementOperator;
-}
-
-static bool isDecrementOperator(const AbstractMetaFunctionCPtr &func)
-{
- return func->functionType() == AbstractMetaFunction::DecrementOperator;
-}
-
-// Filter predicate for operator functions
-static bool skipOperatorFunc(const AbstractMetaFunctionCPtr &func)
-{
- if (func->isModifiedRemoved() || func->usesRValueReferences())
- return true;
- const auto &name = func->name();
- return name == u"operator[]" || name == u"operator->" || name == u"operator!";
-}
-
-QList<AbstractMetaFunctionCList>
- CppGenerator::filterGroupedOperatorFunctions(const AbstractMetaClass *metaClass,
- OperatorQueryOptions query)
-{
- // ( func_name, num_args ) => func_list
- QMap<QPair<QString, int>, AbstractMetaFunctionCList> results;
-
- auto funcs = metaClass->operatorOverloads(query);
- auto end = std::remove_if(funcs.begin(), funcs.end(), skipOperatorFunc);
- funcs.erase(end, funcs.end());
-
- // If we have operator+=, we remove the operator++/-- which would
- // otherwise be used for emulating __iadd__, __isub__.
- if (std::any_of(funcs.cbegin(), funcs.cend(), isInplaceAdd)) {
- end = std::remove_if(funcs.begin(), funcs.end(),
- [] (const AbstractMetaFunctionCPtr &func) {
- return func->isIncDecrementOperator();
- });
- funcs.erase(end, funcs.end());
- } else {
- // If both prefix/postfix ++/-- are present, remove one
- if (std::count_if(funcs.begin(), funcs.end(), isIncrementOperator) > 1)
- funcs.erase(std::find_if(funcs.begin(), funcs.end(), isIncrementOperator));
- if (std::count_if(funcs.begin(), funcs.end(), isDecrementOperator) > 1)
- funcs.erase(std::find_if(funcs.begin(), funcs.end(), isDecrementOperator));
- }
-
- for (const auto &func : funcs) {
- int args;
- if (func->isComparisonOperator()) {
- args = -1;
- } else {
- args = func->arguments().size();
- }
- QPair<QString, int > op(func->name(), args);
- results[op].append(func);
- }
- QList<AbstractMetaFunctionCList> result;
- result.reserve(results.size());
- for (auto it = results.cbegin(), end = results.cend(); it != end; ++it)
- result.append(it.value());
- return result;
-}
-
-CppGenerator::BoolCastFunctionOptional
- CppGenerator::boolCast(const AbstractMetaClass *metaClass) const
-{
- const auto *te = metaClass->typeEntry();
- if (te->isSmartPointer()) {
- auto *ste = static_cast<const SmartPointerTypeEntry *>(te);
-
- auto valueCheckMethod = ste->valueCheckMethod();
- if (!valueCheckMethod.isEmpty()) {
- const auto func = metaClass->findFunction(valueCheckMethod);
- if (func.isNull())
- throw Exception(msgMethodNotFound(metaClass, valueCheckMethod));
- return BoolCastFunction{func, false};
- }
-
- auto nullCheckMethod = ste->nullCheckMethod();
- if (!nullCheckMethod.isEmpty()) {
- const auto func = metaClass->findFunction(nullCheckMethod);
- if (func.isNull())
- throw Exception(msgMethodNotFound(metaClass, nullCheckMethod));
- return BoolCastFunction{func, true};
- }
- }
-
- auto mode = te->operatorBoolMode();
- if (useOperatorBoolAsNbNonZero()
- ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
- const auto func = metaClass->findOperatorBool();
- if (!func.isNull())
- return BoolCastFunction{func, false};
- }
-
- mode = te->isNullMode();
- if (useIsNullAsNbNonZero()
- ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
- const auto func = metaClass->findQtIsNullMethod();
- if (!func.isNull())
- return BoolCastFunction{func, true};
- }
- return std::nullopt;
-}
-
-std::optional<AbstractMetaType>
- CppGenerator::findSmartPointerInstantiation(const SmartPointerTypeEntry *pointer,
- const TypeEntry *pointee) const
-{
- for (const auto &smp : api().instantiatedSmartPointers()) {
- const auto &i = smp.type;
- if (i.typeEntry() == pointer && i.instantiations().at(0).typeEntry() == pointee)
- return i;
- }
- return {};
-}
-
void CppGenerator::clearTpFuncs()
{
+ // Functions that should not be registered under a name in PyMethodDef,
+ // but under a special constant under slots.
m_tpFuncs = {
{u"__str__"_s, {}}, {u"__str__"_s, {}},
- {reprFunction(), {}}, {u"__iter__"_s, {}},
+ {REPR_FUNCTION, {}}, {u"__iter__"_s, {}},
{u"__next__"_s, {}}
};
+ m_nbFuncs = { {u"__abs__"_s, {}}, {u"__pow__"_s, {} }};
}
// Prevent ELF symbol qt_version_tag from being generated into the source
@@ -333,7 +255,7 @@ static const char includeQDebug[] =
"#endif\n"
"#include <QtCore/QDebug>\n";
-static QString chopType(QString s)
+QString CppGenerator::chopType(QString s)
{
if (s.endsWith(u"_Type"))
s.chop(5);
@@ -367,18 +289,121 @@ static QString buildPropertyString(const QPropertySpec &spec)
return text;
}
+static QString _plainName(const QString &s)
+{
+ auto cutPos = s.lastIndexOf(u"::"_s);
+ return cutPos < 0 ? s : s.right(s.length() - (cutPos + 2));
+}
+
+/**********************************************************************
+ *
+ * Decision whether to use an IntEnum/IntFlag
+ * ------------------------------------------
+ *
+ * Unfortunately, all attempts to drive this decision automagically
+ * did not work out. We therefore compile a list in with known
+ * IntEnum and IntFlag.
+ */
+
+/*
+ * This function is now unused and replaced by TypeSystem::PythonEnumType
+ */
+#if 0
+static QSet<QString> useIntSet()
+{
+ static const QSet<QString> result{
+ /* IntEnum */ u"PySide6.QtCore.QDataStream.Version"_s,
+ /* IntEnum */ u"PySide6.QtCore.QEvent.Type"_s,
+ /* IntEnum */ u"PySide6.QtCore.QLocale.FloatingPointPrecisionOption"_s,
+ /* IntFlag */ u"PySide6.QtCore.QLocale.LanguageCodeType"_s,
+ /* IntFlag */ u"PySide6.QtCore.QUrl.ComponentFormattingOption"_s,
+ // note: "QUrl::UrlFormattingOption" is set as IntFlag without flags
+ /* IntFlag */ u"PySide6.QtCore.QUrl.UrlFormattingOption"_s,
+ /* IntFlag */ u"PySide6.QtCore.Qt.AlignmentFlag"_s,
+ /* IntFlag */ u"PySide6.QtCore.Qt.FocusPolicy"_s,
+ /* IntEnum */ u"PySide6.QtCore.Qt.GestureType"_s,
+ /* IntEnum */ u"PySide6.QtCore.Qt.ItemDataRole"_s,
+ /* IntEnum */ u"PySide6.QtCore.Qt.Key"_s,
+ /* Flag */ u"PySide6.QtCore.Qt.Modifier"_s,
+ // note: "Qt::TextFlag" is set as IntFlag without flags
+ /* IntFlag */ u"PySide6.QtCore.Qt.TextFlag"_s,
+ /* IntFlag */ u"PySide6.QtCore.Qt.WindowType"_s,
+ // This is found in QtWidgets but should be in QtGui.
+ /* IntEnum */ u"PySide6.QtGui.QFileSystemModel.Roles"_s,
+ /* IntEnum */ u"PySide6.QtGui.QFont.Stretch"_s,
+ /* IntEnum */ u"PySide6.QtGui.QFont.Weight"_s,
+ /* IntEnum */ u"PySide6.QtGui.QTextDocument.ResourceType"_s,
+ /* IntEnum */ u"PySide6.QtGui.QTextFormat.FormatType"_s,
+ /* IntEnum */ u"PySide6.QtGui.QTextFormat.ObjectTypes"_s,
+ /* IntEnum */ u"PySide6.QtGui.QTextFormat.Property"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QDialog.DialogCode"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QFrame.Shadow"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QFrame.Shape"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QListWidgetItem.ItemType"_s,
+ /* IntFlag */ u"PySide6.QtWidgets.QMessageBox.StandardButton"_s,
+ // note: "QSizePolicy::PolicyFlag" is set as IntFlag without flags
+ /* IntFlag */ u"PySide6.QtWidgets.QSizePolicy.PolicyFlag"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.ComplexControl"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.ContentsType"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.ControlElement"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.PixelMetric"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.PrimitiveElement"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.StandardPixmap"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.StyleHint"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QStyle.SubElement"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QTableWidgetItem.ItemType"_s,
+ /* IntEnum */ u"PySide6.QtWidgets.QTreeWidgetItem.ItemType"_s,
+ /* IntEnum */ u"PySide6.QtCharts.QBoxSet.ValuePositions"_s,
+ /* IntEnum */ u"PySide6.QtMultimedia.QMediaPlayer.Loops"_s,
+ /* IntEnum */ u"PySide6.QtQuick.QSGGeometry.DrawingMode"_s,
+ /* IntEnum */ u"PySide6.QtWebEngineCore.QWebEngineScript.ScriptWorldId"_s,
+ // Added because it should really be used as number
+ /* IntEnum */ u"PySide6.QtCore.QMetaType.Type"_s,
+ /* IntEnum */ u"PySide6.QtSerialPort.QSerialPort.BaudRate"_s,
+ };
+ return result;
+}
+#endif
+
+static bool _shouldInheritInt(const AbstractMetaEnum &cppEnum)
+{
+ return !cppEnum.fullName().startsWith(u"PySide6."_s);
+}
+
+static QString BuildEnumFlagInfo(const AbstractMetaEnum &cppEnum)
+{
+ auto enumType = cppEnum.typeEntry();
+ QString result = _plainName(enumType->name());
+ auto flags = enumType->flags();
+ auto decision = enumType->pythonEnumType();
+ bool _int = _shouldInheritInt(cppEnum);
+ bool _flag = bool(flags);
+
+ if (decision != TypeSystem::PythonEnumType::Unspecified) {
+ _int = decision == TypeSystem::PythonEnumType::IntEnum ||
+ decision == TypeSystem::PythonEnumType::IntFlag;
+ _flag = decision == TypeSystem::PythonEnumType::Flag ||
+ decision == TypeSystem::PythonEnumType::IntFlag;
+ }
+ result += _flag ? (_int ? u":IntFlag"_s : u":Flag"_s)
+ : (_int ? u":IntEnum"_s : u":Enum"_s);
+ if (flags)
+ result += u':' + _plainName(flags->flagsName());
+ return u'"' + result + u'"';
+}
+
static void writePyGetSetDefEntry(TextStream &s, const QString &name,
const QString &getFunc, const QString &setFunc)
{
- s << "{const_cast<char *>(\"" << name << "\"), " << getFunc << ", "
- << (setFunc.isEmpty() ? NULL_PTR : setFunc) << "},\n";
+ s << "{const_cast<char *>(\"" << mangleName(name) << "\"), " << getFunc << ", "
+ << (setFunc.isEmpty() ? NULL_PTR : setFunc) << ", nullptr, nullptr},\n";
}
static bool generateRichComparison(const GeneratorContext &c)
{
- auto *metaClass = c.metaClass();
+ const auto metaClass = c.metaClass();
if (c.forSmartPointer()) {
- auto *te = static_cast<const SmartPointerTypeEntry *>(metaClass->typeEntry());
+ auto te = std::static_pointer_cast<const SmartPointerTypeEntry>(metaClass->typeEntry());
return te->smartPointerType() == TypeSystem::SmartPointerType::Shared;
}
@@ -389,17 +414,16 @@ void CppGenerator::generateIncludes(TextStream &s, const GeneratorContext &class
const IncludeGroupList &includes,
const AbstractMetaClassCList &innerClasses) const
{
- const AbstractMetaClass *metaClass = classContext.metaClass();
+ const auto metaClass = classContext.metaClass();
// write license comment
s << licenseComment() << '\n';
const bool normalClass = !classContext.forSmartPointer();
- if (normalClass && !avoidProtectedHack() && !metaClass->isNamespace()
- && !metaClass->hasPrivateDestructor()) {
- s << "//workaround to access protected functions\n";
- s << "#define protected public\n\n";
- }
+ // Normally only required for classes for which we want to generate protected API,
+ // but it needs to be generated into all files to ensure ODR for Unity builds.
+ if (!avoidProtectedHack())
+ s << HeaderGenerator::protectedHackDefine;
QByteArrayList cppIncludes{"typeinfo", "iterator", // for containers
"cctype", "cstring"};
@@ -413,7 +437,9 @@ void CppGenerator::generateIncludes(TextStream &s, const GeneratorContext &class
if (normalClass && usePySideExtensions()) {
s << includeQDebug;
- if (metaClass->isQObject()) {
+ if (metaClass->hasToStringCapability())
+ s << "#include <QtCore/QBuffer>\n";
+ if (isQObject(metaClass)) {
s << "#include <pysideqobject.h>\n"
<< "#include <pysidesignal.h>\n"
<< "#include <pysideproperty.h>\n"
@@ -443,58 +469,60 @@ void CppGenerator::generateIncludes(TextStream &s, const GeneratorContext &class
if (!innerClasses.isEmpty()) {
s << "\n// inner classes\n";
- for (const AbstractMetaClass *innerClass : innerClasses) {
+ for (const auto &innerClass : innerClasses) {
GeneratorContext innerClassContext = contextForClass(innerClass);
s << "#include \""
<< HeaderGenerator::headerFileNameForContext(innerClassContext) << "\"\n";
}
}
+ if (avoidProtectedHack())
+ s << baseWrapperIncludes(classContext);
+
for (const auto &g : includes)
s << g;
// C++ includes
std::sort(cppIncludes.begin(), cppIncludes.end());
s << '\n';
- for (const auto &i : qAsConst(cppIncludes))
+ for (const auto &i : std::as_const(cppIncludes))
s << "#include <" << i << ">\n";
}
-static const char openTargetExternC[] = R"(
-// Target ---------------------------------------------------------
-
-extern "C" {
-)";
-
-static const char closeExternC[] = "} // extern \"C\"\n\n";
-
// Write methods definition
-static void writePyMethodDefs(TextStream &s, const QString &className,
- const QString &methodsDefinitions, bool generateCopy)
+void CppGenerator::writePyMethodDefs(TextStream &s, const QString &className,
+ const QString &methodsDefinitions)
{
s << "static PyMethodDef " << className << "_methods[] = {\n" << indent
- << methodsDefinitions << '\n';
- if (generateCopy) {
- s << "{\"__copy__\", reinterpret_cast<PyCFunction>(" << className << "___copy__)"
- << ", METH_NOARGS},\n";
+ << methodsDefinitions << METHOD_DEF_SENTINEL << outdent << "};\n\n";
+}
+
+void CppGenerator::writeModuleCodeSnips(TextStream &s, const CodeSnipList &codeSnips,
+ TypeSystem::CodeSnipPosition position,
+ TypeSystem::Language language) const
+{
+ if (!codeSnips.isEmpty()) {
+ try {
+ writeCodeSnips(s, codeSnips, position, language);
+ } catch (const std::exception &e) {
+ throw Exception(msgSnippetError("module source of "_L1 + moduleName(), e.what()));
+ }
}
- s << '{' << NULL_PTR << ", " << NULL_PTR << "} // Sentinel\n" << outdent
- << "};\n\n";
}
-static bool hasHashFunction(const AbstractMetaClass *c)
+bool CppGenerator::hasHashFunction(const AbstractMetaClassCPtr &c)
{
return !c->typeEntry()->hashFunction().isEmpty()
|| c->hasHashFunction();
}
-static bool needsTypeDiscoveryFunction(const AbstractMetaClass *c)
+static bool needsTypeDiscoveryFunction(const AbstractMetaClassCPtr &c)
{
return c->baseClass() != nullptr
&& (c->isPolymorphic() || !c->typeEntry()->polymorphicIdValue().isEmpty());
}
-static void writeAddedTypeSignatures(TextStream &s, const ComplexTypeEntry *te)
+static void writeAddedTypeSignatures(TextStream &s, const ComplexTypeEntryCPtr &te)
{
for (const auto &e : te->addedPyMethodDefEntrys()) {
if (auto count = e.signatures.size()) {
@@ -518,12 +546,12 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
}
s.setLanguage(TextStream::Language::Cpp);
- const AbstractMetaClass *metaClass = classContext.metaClass();
- const auto *typeEntry = metaClass->typeEntry();
+ AbstractMetaClassCPtr metaClass = classContext.metaClass();
+ const auto typeEntry = metaClass->typeEntry();
auto innerClasses = metaClass->innerClasses();
for (auto it = innerClasses.begin(); it != innerClasses.end(); ) {
- auto *innerTypeEntry = (*it)->typeEntry();
+ auto innerTypeEntry = (*it)->typeEntry();
if (shouldGenerate(innerTypeEntry) && !innerTypeEntry->isSmartPointer())
++it;
else
@@ -533,20 +561,9 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
AbstractMetaEnumList classEnums = metaClass->enums();
metaClass->getEnumsFromInvisibleNamespacesToBeGenerated(&classEnums);
- //Extra includes
IncludeGroupList includeGroups;
- if (!classContext.useWrapper()) {
- includeGroups.append(IncludeGroup{u"Extra includes"_s,
- typeEntry->extraIncludes()});
- }
-
- includeGroups.append(IncludeGroup{u"Argument includes"_s,
- typeEntry->argumentIncludes()});
-
- includeGroups.append(IncludeGroup{u"Enum includes"_s, {}});
- for (const AbstractMetaEnum &cppEnum : qAsConst(classEnums))
- includeGroups.back().includes.append(cppEnum.typeEntry()->extraIncludes());
-
+ if (!classContext.useWrapper() || !avoidProtectedHack())
+ includeGroups.append(classIncludes(metaClass));
generateIncludes(s, classContext, includeGroups, innerClasses);
if (typeEntry->typeFlags().testFlag(ComplexTypeEntry::Deprecated))
@@ -554,10 +571,10 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
// Use class base namespace
{
- const AbstractMetaClass *context = metaClass->enclosingClass();
+ AbstractMetaClassCPtr context = metaClass->enclosingClass();
while (context) {
if (context->isNamespace() && !context->enclosingClass()
- && static_cast<const NamespaceTypeEntry *>(context->typeEntry())->generateUsing()) {
+ && std::static_pointer_cast<const NamespaceTypeEntry>(context->typeEntry())->generateUsing()) {
s << "\nusing namespace " << context->qualifiedCppName() << ";\n";
break;
}
@@ -565,7 +582,7 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
}
}
- s << '\n' << typeNameFunc << '\n';
+ s << '\n';
// class inject-code native/beginning
if (!typeEntry->codeSnips().isEmpty()) {
@@ -576,19 +593,22 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
}
// python conversion rules
- if (typeEntry->hasTargetConversionRule()) {
- s << "// Python Conversion\n";
- s << typeEntry->targetConversionRule() << '\n';
+ if (typeEntry->isValue()) {
+ auto vte = std::static_pointer_cast<const ValueTypeEntry>(typeEntry);
+ if (vte->hasTargetConversionRule()) {
+ s << "// Python Conversion\n";
+ s << vte->targetConversionRule() << '\n';
+ }
}
if (classContext.useWrapper()) {
s << "// Native ---------------------------------------------------------\n\n";
if (avoidProtectedHack() && usePySideExtensions()) {
- s << "void " << classContext.wrapperName() << "::pysideInitQtMetaTypes()\n{\n";
- Indentation indent(s);
+ s << "void " << classContext.wrapperName() << "::pysideInitQtMetaTypes()\n{\n"
+ << indent;
writeInitQtMetaTypeFunctionBody(s, classContext);
- s << "}\n\n";
+ s << outdent << "}\n\n";
}
int maxOverrides = 0;
@@ -601,13 +621,15 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
writeVirtualMethodNative(s, func, maxOverrides++);
}
- if (!avoidProtectedHack() || !metaClass->hasPrivateDestructor()) {
- if (usePySideExtensions() && metaClass->isQObject())
- writeMetaObjectMethod(s, classContext);
+ if (shouldGenerateMetaObjectFunctions(metaClass))
+ writeMetaObjectMethod(s, classContext);
+ if (!avoidProtectedHack() || !metaClass->hasPrivateDestructor())
writeDestructorNative(s, classContext);
- }
}
+ for (const auto &f : metaClass->userAddedPythonOverrides())
+ writeUserAddedPythonOverride(s, f);
+
StringStream smd(TextStream::Language::Cpp);
StringStream md(TextStream::Language::Cpp);
StringStream signatureStream(TextStream::Language::Cpp);
@@ -647,22 +669,22 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
smd << "static PyMethodDef " << methDefName << " = " << indent
<< defEntries.constFirst() << outdent << ";\n\n";
}
- if (!m_tpFuncs.contains(rfunc->name()))
+ const auto &fname = rfunc->name();
+ if (!m_tpFuncs.contains(fname) && !m_nbFuncs.contains(fname))
md << defEntries;
}
}
for (const auto &pyMethodDef : typeEntry->addedPyMethodDefEntrys())
md << pyMethodDef << ",\n";
+
+ if (typeEntry->isValue())
+ writeCopyFunction(s, md, signatureStream, classContext);
+
const QString methodsDefinitions = md.toString();
const QString singleMethodDefinitions = smd.toString();
const QString className = chopType(cpythonTypeName(metaClass));
- if (typeEntry->isValue()) {
- writeCopyFunction(s, classContext);
- signatureStream << fullPythonClassName(metaClass) << ".__copy__()\n";
- }
-
// Write single method definitions
s << singleMethodDefinitions;
@@ -678,14 +700,27 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
s << '\n';
s << "static const char *" << className << "_PropertyStrings[] = {\n" << indent;
- for (const auto &entry : qAsConst(sorter))
+ for (const auto &entry : std::as_const(sorter))
+ s << entry << ",\n";
+ s << NULL_PTR << " // Sentinel\n"
+ << outdent << "};\n\n";
+
+ }
+ // PYSIDE-1735: Write an EnumFlagInfo structure
+ QStringList sorter;
+ for (const auto &entry : std::as_const(classEnums))
+ sorter.append(BuildEnumFlagInfo(entry));
+ sorter.sort();
+ if (!sorter.empty()) {
+ s << "static const char *" << className << "_EnumFlagInfo[] = {\n" << indent;
+ for (const auto &entry : std::as_const(sorter))
s << entry << ",\n";
s << NULL_PTR << " // Sentinel\n"
<< outdent << "};\n\n";
}
// Write methods definition
- writePyMethodDefs(s, className, methodsDefinitions, typeEntry->isValue());
+ writePyMethodDefs(s, className, methodsDefinitions);
// Write tp_s/getattro function
const AttroCheck attroCheck = checkAttroFunctionNeeds(metaClass);
@@ -698,25 +733,8 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
writeNbBoolFunction(classContext, f.value(), s);
if (supportsNumberProtocol(metaClass)) {
- const QList<AbstractMetaFunctionCList> opOverloads = filterGroupedOperatorFunctions(
- metaClass,
- OperatorQueryOption::ArithmeticOp
- | OperatorQueryOption::IncDecrementOp
- | OperatorQueryOption::LogicalOp
- | OperatorQueryOption::BitwiseOp);
-
- for (const AbstractMetaFunctionCList &allOverloads : opOverloads) {
- AbstractMetaFunctionCList overloads;
- for (const auto &func : allOverloads) {
- if (!func->isModifiedRemoved()
- && !func->isPrivate()
- && (func->ownerClass() == func->implementingClass() || func->isAbstract()))
- overloads.append(func);
- }
-
- if (overloads.isEmpty())
- continue;
-
+ const auto numberProtocolOps = numberProtocolOperators(metaClass);
+ for (const auto &overloads : numberProtocolOps) {
OverloadData overloadData(overloads, api());
writeMethodWrapper(s, overloadData, classContext);
writeSignatureInfo(signatureStream, overloadData);
@@ -779,7 +797,7 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
cpythonGetterFunctionName(property, metaClass), setter);
}
}
- s << '{' << NULL_PTR << "} // Sentinel\n"
+ s << "{nullptr, nullptr, nullptr, nullptr, nullptr} // Sentinel\n"
<< outdent << "};\n\n";
}
@@ -795,11 +813,10 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
writeClassDefinition(s, metaClass, classContext);
s << '\n';
- if (needsTypeDiscoveryFunction(metaClass))
+ if (needsTypeDiscoveryFunction(metaClass)) {
writeTypeDiscoveryFunction(s, metaClass);
-
- writeFlagsNumberMethodsDefinitions(s, classEnums);
- s << '\n';
+ s << '\n';
+ }
writeConverterFunctions(s, metaClass, classContext);
writeAddedTypeSignatures(signatureStream, typeEntry);
@@ -817,145 +834,6 @@ void CppGenerator::generateClass(TextStream &s, const GeneratorContext &classCon
}
}
-static bool hasParameterPredicate(const AbstractMetaFunctionCPtr &f)
-{
- return !f->arguments().isEmpty();
-}
-
-void CppGenerator::generateSmartPointerClass(TextStream &s, const GeneratorContext &classContext)
-{
- s.setLanguage(TextStream::Language::Cpp);
- const AbstractMetaClass *metaClass = classContext.metaClass();
- const auto *typeEntry = static_cast<const SmartPointerTypeEntry *>(metaClass->typeEntry());
- const bool hasPointeeClass = classContext.pointeeClass() != nullptr;
- const auto smartPointerType = typeEntry->smartPointerType();
- const bool isValueHandle = smartPointerType ==TypeSystem::SmartPointerType::ValueHandle;
-
- IncludeGroup includes{u"Extra includes"_s, typeEntry->extraIncludes()};
- generateIncludes(s, classContext, {includes});
-
- s << '\n' << typeNameFunc << '\n';
-
- // Create string literal for smart pointer getter method.
- QString rawGetter = typeEntry->getter();
- s << "static const char " << SMART_POINTER_GETTER << "[] = \"" << rawGetter << "\";";
-
- // class inject-code native/beginning
- if (!typeEntry->codeSnips().isEmpty()) {
- writeClassCodeSnips(s, typeEntry->codeSnips(),
- TypeSystem::CodeSnipPositionBeginning, TypeSystem::NativeCode,
- classContext);
- s << '\n';
- }
-
- StringStream smd(TextStream::Language::Cpp);
- StringStream md(TextStream::Language::Cpp);
- StringStream signatureStream(TextStream::Language::Cpp);
-
- s << openTargetExternC;
-
- const auto &functionGroups = getFunctionGroups(metaClass);
-
- // Skip all public methods of the smart pointer except for the special
- // methods declared in the type entry.
-
- auto ctors = metaClass->queryFunctions(FunctionQueryOption::Constructors);
- if (!hasPointeeClass && !isValueHandle) { // Cannot generate "int*"
- auto end = std::remove_if(ctors.begin(), ctors.end(), hasParameterPredicate);
- ctors.erase(end, ctors.end());
- }
-
- if (!ctors.isEmpty()) {
- OverloadData overloadData(ctors, api());
- writeConstructorWrapper(s, overloadData, classContext);
- writeSignatureInfo(signatureStream, overloadData);
- }
-
- if (!typeEntry->resetMethod().isEmpty()) {
- auto it = functionGroups.constFind(typeEntry->resetMethod());
- if (it == functionGroups.cend())
- throw Exception(msgCannotFindSmartPointerMethod(typeEntry, typeEntry->resetMethod()));
- AbstractMetaFunctionCList resets = it.value();
- if (!hasPointeeClass && !isValueHandle) { // Cannot generate "int*"
- auto end = std::remove_if(resets.begin(), resets.end(), hasParameterPredicate);
- resets.erase(end, resets.end());
- }
- if (!resets.isEmpty())
- writeMethodWrapper(s, md, signatureStream, resets, classContext);
- }
-
- auto it = functionGroups.constFind(rawGetter);
- if (it == functionGroups.cend() || it.value().size() != 1)
- throw Exception(msgCannotFindSmartPointerGetter(typeEntry));
-
- writeMethodWrapper(s, md, signatureStream, it.value(), classContext);
-
- QStringList optionalMethods;
- if (!typeEntry->refCountMethodName().isEmpty())
- optionalMethods.append(typeEntry->refCountMethodName());
- const QString valueCheckMethod = typeEntry->valueCheckMethod();
- if (!valueCheckMethod.isEmpty() && !valueCheckMethod.startsWith(u"operator"))
- optionalMethods.append(valueCheckMethod);
- if (!typeEntry->nullCheckMethod().isEmpty())
- optionalMethods.append(typeEntry->nullCheckMethod());
-
- for (const QString &optionalMethod : optionalMethods) {
- auto it = functionGroups.constFind(optionalMethod);
- if (it == functionGroups.cend() || it.value().size() != 1)
- throw Exception(msgCannotFindSmartPointerMethod(typeEntry, optionalMethod));
- writeMethodWrapper(s, md, signatureStream, it.value(), classContext);
- }
-
- const QString methodsDefinitions = md.toString();
- const QString singleMethodDefinitions = smd.toString();
-
- const QString className = chopType(cpythonTypeName(typeEntry));
-
- writeCopyFunction(s, classContext);
- signatureStream << fullPythonClassName(metaClass) << ".__copy__()\n";
-
- // Write single method definitions
- s << singleMethodDefinitions;
-
- // Write methods definition
- writePyMethodDefs(s, className, methodsDefinitions, true /* ___copy__ */);
-
- // Write tp_s/getattro function
- const auto boolCastOpt = boolCast(metaClass);
- writeSmartPointerGetattroFunction(s, classContext, boolCastOpt);
- writeSmartPointerSetattroFunction(s, classContext);
-
- if (boolCastOpt.has_value())
- writeNbBoolFunction(classContext, boolCastOpt.value(), s);
-
- if (smartPointerType == TypeSystem::SmartPointerType::Shared)
- writeSmartPointerRichCompareFunction(s, classContext);
-
- s << closeExternC;
-
- if (hasHashFunction(metaClass))
- writeHashFunction(s, classContext);
-
- // Write tp_traverse and tp_clear functions.
- writeTpTraverseFunction(s, metaClass);
- writeTpClearFunction(s, metaClass);
-
- writeClassDefinition(s, metaClass, classContext);
-
- s << '\n';
-
- writeConverterFunctions(s, metaClass, classContext);
- writeClassRegister(s, metaClass, classContext, signatureStream);
-
- // class inject-code native/end
- if (!typeEntry->codeSnips().isEmpty()) {
- writeClassCodeSnips(s, typeEntry->codeSnips(),
- TypeSystem::CodeSnipPositionEnd, TypeSystem::NativeCode,
- classContext);
- s << '\n';
- }
-}
-
void CppGenerator::writeMethodWrapper(TextStream &s, TextStream &definitionStream,
TextStream &signatureStream,
const AbstractMetaFunctionCList &overloads,
@@ -997,7 +875,7 @@ void CppGenerator::writeConstructorNative(TextStream &s, const GeneratorContext
}
void CppGenerator::writeDestructorNative(TextStream &s,
- const GeneratorContext &classContext) const
+ const GeneratorContext &classContext)
{
s << classContext.wrapperName() << "::~"
<< classContext.wrapperName() << "()\n{\n" << indent;
@@ -1022,9 +900,10 @@ QString CppGenerator::getVirtualFunctionReturnTypeName(const AbstractMetaFunctio
// SbkType would return null when the type is a container.
auto typeEntry = func->type().typeEntry();
if (typeEntry->isContainer()) {
- const auto *cte = static_cast<const ContainerTypeEntry *>(typeEntry);
+ const auto cte = std::static_pointer_cast<const ContainerTypeEntry>(typeEntry);
switch (cte->containerKind()) {
case ContainerTypeEntry::ListContainer:
+ case ContainerTypeEntry::SpanContainer:
break;
case ContainerTypeEntry::SetContainer:
return uR"("set")"_s;
@@ -1051,8 +930,8 @@ QString CppGenerator::getVirtualFunctionReturnTypeName(const AbstractMetaFunctio
if (func->type().isPrimitive())
return u'"' + func->type().name() + u'"';
- return u"reinterpret_cast<PyTypeObject *>(Shiboken::SbkType< "_s
- + typeEntry->qualifiedCppName() + u" >())->tp_name"_s;
+ return u"Shiboken::SbkType< "_s
+ + typeEntry->qualifiedCppName() + u" >()->tp_name"_s;
}
// When writing an overridden method of a wrapper class, write the part
@@ -1062,8 +941,8 @@ void CppGenerator::writeVirtualMethodCppCall(TextStream &s,
const QString &funcName,
const CodeSnipList &snips,
const AbstractMetaArgument *lastArg,
- const TypeEntry *retType,
- const QString &returnStatement) const
+ const TypeEntryCPtr &retType,
+ const QString &returnStatement, bool hasGil) const
{
if (!snips.isEmpty()) {
writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning,
@@ -1071,12 +950,17 @@ void CppGenerator::writeVirtualMethodCppCall(TextStream &s,
}
if (func->isAbstract()) {
+ if (!hasGil)
+ s << "Shiboken::GilState gil;\n";
s << "Shiboken::Errors::setPureVirtualMethodError(\""
<< func->ownerClass()->name() << '.' << funcName << "\");\n"
<< returnStatement << '\n';
return;
}
+ if (hasGil)
+ s << "gil.release();\n";
+
if (retType)
s << "return ";
s << "this->::" << func->implementingClass()->qualifiedCppName() << "::";
@@ -1092,17 +976,24 @@ void CppGenerator::writeVirtualMethodCppCall(TextStream &s,
}
// Determine the return statement (void or a result value).
-QString CppGenerator::virtualMethodReturn(TextStream &s, const ApiExtractorResult &api,
- const AbstractMetaFunctionCPtr &func,
- const FunctionModificationList &functionModifications)
+
+CppGenerator::VirtualMethodReturn
+ CppGenerator::virtualMethodReturn(const ApiExtractorResult &api,
+ const AbstractMetaFunctionCPtr &func,
+ const FunctionModificationList &functionModifications)
{
- if (func->isVoid())
- return u"return;"_s;
+ VirtualMethodReturn result;
+ if (func->isVoid()) {
+ result.statement = "return;"_L1;
+ return result;
+ }
+
+ result.statement = "return "_L1;
const AbstractMetaType &returnType = func->type();
for (const FunctionModification &mod : functionModifications) {
for (const ArgumentModification &argMod : mod.argument_mods()) {
if (argMod.index() == 0 && !argMod.replacedDefaultExpression().isEmpty()) {
- static const QRegularExpression regex(QStringLiteral("%(\\d+)"));
+ static const QRegularExpression regex("%(\\d+)"_L1);
Q_ASSERT(regex.isValid());
QString expr = argMod.replacedDefaultExpression();
for (int offset = 0; ; ) {
@@ -1118,8 +1009,8 @@ QString CppGenerator::virtualMethodReturn(TextStream &s, const ApiExtractorResul
offset = match.capturedStart(1);
}
DefaultValue defaultReturnExpr(DefaultValue::Custom, expr);
- return u"return "_s + defaultReturnExpr.returnValue()
- + u';';
+ result.statement += defaultReturnExpr.returnValue() + u';';
+ return result;
}
}
}
@@ -1131,31 +1022,28 @@ QString CppGenerator::virtualMethodReturn(TextStream &s, const ApiExtractorResul
errorMsg = msgCouldNotFindMinimalConstructor(errorMsg,
func->type().cppSignature(),
errorMessage);
- qCWarning(lcShiboken).noquote().nospace() << errorMsg;
- s << "\n#error " << errorMsg << '\n';
- }
- if (returnType.referenceType() == LValueReference) {
- s << "static " << returnType.typeEntry()->qualifiedCppName()
- << " result;\n";
- return u"return result;"_s;
+ throw Exception(errorMsg);
}
- return u"return "_s + defaultReturnExpr->returnValue()
- + u';';
+
+ result.needsReference = returnType.referenceType() == LValueReference;
+ result.statement += (result.needsReference
+ ? virtualMethodStaticReturnVar : defaultReturnExpr->returnValue()) + u';';
+ return result;
}
// Create an argument for Py_BuildValue() when writing virtual methods.
// Return a pair of (argument, format-char).
-QPair<QString, QChar> CppGenerator::virtualMethodNativeArg(const AbstractMetaFunctionCPtr &func,
- const AbstractMetaArgument &arg)
+std::pair<QString, QChar> CppGenerator::virtualMethodNativeArg(const AbstractMetaFunctionCPtr &func,
+ const AbstractMetaArgument &arg)
{
if (func->hasConversionRule(TypeSystem::TargetLangCode, arg.argumentIndex() + 1))
return {arg.name() + CONV_RULE_OUT_VAR_SUFFIX, u'N'};
const auto &type = arg.type();
- auto *argTypeEntry = type.typeEntry();
+ auto argTypeEntry = type.typeEntry();
// Check for primitive types convertible by Py_BuildValue()
if (argTypeEntry->isPrimitive() && !type.isCString()) {
- const auto *pte = argTypeEntry->asPrimitive()->basicReferencedTypeEntry();
+ const auto pte = basicReferencedTypeEntry(argTypeEntry);
auto it = formatUnits().constFind(pte->name());
if (it != formatUnits().constEnd())
return {arg.name(), it.value()};
@@ -1167,10 +1055,41 @@ QPair<QString, QChar> CppGenerator::virtualMethodNativeArg(const AbstractMetaFun
return {ac.toString(), u'N'};
}
+static const char PYTHON_ARGS_ARRAY[] = "pyArgArray";
+
+void CppGenerator::writeVirtualMethodNativeVectorCallArgs(TextStream &s,
+ const AbstractMetaFunctionCPtr &func,
+ const AbstractMetaArgumentList &arguments,
+ const QList<int> &invalidateArgs)
+{
+ Q_ASSERT(!arguments.isEmpty());
+ s << "PyObject *" << PYTHON_ARGS_ARRAY <<'[' << arguments.size() << "] = {\n" << indent;
+ const qsizetype last = arguments.size() - 1;
+ for (qsizetype i = 0; i <= last; ++i) {
+ const AbstractMetaArgument &arg = arguments.at(i);
+ if (func->hasConversionRule(TypeSystem::TargetLangCode, arg.argumentIndex() + 1)) {
+ s << arg.name() + CONV_RULE_OUT_VAR_SUFFIX;
+ } else {
+ writeToPythonConversion(s, arg.type(), func->ownerClass(), arg.name());
+ }
+ if (i < last)
+ s << ',';
+ s << '\n';
+ }
+ s << outdent << "};\n";
+
+ if (!invalidateArgs.isEmpty())
+ s << '\n';
+ for (int index : invalidateArgs) {
+ s << "const bool invalidateArg" << index << " = Py_REFCNT(" << PYTHON_ARGS_ARRAY <<
+ '[' << index - 1 << "]) == 1;\n";
+ }
+}
+
void CppGenerator::writeVirtualMethodNativeArgs(TextStream &s,
const AbstractMetaFunctionCPtr &func,
const AbstractMetaArgumentList &arguments,
- const QList<int> &invalidateArgs) const
+ const QList<int> &invalidateArgs)
{
s << "Shiboken::AutoDecRef " << PYTHON_ARGS << '(';
if (arguments.isEmpty()) {
@@ -1189,22 +1108,61 @@ void CppGenerator::writeVirtualMethodNativeArgs(TextStream &s,
s << "Py_BuildValue(\"(" << format << ")\",\n"
<< indent << argConversions.join(u",\n"_s) << outdent << "\n));\n";
- for (int index : qAsConst(invalidateArgs)) {
- s << "bool invalidateArg" << index << " = PyTuple_GET_ITEM(" << PYTHON_ARGS
- << ", " << index - 1 << ")->ob_refcnt == 1;\n";
+ for (int index : std::as_const(invalidateArgs)) {
+ s << "bool invalidateArg" << index << " = Py_REFCNT(PyTuple_GET_ITEM(" << PYTHON_ARGS
+ << ", " << index - 1 << ")) == 1;\n";
}
}
-static bool isArgumentRemoved(const AbstractMetaArgument &a)
+static bool isArgumentNotRemoved(const AbstractMetaArgument &a)
{
- return a.isModifiedRemoved();
+ return !a.isModifiedRemoved();
+}
+
+// PyObject_Vectorcall(): since 3.9
+static const char vectorCallCondition[] =
+ "#if !defined(PYPY_VERSION) && !defined(Py_LIMITED_API)\n";
+
+// PyObject_CallNoArgs(): since 3.9, stable API since 3.10
+static const char noArgsCallCondition[] =
+ "#if !defined(PYPY_VERSION) && ((defined(Py_LIMITED_API) && Py_LIMITED_API >= 0x030A0000) || !defined(Py_LIMITED_API))\n";
+static const char inverseNoArgsCallCondition[] =
+ "#if defined(PYPY_VERSION) || (defined(Py_LIMITED_API) && Py_LIMITED_API < 0x030A0000)\n";
+
+static inline void writeVirtualMethodStaticReturnVar(TextStream &s, const AbstractMetaFunctionCPtr &func)
+{
+ s << "static " << func->type().typeEntry()->qualifiedCppName() << ' '
+ << virtualMethodStaticReturnVar << ";\n";
+}
+
+static void writeFuncNameVar(TextStream &s, const AbstractMetaFunctionCPtr &func,
+ const QString &funcName)
+{
+ // PYSIDE-1019: Add info about properties
+ int propFlag = 0;
+ if (func->isPropertyReader())
+ propFlag |= 1;
+ if (func->isPropertyWriter())
+ propFlag |= 2;
+ if (propFlag && func->isStatic())
+ propFlag |= 4;
+ QString propStr;
+ if (propFlag != 90)
+ propStr = QString::number(propFlag) + u':';
+
+ if (propFlag != 0)
+ s << "// This method belongs to a property.\n";
+ s << "static const char *funcName = \"";
+ if (propFlag != 0)
+ s << propFlag << ':';
+ s << funcName << "\";\n";
}
void CppGenerator::writeVirtualMethodNative(TextStream &s,
const AbstractMetaFunctionCPtr &func,
int cacheIndex) const
{
- const TypeEntry *retType = func->type().typeEntry();
+ TypeEntryCPtr retType = func->type().typeEntry();
const QString funcName = func->isOperatorOverload()
? pythonOperatorFunctionName(func) : func->definitionNames().constFirst();
@@ -1213,12 +1171,16 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
Generator::OriginalTypeDescription)
<< "\n{\n" << indent;
- const QString returnStatement = virtualMethodReturn(s, api(), func,
- func->modifications());
+ const auto returnStatement = virtualMethodReturn(api(), func,
+ func->modifications());
+
+ if (returnStatement.needsReference)
+ writeVirtualMethodStaticReturnVar(s, func);
- if (func->isAbstract() && func->isModifiedRemoved()) {
- qCWarning(lcShiboken, "%s", qPrintable(msgPureVirtualFunctionRemoved(func.data())));
- s << returnStatement << '\n' << outdent << "}\n\n";
+ const bool isAbstract = func->isAbstract();
+ if (isAbstract && func->isModifiedRemoved()) {
+ qCWarning(lcShiboken, "%s", qPrintable(msgPureVirtualFunctionRemoved(func.get())));
+ s << returnStatement.statement << '\n' << outdent << "}\n\n";
return;
}
@@ -1243,48 +1205,45 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
<< R"(] << '\n';)" << '\n';
}
// PYSIDE-803: Build a boolean cache for unused overrides
- const bool multi_line = func->isVoid() || !snips.isEmpty() || func->isAbstract();
- s << "if (m_PyMethodCache[" << cacheIndex << "])" << (multi_line ? " {\n" : "\n");
- {
- Indentation indentation(s);
- writeVirtualMethodCppCall(s, func, funcName, snips, lastArg, retType,
- returnStatement);
- }
+ const bool multi_line = func->isVoid() || !snips.isEmpty() || isAbstract;
+ s << "if (m_PyMethodCache[" << cacheIndex << "])" << (multi_line ? " {\n" : "\n")
+ << indent;
+ writeVirtualMethodCppCall(s, func, funcName, snips, lastArg, retType,
+ returnStatement.statement, false);
+ s << outdent;
if (multi_line)
s << "}\n";
s << "Shiboken::GilState gil;\n";
// Get out of virtual method call if someone already threw an error.
- s << "if (PyErr_Occurred())\n" << indent
- << returnStatement << '\n' << outdent;
-
- // PYSIDE-1019: Add info about properties
- int propFlag = 0;
- if (func->isPropertyReader())
- propFlag |= 1;
- if (func->isPropertyWriter())
- propFlag |= 2;
- if (propFlag && func->isStatic())
- propFlag |= 4;
- QString propStr;
- if (propFlag)
- propStr = QString::number(propFlag) + u':';
+ s << "if (" << shibokenErrorsOccurred << ")\n" << indent
+ << returnStatement.statement << '\n' << outdent;
s << "static PyObject *nameCache[2] = {};\n";
- if (propFlag)
- s << "// This method belongs to a property.\n";
- s << "static const char *funcName = \"" << propStr << funcName << "\";\n"
- << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR
+ writeFuncNameVar(s, func, funcName);
+ s << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR
<< "(Shiboken::BindingManager::instance().getOverride(this, nameCache, funcName));\n"
- << "if (" << PYTHON_OVERRIDE_VAR << ".isNull()) {\n"
- << indent << "gil.release();\n";
+ << "if (" << PYTHON_OVERRIDE_VAR << ".isNull()) {\n" << indent;
if (useOverrideCaching(func->ownerClass()))
s << "m_PyMethodCache[" << cacheIndex << "] = true;\n";
writeVirtualMethodCppCall(s, func, funcName, snips, lastArg, retType,
- returnStatement);
+ returnStatement.statement, true);
s << outdent << "}\n\n"; //WS
+ if (!snips.isEmpty()) {
+ writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionPyOverride,
+ TypeSystem::ShellCode, func, false, lastArg);
+ }
+
+ writeVirtualMethodPythonOverride(s, func, snips, returnStatement);
+}
+
+void CppGenerator::writeVirtualMethodPythonOverride(TextStream &s,
+ const AbstractMetaFunctionCPtr &func,
+ const CodeSnipList &snips,
+ const VirtualMethodReturn &returnStatement) const
+{
writeConversionRule(s, func, TypeSystem::TargetLangCode, false);
bool invalidateReturn = false;
@@ -1305,9 +1264,34 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
std::sort(invalidateArgs.begin(), invalidateArgs.end());
auto arguments = func->arguments();
- auto removedEnd = std::remove_if(arguments.begin(), arguments.end(), isArgumentRemoved);
+ auto removedEnd = std::stable_partition(arguments.begin(), arguments.end(),
+ isArgumentNotRemoved);
+ if (func->isAbstract()) { // Base function is not called, indicate unused arguments.
+ for (auto it = removedEnd; it != arguments.end(); ++it)
+ s << sbkUnusedVariableCast(it->name());
+ }
arguments.erase(removedEnd, arguments.end());
+
+ // FIXME PYSIDE-7: new functions PyObject_Vectorcall() (since 3.9) and
+ // PyObject_CallNoArgs() (since 3.9, stable API since 3.10) might have
+ // become part of the stable API?
+
+ // Code snips might expect the args tuple, don't generate new code
+ const bool generateNewCall = snips.isEmpty();
+ const qsizetype argCount = arguments.size();
+ const char *newCallCondition = argCount == 0 ? noArgsCallCondition : vectorCallCondition;
+ if (generateNewCall) {
+ if (argCount > 0) {
+ s << newCallCondition;
+ writeVirtualMethodNativeVectorCallArgs(s, func, arguments, invalidateArgs);
+ s << "#else\n";
+ } else {
+ s << inverseNoArgsCallCondition;
+ }
+ }
writeVirtualMethodNativeArgs(s, func, arguments, invalidateArgs);
+ if (generateNewCall)
+ s << "#endif\n";
s << '\n';
if (!snips.isEmpty()) {
@@ -1323,23 +1307,42 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
qsizetype returnIndirections = 0;
if (!func->injectedCodeCallsPythonOverride()) {
+ if (generateNewCall) {
+ s << newCallCondition << "Shiboken::AutoDecRef " << PYTHON_RETURN_VAR << '(';
+ if (argCount > 0) {
+ s << "PyObject_Vectorcall(" << PYTHON_OVERRIDE_VAR << ", "
+ << PYTHON_ARGS_ARRAY << ", " << argCount << ", nullptr));\n";
+ for (int argIndex : std::as_const(invalidateArgs)) {
+ s << "if (invalidateArg" << argIndex << ")\n" << indent
+ << "Shiboken::Object::invalidate(" << PYTHON_ARGS_ARRAY
+ << '[' << (argIndex - 1) << "]);\n" << outdent;
+ }
+ for (qsizetype i = 0, size = arguments.size(); i < size; ++i)
+ s << "Py_DECREF(" << PYTHON_ARGS_ARRAY << '[' << i << "]);\n";
+ } else {
+ s << "PyObject_CallNoArgs(" << PYTHON_OVERRIDE_VAR << "));\n";
+ }
+ s << "#else\n";
+ }
s << "Shiboken::AutoDecRef " << PYTHON_RETURN_VAR << "(PyObject_Call("
<< PYTHON_OVERRIDE_VAR << ", " << PYTHON_ARGS << ", nullptr));\n";
- for (int argIndex : qAsConst(invalidateArgs)) {
+ for (int argIndex : std::as_const(invalidateArgs)) {
s << "if (invalidateArg" << argIndex << ")\n" << indent
<< "Shiboken::Object::invalidate(PyTuple_GET_ITEM(" << PYTHON_ARGS
<< ", " << (argIndex - 1) << "));\n" << outdent;
}
+ if (generateNewCall)
+ s << "#endif\n";
s << "if (" << PYTHON_RETURN_VAR << ".isNull()) {\n" << indent
<< "// An error happened in python code!\n"
- << "PyErr_Print();\n"
- << returnStatement << "\n" << outdent
+ << "Shiboken::Errors::storeErrorOrPrint();\n"
+ << returnStatement.statement << "\n" << outdent
<< "}\n";
if (invalidateReturn) {
- s << "bool invalidateArg0 = " << PYTHON_RETURN_VAR << "->ob_refcnt == 1;\n"
+ s << "bool invalidateArg0 = Py_REFCNT(" << PYTHON_RETURN_VAR << ") == 1;\n"
<< "if (invalidateArg0)\n" << indent
<< "Shiboken::Object::releaseOwnership(" << PYTHON_RETURN_VAR
<< ".object());\n" << outdent;
@@ -1347,7 +1350,7 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
if (!func->isVoid()) {
- if (func->modifiedTypeName() != cPyObjectT()) {
+ if (func->modifiedTypeName() != cPyObjectT) {
s << "// Check return type\n";
@@ -1359,10 +1362,10 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
<< PYTHON_RETURN_VAR << ");\n" << outdent
<< "if (!" << PYTHON_TO_CPP_VAR << ") {\n" << indent
<< "Shiboken::Warnings::warnInvalidReturnValue(\""
- << func->ownerClass()->name() << "\", \"" << funcName << "\", "
+ << func->ownerClass()->name() << "\", funcName, "
<< getVirtualFunctionReturnTypeName(func) << ", "
<< "Py_TYPE(" << PYTHON_RETURN_VAR << ")->tp_name);\n"
- << returnStatement << '\n' << outdent
+ << returnStatement.statement << '\n' << outdent
<< "}\n";
} else {
@@ -1381,10 +1384,10 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
s << " && " << PYTHON_RETURN_VAR << " != Py_None";
s << ") {\n" << indent
<< "Shiboken::Warnings::warnInvalidReturnValue(\""
- << func->ownerClass()->name() << "\", \"" << funcName << "\", "
+ << func->ownerClass()->name() << "\", funcName, "
<< getVirtualFunctionReturnTypeName(func) << ", "
<< "Py_TYPE(" << PYTHON_RETURN_VAR << ")->tp_name);\n"
- << returnStatement << '\n' << outdent
+ << returnStatement.statement << '\n' << outdent
<< "}\n";
}
@@ -1421,13 +1424,14 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
if (!func->isVoid()) {
s << "return ";
+ TypeEntryCPtr retType = func->type().typeEntry();
if (avoidProtectedHack() && retType->isEnum()) {
auto metaEnum = api().findAbstractMetaEnum(retType);
bool isProtectedEnum = metaEnum.has_value() && metaEnum->isProtected();
if (isProtectedEnum) {
QString typeCast;
if (metaEnum->enclosingClass())
- typeCast += u"::"_s + metaEnum->enclosingClass()->qualifiedCppName();
+ typeCast += getFullTypeName(metaEnum->enclosingClass());
typeCast += u"::"_s + metaEnum->name();
s << '(' << typeCast << ')';
}
@@ -1441,6 +1445,28 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
s << outdent << "}\n\n";
}
+void CppGenerator::writeUserAddedPythonOverride(TextStream &s,
+ const AbstractMetaFunctionCPtr &func) const
+{
+ TypeEntryCPtr retType = func->type().typeEntry();
+ const QString funcName = func->isOperatorOverload()
+ ? pythonOperatorFunctionName(func) : func->definitionNames().constFirst();
+
+ const CodeSnipList snips = func->hasInjectedCode()
+ ? func->injectedCodeSnips() : CodeSnipList();
+
+ QString prefix = wrapperName(func->ownerClass()) + u"::"_s;
+ s << '\n' << functionSignature(func, prefix, QString(), Generator::SkipDefaultValues |
+ Generator::OriginalTypeDescription)
+ << "\n{\n" << indent << sbkUnusedVariableCast("gil");
+
+ writeFuncNameVar(s, func, funcName);
+
+ const auto returnStatement = virtualMethodReturn(api(), func,
+ func->modifications());
+ writeVirtualMethodPythonOverride(s, func, snips, returnStatement);
+}
+
void CppGenerator::writeMetaObjectMethod(TextStream &s,
const GeneratorContext &classContext) const
{
@@ -1448,7 +1474,7 @@ void CppGenerator::writeMetaObjectMethod(TextStream &s,
const QString wrapperClassName = classContext.wrapperName();
const QString qualifiedCppName = classContext.metaClass()->qualifiedCppName();
s << "const QMetaObject *" << wrapperClassName << "::metaObject() const\n{\n";
- s << indent << "if (QObject::d_ptr->metaObject)\n"
+ s << indent << "if (QObject::d_ptr->metaObject != nullptr)\n"
<< indent << "return QObject::d_ptr->dynamicMetaObject();\n" << outdent
<< "SbkObject *pySelf = Shiboken::BindingManager::instance().retrieveWrapper(this);\n"
<< "if (pySelf == nullptr)\n"
@@ -1491,42 +1517,65 @@ void CppGenerator::writeMetaCast(TextStream &s,
const QString wrapperClassName = classContext.wrapperName();
const QString qualifiedCppName = classContext.metaClass()->qualifiedCppName();
s << "void *" << wrapperClassName << "::qt_metacast(const char *_clname)\n{\n"
- << indent << "if (!_clname)\n" << indent << "return {};\n" << outdent
+ << indent << "if (_clname == nullptr)\n" << indent << "return {};\n" << outdent
<< "SbkObject *pySelf = Shiboken::BindingManager::instance().retrieveWrapper(this);\n"
- << "if (pySelf && PySide::inherits(Py_TYPE(pySelf), _clname))\n"
+ << "if (pySelf != nullptr && PySide::inherits(Py_TYPE(pySelf), _clname))\n"
<< indent << "return static_cast<void *>(const_cast< "
<< wrapperClassName << " *>(this));\n" << outdent
<< "return " << qualifiedCppName << "::qt_metacast(_clname);\n"
<< outdent << "}\n\n";
}
-void CppGenerator::writeEnumConverterFunctions(TextStream &s, const AbstractMetaEnum &metaEnum) const
+static void generateDeprecatedValueWarnings(TextStream &c,
+ const AbstractMetaEnum &metaEnum,
+ bool useSurrogateName)
{
- if (metaEnum.isPrivate() || metaEnum.isAnonymous())
- return;
- writeEnumConverterFunctions(s, metaEnum.typeEntry());
+ EnumTypeEntryCPtr enumType = metaEnum.typeEntry();
+ const QString prefix = enumType->qualifiedCppName() + u"::"_s;
+ c << "switch (value) {\n";
+ const auto &deprecatedValues = metaEnum.deprecatedValues();
+ for (const auto &v : deprecatedValues) {
+ c << "case ";
+ if (useSurrogateName)
+ c << v.value().toString(); // Protected, use int representation
+ else
+ c << prefix << v.name();
+ c << ":\n" << indent
+ << "Shiboken::Warnings::warnDeprecatedEnumValue(\"" << enumType->name()
+ << "\", \"" << v.name() << "\");\nbreak;\n" << outdent;
+ }
+ if (deprecatedValues.size() < metaEnum.values().size())
+ c << "default:\n" << indent << "break;\n" << outdent;
+ c << "}\n";
}
-void CppGenerator::writeEnumConverterFunctions(TextStream &s, const TypeEntry *enumType) const
+void CppGenerator::writeEnumConverterFunctions(TextStream &s, const AbstractMetaEnum &metaEnum) const
{
- if (!enumType)
+ if (metaEnum.isPrivate() || metaEnum.isAnonymous())
return;
+ EnumTypeEntryCPtr enumType = metaEnum.typeEntry();
+ Q_ASSERT(enumType);
QString typeName = fixedCppTypeName(enumType);
QString enumPythonType = cpythonTypeNameExt(enumType);
- QString cppTypeName = getFullTypeName(enumType).trimmed();
- if (avoidProtectedHack()) {
- auto metaEnum = api().findAbstractMetaEnum(enumType);
- if (metaEnum.has_value() && metaEnum->isProtected())
- cppTypeName = protectedEnumSurrogateName(metaEnum.value());
- }
+ const bool useSurrogateName = avoidProtectedHack() && metaEnum.isProtected();
+ QString cppTypeName = useSurrogateName
+ ? protectedEnumSurrogateName(metaEnum) : getFullTypeName(enumType).trimmed();
+
StringStream c(TextStream::Language::Cpp);
- c << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n"
- << " ";
- if (enumType->isFlags())
- c << cppTypeName << "(QFlag(int(PySide::QFlags::getValue(reinterpret_cast<PySideQFlagsObject *>(pyIn)))))";
- else
- c << "static_cast<" << cppTypeName << ">(Shiboken::Enum::getValue(pyIn))";
- c << ";\n";
+ if (metaEnum.isDeprecated())
+ c << "Shiboken::Warnings::warnDeprecatedEnum(\"" << enumType->name() << "\");\n";
+
+ c << "const auto value = static_cast<" << cppTypeName
+ << ">(Shiboken::Enum::getValue(pyIn));\n";
+
+ // Warn about deprecated values unless it is protected+scoped (inaccessible values)
+ const bool valuesAcccessible = !useSurrogateName || metaEnum.enumKind() != EnumClass;
+ if (valuesAcccessible && metaEnum.hasDeprecatedValues())
+ generateDeprecatedValueWarnings(c, metaEnum, useSurrogateName);
+
+ c << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) = value;\n";
+
+ ConfigurableScope configScope(s, enumType);
writePythonToCppFunction(s, c.toString(), typeName, typeName);
QString pyTypeCheck = u"PyObject_TypeCheck(pyIn, "_s + enumPythonType + u')';
@@ -1535,65 +1584,44 @@ void CppGenerator::writeEnumConverterFunctions(TextStream &s, const TypeEntry *e
c.clear();
c << "const int castCppIn = int(*reinterpret_cast<const "
- << cppTypeName << " *>(cppIn));\n" << "return ";
- if (enumType->isFlags()) {
- c << "reinterpret_cast<PyObject *>(PySide::QFlags::newObject(castCppIn, "
- << enumPythonType << "))";
- } else {
- c << "Shiboken::Enum::newItem(" << enumPythonType << ", castCppIn)";
- }
- c << ";\n";
+ << cppTypeName << " *>(cppIn));\n" << "return "
+ << "Shiboken::Enum::newItem(" << enumPythonType << ", castCppIn);\n";
writeCppToPythonFunction(s, c.toString(), typeName, typeName);
s << '\n';
+}
- if (enumType->isFlags())
- return;
-
- auto flags = reinterpret_cast<const EnumTypeEntry *>(enumType)->flags();
- if (!flags)
- return;
-
- // QFlags part.
-
- writeEnumConverterFunctions(s, flags);
-
- c.clear();
- cppTypeName = getFullTypeName(flags).trimmed();
- c << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n"
- << " " << cppTypeName
- << "(QFlag(int(Shiboken::Enum::getValue(pyIn))));\n";
+static void writePointerToPythonConverter(TextStream &c,
+ const AbstractMetaClassCPtr &metaClass,
+ const QString &typeName,
+ const QString &cpythonType)
+{
+ c << "auto *pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppIn));\n"
+ << "if (pyOut) {\n" << indent
+ << "Py_INCREF(pyOut);\nreturn pyOut;\n" << outdent
+ << "}\n"
+ << "auto *tCppIn = reinterpret_cast<const " << typeName << R"( *>(cppIn);
+const char *typeName = )";
- QString flagsTypeName = fixedCppTypeName(flags);
- writePythonToCppFunction(s, c.toString(), typeName, flagsTypeName);
- writeIsPythonConvertibleToCppFunction(s, typeName, flagsTypeName, pyTypeCheck);
+ const QString nameFunc = metaClass->typeEntry()->polymorphicNameFunction();
+ if (nameFunc.isEmpty())
+ c << "typeid(*tCppIn).name();\n";
+ else
+ c << nameFunc << "(tCppIn);\n";
+ c << "return Shiboken::Object::newObjectForPointer("
+ << cpythonType << ", const_cast<void *>(cppIn), false, typeName);\n";
+}
- c.clear();
- c << "Shiboken::AutoDecRef pyLong(PyNumber_Long(pyIn));\n"
- << "*reinterpret_cast<" << cppTypeName << " *>(cppOut) =\n"
- << " " << cppTypeName
- << "(QFlag(int(PyLong_AsLong(pyLong.object()))));\n";
- // PYSIDE-898: Include an additional condition to detect if the type of the
- // enum corresponds to the object that is being evaluated.
- // Using only `PyNumber_Check(...)` is too permissive,
- // then we would have been unable to detect the difference between
- // a PolarOrientation and Qt::AlignmentFlag, which was the main
- // issue of the bug.
- const QString numberCondition = QStringLiteral("PyNumber_Check(pyIn) && ") + pyTypeCheck;
- writePythonToCppFunction(s, c.toString(), u"number"_s, flagsTypeName);
- writeIsPythonConvertibleToCppFunction(s, u"number"_s, flagsTypeName, numberCondition);
-}
-
-void CppGenerator::writeConverterFunctions(TextStream &s, const AbstractMetaClass *metaClass,
+void CppGenerator::writeConverterFunctions(TextStream &s, const AbstractMetaClassCPtr &metaClass,
const GeneratorContext &classContext) const
{
s << "// Type conversion functions.\n\n";
AbstractMetaEnumList classEnums = metaClass->enums();
- auto *typeEntry = metaClass->typeEntry();
+ auto typeEntry = metaClass->typeEntry();
metaClass->getEnumsFromInvisibleNamespacesToBeGenerated(&classEnums);
if (!classEnums.isEmpty())
s << "// Python to C++ enum conversion.\n";
- for (const AbstractMetaEnum &metaEnum : qAsConst(classEnums))
+ for (const AbstractMetaEnum &metaEnum : std::as_const(classEnums))
writeEnumConverterFunctions(s, metaEnum);
if (metaClass->isNamespace())
@@ -1624,37 +1652,11 @@ void CppGenerator::writeConverterFunctions(TextStream &s, const AbstractMetaClas
// C++ pointer to a Python wrapper, keeping identity.
s << "// C++ to Python pointer conversion - tries to find the Python wrapper for the C++ object (keeps object identity).\n";
c.clear();
- if (usePySideExtensions() && metaClass->isQObject()) {
+ if (usePySideExtensions() && isQObject(metaClass)) {
c << "return PySide::getWrapperForQObject(reinterpret_cast<"
<< typeName << " *>(const_cast<void *>(cppIn)), " << cpythonType << ");\n";
} else {
- c << "auto pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper(cppIn));\n"
- << "if (pyOut) {\n";
- {
- Indentation indent(c);
- c << "Py_INCREF(pyOut);\nreturn pyOut;\n";
- }
- c << "}\n"
- << "bool changedTypeName = false;\n"
- << "auto tCppIn = reinterpret_cast<const " << typeName << R"( *>(cppIn);
-const char *typeName = )";
-
- const QString nameFunc = metaClass->typeEntry()->polymorphicNameFunction();
- if (nameFunc.isEmpty())
- c << "typeid(*tCppIn).name();\n";
- else
- c << nameFunc << "(tCppIn);\n";
- c << R"(auto sbkType = Shiboken::ObjectType::typeForTypeName(typeName);
-if (sbkType && Shiboken::ObjectType::hasSpecialCastFunction(sbkType)) {
- typeName = typeNameOf(tCppIn);
- changedTypeName = true;
-}
-)"
- << "PyObject *result = Shiboken::Object::newObject(" << cpythonType
- << R"(, const_cast<void *>(cppIn), false, /* exactType */ changedTypeName, typeName);
-if (changedTypeName)
- delete [] typeName;
-return result;)";
+ writePointerToPythonConverter(c, metaClass, typeName, cpythonType);
}
std::swap(targetTypeName, sourceTypeName);
writeCppToPythonFunction(s, c.toString(), sourceTypeName, targetTypeName);
@@ -1683,7 +1685,7 @@ return result;)";
c << "auto *source = reinterpret_cast<const " << typeName << " *>(cppIn);\n";
}
c << "return Shiboken::Object::newObject(" << cpythonType
- << ", new ::" << classContext.effectiveClassName() << '('
+ << ", new " << globalScopePrefix(classContext) << classContext.effectiveClassName() << '('
<< (isUniquePointer ? "std::move(*source)" : "*source")
<< "), true, true);";
writeCppToPythonFunction(s, c.toString(), sourceTypeName, targetTypeName);
@@ -1693,7 +1695,7 @@ return result;)";
s << "// Python to C++ copy conversion.\n";
sourceTypeName = metaClass->name();
- targetTypeName = sourceTypeName + QStringLiteral("_COPY");
+ targetTypeName = sourceTypeName + "_COPY"_L1;
c.clear();
QString pyInVariable = u"pyIn"_s;
@@ -1702,7 +1704,7 @@ return result;)";
c << '*' << outPtr << " = *"
<< cpythonWrapperCPtr(typeEntry, pyInVariable) << ';';
} else {
- auto *ste = static_cast<const SmartPointerTypeEntry *>(typeEntry);
+ auto ste = std::static_pointer_cast<const SmartPointerTypeEntry>(typeEntry);
const QString resetMethod = ste->resetMethod();
c << "auto *ptr = " << outPtr << ";\n";
c << "if (" << pyInVariable << " == Py_None)\n" << indent;
@@ -1732,7 +1734,7 @@ return result;)";
s << "// Implicit conversions.\n";
AbstractMetaType targetType = AbstractMetaType::fromAbstractMetaClass(metaClass);
- for (const auto &conv : qAsConst(implicitConvs)) {
+ for (const auto &conv : std::as_const(implicitConvs)) {
if (conv->isModifiedRemoved())
continue;
@@ -1740,7 +1742,7 @@ return result;)";
QString toCppConv;
QString toCppPreConv;
if (conv->isConversionOperator()) {
- const AbstractMetaClass *sourceClass = conv->ownerClass();
+ const auto sourceClass = conv->ownerClass();
typeCheck = u"PyObject_TypeCheck(pyIn, "_s
+ cpythonTypeNameExt(sourceClass->typeEntry()) + u')';
toCppConv = u'*' + cpythonWrapperCPtr(sourceClass->typeEntry(),
@@ -1774,14 +1776,14 @@ return result;)";
StringStream pc(TextStream::Language::Cpp);
pc << getFullTypeNameWithoutModifiers(sourceType) << " cppIn"
<< minimalConstructorExpression(api(), sourceType) << ";\n";
- writeToCppConversion(pc, sourceType, nullptr, pyInVariable,
+ writeToCppConversion(pc, sourceType, pyInVariable,
u"cppIn"_s);
pc << ';';
toCppPreConv = pc.toString();
toCppConv.append(u"cppIn"_s);
} else if (!sourceType.isWrapperType()) {
StringStream tcc(TextStream::Language::Cpp);
- writeToCppConversion(tcc, sourceType, metaClass, pyInVariable,
+ writeToCppConversion(tcc, sourceType, pyInVariable,
u"/*BOZO-1061*/"_s);
toCppConv = tcc.toString();
}
@@ -1792,54 +1794,53 @@ return result;)";
writePythonToCppConversionFunctions(s, sourceType, targetType, typeCheck, toCppConv, toCppPreConv);
}
- writeCustomConverterFunctions(s, typeEntry->customConversion());
+ if (typeEntry->isValue()) {
+ auto vte = std::static_pointer_cast<const ValueTypeEntry>(typeEntry);
+ writeCustomConverterFunctions(s, vte->customConversion());
+ }
}
void CppGenerator::writeCustomConverterFunctions(TextStream &s,
- const CustomConversion *customConversion) const
+ const CustomConversionPtr &customConversion) const
{
if (!customConversion)
return;
- const CustomConversion::TargetToNativeConversions &toCppConversions = customConversion->targetToNativeConversions();
+ const TargetToNativeConversions &toCppConversions = customConversion->targetToNativeConversions();
if (toCppConversions.isEmpty())
return;
- s << "// Python to C++ conversions for type '" << customConversion->ownerType()->qualifiedCppName() << "'.\n";
- for (CustomConversion::TargetToNativeConversion *toNative : toCppConversions)
- writePythonToCppConversionFunctions(s, toNative, customConversion->ownerType());
+ auto ownerType = customConversion->ownerType();
+ s << "// Python to C++ conversions for type '" << ownerType->qualifiedCppName() << "'.\n";
+ for (const auto &toNative : toCppConversions)
+ writePythonToCppConversionFunctions(s, toNative, ownerType);
s << '\n';
}
-void CppGenerator::writeConverterRegister(TextStream &s, const AbstractMetaClass *metaClass,
+void CppGenerator::writeConverterRegister(TextStream &s, const AbstractMetaClassCPtr &metaClass,
const GeneratorContext &classContext) const
{
- const auto *typeEntry = metaClass->typeEntry();
+ const auto typeEntry = metaClass->typeEntry();
if (typeEntry->isNamespace())
return;
s << "// Register Converter\n"
- << "SbkConverter *converter = Shiboken::Conversions::createConverter(pyType,\n";
- {
- Indentation indent(s);
- QString sourceTypeName = metaClass->name();
- QString targetTypeName = sourceTypeName + u"_PTR"_s;
- s << pythonToCppFunctionName(sourceTypeName, targetTypeName) << ',' << '\n'
- << convertibleToCppFunctionName(sourceTypeName, targetTypeName) << ',' << '\n';
- std::swap(targetTypeName, sourceTypeName);
+ << "SbkConverter *converter = Shiboken::Conversions::createConverter(pyType,\n"
+ << indent;
+ QString sourceTypeName = metaClass->name();
+ QString targetTypeName = sourceTypeName + u"_PTR"_s;
+ s << pythonToCppFunctionName(sourceTypeName, targetTypeName) << ',' << '\n'
+ << convertibleToCppFunctionName(sourceTypeName, targetTypeName) << ',' << '\n';
+ std::swap(targetTypeName, sourceTypeName);
+ s << cppToPythonFunctionName(sourceTypeName, targetTypeName);
+ if (typeEntry->isValue() || typeEntry->isSmartPointer()) {
+ s << ',' << '\n';
+ sourceTypeName = metaClass->name() + u"_COPY"_s;
s << cppToPythonFunctionName(sourceTypeName, targetTypeName);
- if (typeEntry->isValue() || typeEntry->isSmartPointer()) {
- s << ',' << '\n';
- sourceTypeName = metaClass->name() + u"_COPY"_s;
- s << cppToPythonFunctionName(sourceTypeName, targetTypeName);
- }
}
- s << ");\n";
-
- s << '\n';
+ s << outdent << ");\n\n";
auto writeConversions = [&s](const QString &signature)
{
- s << "Shiboken::Conversions::registerConverterName(converter, \"" << signature << "\");\n"
- << "Shiboken::Conversions::registerConverterName(converter, \"" << signature << "*\");\n"
- << "Shiboken::Conversions::registerConverterName(converter, \"" << signature << "&\");\n";
+ s << registerConverterName(signature) << registerConverterName(signature + u'*')
+ << registerConverterName(signature + u'&');
};
auto writeConversionsForType = [writeConversions](const QString &fullTypeName)
@@ -1864,14 +1865,14 @@ void CppGenerator::writeConverterRegister(TextStream &s, const AbstractMetaClass
Qt::SkipEmptyParts);
while (!lst.isEmpty()) {
QString signature = lst.join(u"::"_s);
- writeConversions(smartPointerName + u'<' + signature + u" >"_s);
+ writeConversions(smartPointerName + u'<' + signature + u'>');
lst.removeFirst();
}
writeConversionsForType(smartPointerType);
}
- s << "Shiboken::Conversions::registerConverterName(converter, typeid(::";
+ s << "Shiboken::Conversions::registerConverterName(converter, typeid(" << m_gsp;
QString qualifiedCppNameInvocation;
if (!classContext.forSmartPointer())
qualifiedCppNameInvocation = metaClass->qualifiedCppName();
@@ -1881,19 +1882,17 @@ void CppGenerator::writeConverterRegister(TextStream &s, const AbstractMetaClass
s << qualifiedCppNameInvocation << ").name());\n";
if (classContext.useWrapper()) {
- s << "Shiboken::Conversions::registerConverterName(converter, typeid(::"
+ s << "Shiboken::Conversions::registerConverterName(converter, typeid("
<< classContext.wrapperName() << ").name());\n";
}
- s << '\n';
-
if (!typeEntry->isValue() && !typeEntry->isSmartPointer())
return;
// Python to C++ copy (value, not pointer neither reference) conversion.
- s << "// Add Python to C++ copy (value, not pointer neither reference) conversion to type converter.\n";
- QString sourceTypeName = metaClass->name();
- QString targetTypeName = sourceTypeName + u"_COPY"_s;
+ s << "\n// Add Python to C++ copy (value, not pointer neither reference) conversion to type converter.\n";
+ sourceTypeName = metaClass->name();
+ targetTypeName = sourceTypeName + u"_COPY"_s;
QString toCpp = pythonToCppFunctionName(sourceTypeName, targetTypeName);
QString isConv = convertibleToCppFunctionName(sourceTypeName, targetTypeName);
writeAddPythonToCppConversion(s, u"converter"_s, toCpp, isConv);
@@ -1907,7 +1906,7 @@ void CppGenerator::writeConverterRegister(TextStream &s, const AbstractMetaClass
s << "// Add implicit conversions to type converter.\n";
AbstractMetaType targetType = AbstractMetaType::fromAbstractMetaClass(metaClass);
- for (const auto &conv : qAsConst(implicitConvs)) {
+ for (const auto &conv : std::as_const(implicitConvs)) {
if (conv->isModifiedRemoved())
continue;
AbstractMetaType sourceType;
@@ -1925,19 +1924,24 @@ void CppGenerator::writeConverterRegister(TextStream &s, const AbstractMetaClass
writeAddPythonToCppConversion(s, u"converter"_s, toCpp, isConv);
}
- writeCustomConverterRegister(s, typeEntry->customConversion(), u"converter"_s);
+ if (typeEntry->isValue()) {
+ auto vte = std::static_pointer_cast<const ValueTypeEntry>(typeEntry);
+ writeCustomConverterRegister(s, vte->customConversion(), u"converter"_s);
+ }
}
-void CppGenerator::writeCustomConverterRegister(TextStream &s, const CustomConversion *customConversion,
+void CppGenerator::writeCustomConverterRegister(TextStream &s,
+ const CustomConversionPtr &customConversion,
const QString &converterVar)
{
if (!customConversion)
return;
- const CustomConversion::TargetToNativeConversions &toCppConversions = customConversion->targetToNativeConversions();
+ const TargetToNativeConversions &toCppConversions =
+ customConversion->targetToNativeConversions();
if (toCppConversions.isEmpty())
return;
s << "// Add user defined implicit conversions to type converter.\n";
- for (CustomConversion::TargetToNativeConversion *toNative : toCppConversions) {
+ for (const auto &toNative : toCppConversions) {
QString toCpp = pythonToCppFunctionName(toNative, customConversion->ownerType());
QString isConv = convertibleToCppFunctionName(toNative, customConversion->ownerType());
writeAddPythonToCppConversion(s, converterVar, toCpp, isConv);
@@ -1951,46 +1955,7 @@ void CppGenerator::writeContainerConverterFunctions(TextStream &s,
writePythonToCppConversionFunctions(s, containerType);
}
-// Helpers to collect all smart pointer pointee base classes
-static AbstractMetaClassCList findSmartPointeeBaseClasses(const ApiExtractorResult &api,
- const AbstractMetaType &smartPointerType)
-{
- AbstractMetaClassCList result;
- auto *instantiationsTe = smartPointerType.instantiations().at(0).typeEntry();
- auto targetClass = AbstractMetaClass::findClass(api.classes(), instantiationsTe);
- if (targetClass != nullptr)
- result = targetClass->allTypeSystemAncestors();
- return result;
-}
-
-void CppGenerator::writeSmartPointerConverterFunctions(TextStream &s,
- const AbstractMetaType &smartPointerType) const
-{
- const auto baseClasses = findSmartPointeeBaseClasses(api(), smartPointerType);
- if (baseClasses.isEmpty())
- return;
-
- auto *smartPointerTypeEntry =
- static_cast<const SmartPointerTypeEntry *>(smartPointerType.typeEntry());
-
- // TODO: Missing conversion to smart pointer pointer type:
-
- s << "// Register smartpointer conversion for all derived classes\n";
- for (auto *base : baseClasses) {
- auto *baseTe = base->typeEntry();
- if (smartPointerTypeEntry->matchesInstantiation(baseTe)) {
- if (auto opt = findSmartPointerInstantiation(smartPointerTypeEntry, baseTe)) {
- const auto smartTargetType = opt.value();
- s << "// SmartPointer derived class: "
- << smartTargetType.cppSignature() << "\n";
- writePythonToCppConversionFunctions(s, smartPointerType,
- smartTargetType, {}, {}, {});
- }
- }
- }
-}
-
-bool CppGenerator::needsArgumentErrorHandling(const OverloadData &overloadData) const
+bool CppGenerator::needsArgumentErrorHandling(const OverloadData &overloadData)
{
if (overloadData.maxArgs() > 0)
return true;
@@ -1999,15 +1964,16 @@ bool CppGenerator::needsArgumentErrorHandling(const OverloadData &overloadData)
return false;
auto rfunc = overloadData.referenceFunction();
return rfunc->functionType() == AbstractMetaFunction::ConstructorFunction
- && rfunc->ownerClass()->isQObject();
+ && isQObject(rfunc->ownerClass());
}
-void CppGenerator::writeMethodWrapperPreamble(TextStream &s,const OverloadData &overloadData,
+void CppGenerator::writeMethodWrapperPreamble(TextStream &s,
+ const OverloadData &overloadData,
const GeneratorContext &context,
- ErrorReturn errorReturn) const
+ ErrorReturn errorReturn)
{
const auto rfunc = overloadData.referenceFunction();
- const AbstractMetaClass *ownerClass = rfunc->targetLangOwner();
+ const auto ownerClass = rfunc->targetLangOwner();
Q_ASSERT(ownerClass == context.metaClass());
int minArgs = overloadData.minArgs();
int maxArgs = overloadData.maxArgs();
@@ -2017,7 +1983,9 @@ void CppGenerator::writeMethodWrapperPreamble(TextStream &s,const OverloadData &
if (rfunc->isConstructor()) {
// Check if the right constructor was called.
if (!ownerClass->hasPrivateDestructor()) {
- s << "if (Shiboken::Object::isUserType(self) && !Shiboken::ObjectType::canCallConstructor(self->ob_type, Shiboken::SbkType< ::";
+ s << "if (Shiboken::Object::isUserType(self) && "
+ << "!Shiboken::ObjectType::canCallConstructor(self->ob_type, Shiboken::SbkType< "
+ << m_gsp;
QString qualifiedCppName;
if (!context.forSmartPointer())
qualifiedCppName = ownerClass->qualifiedCppName();
@@ -2027,7 +1995,7 @@ void CppGenerator::writeMethodWrapperPreamble(TextStream &s,const OverloadData &
s << qualifiedCppName << " >()))\n" << indent << errorReturn << outdent << '\n';
}
// Declare pointer for the underlying C++ object.
- s << "::" << context.effectiveClassName() << " *cptr{};\n";
+ s << globalScopePrefix(context) << context.effectiveClassName() << " *cptr{};\n";
initPythonArguments = maxArgs > 0;
@@ -2047,18 +2015,24 @@ void CppGenerator::writeMethodWrapperPreamble(TextStream &s,const OverloadData &
initPythonArguments = minArgs != maxArgs || maxArgs > 1;
}
- if (needsArgumentErrorHandling(overloadData)) {
- s << R"(Shiboken::AutoDecRef errInfo{};
-static const char fullName[] = ")" << fullPythonFunctionName(rfunc, true)
- << "\";\nSBK_UNUSED(fullName)\n";
- }
+ if (needsArgumentErrorHandling(overloadData))
+ s << "Shiboken::AutoDecRef errInfo{};\n";
+
+ s << "static const char fullName[] = \"" << fullPythonFunctionName(rfunc, true)
+ << "\";\nSBK_UNUSED(fullName)\n"
+ << "Shiboken::PythonContextMarker pcm;\n";
+ // PYSIDE-2335: Mark blocking calls like `exec` or `run` as such.
+ bool isBlockingFunction = rfunc->name() == u"exec"_s || rfunc->name() == u"exec_"_s
+ || rfunc->name() == u"run"_s;
+ if (isBlockingFunction)
+ s << "pcm.setBlocking();\n";
+
if (maxArgs > 0) {
s << "int overloadId = -1;\n"
<< PYTHON_TO_CPPCONVERSION_STRUCT << ' ' << PYTHON_TO_CPP_VAR;
if (overloadData.pythonFunctionWrapperUsesListOfArguments())
s << '[' << maxArgs << ']';
- s << ";\n";
- writeUnusedVariableCast(s, PYTHON_TO_CPP_VAR);
+ s << ";\n" << sbkUnusedVariableCast(PYTHON_TO_CPP_VAR);
}
if (initPythonArguments) {
@@ -2078,17 +2052,20 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const OverloadData &ov
const ErrorReturn errorReturn = ErrorReturn::MinusOne;
const auto rfunc = overloadData.referenceFunction();
- const AbstractMetaClass *metaClass = rfunc->ownerClass();
+ const auto metaClass = rfunc->ownerClass();
s << "static int\n";
s << cpythonFunctionName(rfunc)
<< "(PyObject *self, PyObject *args, PyObject *kwds)\n{\n" << indent;
+ if (overloadData.maxArgs() == 0 || metaClass->isAbstract())
+ s << sbkUnusedVariableCast("args");
+ s << sbkUnusedVariableCast("kwds");
- const bool needsMetaObject = usePySideExtensions() && metaClass->isQObject();
+ const bool needsMetaObject = usePySideExtensions() && isQObject(metaClass);
if (needsMetaObject)
s << "const QMetaObject *metaObject;\n";
- s << "SbkObject *sbkSelf = reinterpret_cast<SbkObject *>(self);\n";
+ s << "auto *sbkSelf = reinterpret_cast<SbkObject *>(self);\n";
if (metaClass->isAbstract() || metaClass->baseClassNames().size() > 1) {
s << "PyTypeObject *type = self->ob_type;\n"
@@ -2099,11 +2076,11 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const OverloadData &ov
if (metaClass->isAbstract()) {
// C++ Wrapper disabled: Abstract C++ class cannot be instantiated.
if (metaClass->typeEntry()->typeFlags().testFlag(ComplexTypeEntry::DisableWrapper)) {
- writeUnusedVariableCast(s, QStringLiteral("sbkSelf"));
- writeUnusedVariableCast(s, QStringLiteral("type"));
- writeUnusedVariableCast(s, QStringLiteral("myType"));
+ s << sbkUnusedVariableCast("sbkSelf")
+ << sbkUnusedVariableCast("type")
+ << sbkUnusedVariableCast("myType");
if (needsMetaObject)
- writeUnusedVariableCast(s, QStringLiteral("metaObject"));
+ s << sbkUnusedVariableCast("metaObject");
s << "Shiboken::Errors::setInstantiateAbstractClassDisabledWrapper(\""
<< metaClass->qualifiedCppName() << "\");\n" << errorReturn << outdent
<< "}\n\n";
@@ -2135,19 +2112,29 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const OverloadData &ov
s << '\n';
if (overloadData.maxArgs() > 0)
- writeOverloadedFunctionDecisor(s, overloadData);
+ writeOverloadedFunctionDecisor(s, overloadData, errorReturn);
+
+ // Handles Python Multiple Inheritance
+ QString pre = needsMetaObject ? u"bool usesPyMI = "_s : u""_s;
+ s << "\n// PyMI support\n"
+ << pre << "Shiboken::callInheritedInit(self, args, kwds, fullName);\n"
+ << "if (" << shibokenErrorsOccurred << ")\n"
+ << indent << errorReturn << outdent << "\n";
writeFunctionCalls(s, overloadData, classContext, errorReturn);
s << '\n';
const QString typeName = classContext.forSmartPointer()
? classContext.preciseType().cppSignature() : metaClass->qualifiedCppName();
- s << "if (PyErr_Occurred() || !Shiboken::Object::setCppPointer(sbkSelf, Shiboken::SbkType< ::"
- << typeName << " >(), cptr)) {\n"
+ s << "if (" << shibokenErrorsOccurred
+ << " || !Shiboken::Object::setCppPointer(sbkSelf, Shiboken::SbkType< "
+ << globalScopePrefix(classContext) << typeName << " >(), cptr)) {\n"
<< indent << "delete cptr;\n" << errorReturn << outdent
<< "}\n";
if (overloadData.maxArgs() > 0)
- s << "if (!cptr) goto " << cpythonFunctionName(rfunc) << "_TypeError;\n\n";
+ s << "if (cptr == nullptr)\n" << indent
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n\n"
+ << outdent;
s << "Shiboken::Object::setValidCpp(sbkSelf, true);\n";
// If the created C++ object has a C++ wrapper the ownership is assigned to Python
@@ -2158,13 +2145,10 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const OverloadData &ov
s << "Shiboken::Object::setHasCppWrapper(sbkSelf, true);\n";
// Need to check if a wrapper for same pointer is already registered
// Caused by bug PYSIDE-217, where deleted objects' wrappers are not released
- s << "if (Shiboken::BindingManager::instance().hasWrapper(cptr)) {\n";
- {
- Indentation indent(s);
- s << "Shiboken::BindingManager::instance().releaseWrapper("
- "Shiboken::BindingManager::instance().retrieveWrapper(cptr));\n";
- }
- s << "}\nShiboken::BindingManager::instance().registerWrapper(sbkSelf, cptr);\n";
+ s << "if (Shiboken::BindingManager::instance().hasWrapper(cptr)) {\n" << indent
+ << "Shiboken::BindingManager::instance().releaseWrapper("
+ "Shiboken::BindingManager::instance().retrieveWrapper(cptr));\n" << outdent
+ << "}\nShiboken::BindingManager::instance().registerWrapper(sbkSelf, cptr);\n";
// Create metaObject and register signal/slot
if (needsMetaObject) {
@@ -2172,8 +2156,9 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const OverloadData &ov
<< "PySide::Signal::updateSourceObject(self);\n"
<< "metaObject = cptr->metaObject(); // <- init python qt properties\n"
<< "if (!errInfo.isNull() && PyDict_Check(errInfo.object())) {\n" << indent
- << "if (!PySide::fillQtProperties(self, metaObject, errInfo))\n" << indent
- << "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n" << outdent << outdent
+ << "if (!PySide::fillQtProperties(self, metaObject, errInfo, usesPyMI))\n" << indent
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n"
+ << outdent << outdent
<< "};\n";
}
@@ -2190,31 +2175,27 @@ void CppGenerator::writeConstructorWrapper(TextStream &s, const OverloadData &ov
}
if (hasCodeInjectionsAtEnd) {
// FIXME: C++ arguments are not available in code injection on constructor when position = end.
- s <<"switch (overloadId) {\n";
+ s << "switch (overloadId) {\n";
for (const auto &func : overloadData.overloads()) {
- Indentation indent(s);
+ s << indent;
const CodeSnipList &injectedCodeSnips = func->injectedCodeSnips();
for (const CodeSnip &cs : injectedCodeSnips) {
if (cs.position == TypeSystem::CodeSnipPositionEnd) {
s << "case " << metaClass->functions().indexOf(func) << ':' << '\n'
- << "{\n";
- {
- Indentation indent(s);
- writeCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionEnd,
- TypeSystem::TargetLangCode, func,
- true /* usesPyArgs */, nullptr);
- }
- s << "}\nbreak;\n";
+ << "{\n" << indent;
+ writeCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionEnd,
+ TypeSystem::TargetLangCode, func,
+ true /* usesPyArgs */, nullptr);
+ s << outdent << "}\nbreak;\n";
break;
}
}
+ s << outdent;
}
s << "}\n";
}
s << "\n\nreturn 1;\n";
- if (needsArgumentErrorHandling(overloadData))
- writeErrorSection(s, overloadData, errorReturn);
s<< outdent << "}\n\n";
}
@@ -2227,87 +2208,65 @@ void CppGenerator::writeMethodWrapper(TextStream &s, const OverloadData &overloa
s << "static PyObject *";
s << cpythonFunctionName(rfunc) << "(PyObject *self";
+ bool hasKwdArgs = false;
if (maxArgs > 0) {
s << ", PyObject *"
<< (overloadData.pythonFunctionWrapperUsesListOfArguments() ? u"args"_s : PYTHON_ARG);
- if (overloadData.hasArgumentWithDefaultValue() || rfunc->isCallOperator())
+ hasKwdArgs = overloadData.hasArgumentWithDefaultValue() || rfunc->isCallOperator();
+ if (hasKwdArgs)
s << ", PyObject *kwds";
}
s << ")\n{\n" << indent;
+ if (rfunc->ownerClass() == nullptr || overloadData.hasStaticFunction())
+ s << sbkUnusedVariableCast(PYTHON_SELF_VAR);
+ if (hasKwdArgs)
+ s << sbkUnusedVariableCast("kwds");
writeMethodWrapperPreamble(s, overloadData, classContext);
s << '\n';
- /*
- * This code is intended for shift operations only:
- * Make sure reverse <</>> operators defined in other classes (specially from other modules)
- * are called. A proper and generic solution would require an reengineering in the operator
- * system like the extended converters.
- *
- * Solves #119 - QDataStream <</>> operators not working for QPixmap
- * http://bugs.openbossa.org/show_bug.cgi?id=119
- */
- bool hasReturnValue = overloadData.hasNonVoidReturnType();
- bool callExtendedReverseOperator = hasReturnValue
- && !rfunc->isInplaceOperator()
- && !rfunc->isCallOperator()
- && rfunc->isOperatorOverload();
-
- QScopedPointer<Indentation> reverseIndent;
-
- if (callExtendedReverseOperator) {
- QString revOpName = ShibokenGenerator::pythonOperatorFunctionName(rfunc);
- revOpName.insert(2, u'r');
+ // This code is intended for shift operations only: Make sure reverse <</>>
+ // operators defined in other classes (specially from other modules)
+ // are called. A proper and generic solution would require an reengineering
+ // in the operator system like the extended converters.
+ // Solves #119 - QDataStream <</>> operators not working for QPixmap.
+ const bool hasReturnValue = overloadData.hasNonVoidReturnType();
+
+ if (hasReturnValue && rfunc->functionType() == AbstractMetaFunction::ShiftOperator
+ && rfunc->isBinaryOperator()) {
// For custom classes, operations like __radd__ and __rmul__
// will enter an infinite loop.
- if (rfunc->isBinaryOperator() && revOpName.contains(u"shift")) {
- revOpName = u"Shiboken::PyMagicName::"_s + revOpName.replace(u"__"_s, u""_s) + u"()"_s;
- s << "static PyObject *attrName = " << revOpName << ";\n";
- s << "if (!isReverse\n";
- {
- Indentation indent(s);
- s << "&& Shiboken::Object::checkType(" << PYTHON_ARG << ")\n"
- << "&& !PyObject_TypeCheck(" << PYTHON_ARG << ", self->ob_type)\n"
- << "&& PyObject_HasAttr(" << PYTHON_ARG << ", attrName)) {\n";
-
- // This PyObject_CallMethod call will emit lots of warnings like
- // "deprecated conversion from string constant to char *" during compilation
- // due to the method name argument being declared as "char *" instead of "const char *"
- // issue 6952 http://bugs.python.org/issue6952
- s << "PyObject *revOpMethod = PyObject_GetAttr(" << PYTHON_ARG << ", attrName);\n";
- s << "if (revOpMethod && PyCallable_Check(revOpMethod)) {\n";
- {
- Indentation indent(s);
- s << PYTHON_RETURN_VAR << " = PyObject_CallFunction(revOpMethod, \"O\", self);\n"
- << "if (PyErr_Occurred() && (PyErr_ExceptionMatches(PyExc_NotImplementedError)"
- << " || PyErr_ExceptionMatches(PyExc_AttributeError))) {\n";
- {
- Indentation indent(s);
- s << "PyErr_Clear();\n"
- << "Py_XDECREF(" << PYTHON_RETURN_VAR << ");\n"
- << PYTHON_RETURN_VAR << " = " << NULL_PTR << ";\n";
- }
- s << "}\n";
- }
- s << "}\n"
- << "Py_XDECREF(revOpMethod);\n\n";
- } //
- s << "}\n\n"
- << "// Do not enter here if other object has implemented a reverse operator.\n"
- << "if (!" << PYTHON_RETURN_VAR << ") {\n";
- reverseIndent.reset(new Indentation(s));
- } // binary shift operator
- }
-
- if (maxArgs > 0)
- writeOverloadedFunctionDecisor(s, overloadData);
-
- writeFunctionCalls(s, overloadData, classContext, ErrorReturn::Default);
-
- if (!reverseIndent.isNull()) { // binary shift operator
- reverseIndent.reset();
- s << '\n' << "} // End of \"if (!" << PYTHON_RETURN_VAR << ")\"\n";
+ const QString pythonOp = ShibokenGenerator::pythonOperatorFunctionName(rfunc);
+ s << "static PyObject *attrName = Shiboken::PyMagicName::r"
+ << pythonOp.mid(2, pythonOp.size() -4) << "();\n" // Strip __
+ << "if (!isReverse\n" << indent
+ << "&& Shiboken::Object::checkType(" << PYTHON_ARG << ")\n"
+ << "&& !PyObject_TypeCheck(" << PYTHON_ARG << ", self->ob_type)\n"
+ << "&& PyObject_HasAttr(" << PYTHON_ARG << ", attrName)) {\n"
+ << "PyObject *revOpMethod = PyObject_GetAttr(" << PYTHON_ARG << ", attrName);\n"
+ << "if (revOpMethod && PyCallable_Check(revOpMethod)) {\n" << indent
+ << PYTHON_RETURN_VAR << " = PyObject_CallFunction(revOpMethod, \"O\", self);\n"
+ << "if (" << shibokenErrorsOccurred
+ << " && (PyErr_ExceptionMatches(PyExc_NotImplementedError)"
+ << " || PyErr_ExceptionMatches(PyExc_AttributeError))) {\n" << indent
+ << "PyErr_Clear();\n"
+ << "Py_XDECREF(" << PYTHON_RETURN_VAR << ");\n"
+ << PYTHON_RETURN_VAR << " = " << NULL_PTR << ";\n"
+ << outdent << "}\n"
+ << outdent << "}\n"
+ << "Py_XDECREF(revOpMethod);\n\n"
+ << outdent << "}\n\n"
+ << "// Do not enter here if other object has implemented a reverse operator.\n"
+ << "if (" << PYTHON_RETURN_VAR << " == nullptr) {\n" << indent;
+ if (maxArgs > 0)
+ writeOverloadedFunctionDecisor(s, overloadData, ErrorReturn::Default);
+ writeFunctionCalls(s, overloadData, classContext, ErrorReturn::Default);
+ s << outdent << '\n' << "} // End of \"if (!" << PYTHON_RETURN_VAR << ")\"\n";
+ } else { // binary shift operator
+ if (maxArgs > 0)
+ writeOverloadedFunctionDecisor(s, overloadData, ErrorReturn::Default);
+ writeFunctionCalls(s, overloadData, classContext, ErrorReturn::Default);
}
s << '\n';
@@ -2325,9 +2284,6 @@ void CppGenerator::writeMethodWrapper(TextStream &s, const OverloadData &overloa
s << "Py_RETURN_NONE;\n";
}
- if (needsArgumentErrorHandling(overloadData))
- writeErrorSection(s, overloadData, ErrorReturn::Default);
-
s<< outdent << "}\n\n";
}
@@ -2335,8 +2291,7 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, const OverloadData &
ErrorReturn errorReturn)
{
const auto rfunc = overloadData.referenceFunction();
- s << "PyTuple_GET_SIZE(args);\n";
- writeUnusedVariableCast(s, u"numArgs"_s);
+ s << "PyTuple_GET_SIZE(args);\n" << sbkUnusedVariableCast("numArgs");
int minArgs = overloadData.minArgs();
int maxArgs = overloadData.maxArgs();
@@ -2365,15 +2320,16 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, const OverloadData &
// Disable argument count checks for QObject constructors to allow for
// passing properties as KW args.
- auto *owner = rfunc->ownerClass();
- bool isQObjectConstructor = owner != nullptr && owner->isQObject()
+ const auto owner = rfunc->ownerClass();
+ bool isQObjectConstructor = owner && isQObject(owner)
&& rfunc->functionType() == AbstractMetaFunction::ConstructorFunction;
if (usesNamedArguments && !isQObjectConstructor) {
s << "errInfo.reset(Shiboken::checkInvalidArgumentCount(numArgs, "
<< minArgs << ", " << maxArgs << "));\n"
<< "if (!errInfo.isNull())\n" << indent
- << "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n" << outdent;
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n"
+ << outdent;
}
const QList<int> invalidArgsLength = overloadData.invalidArgumentLengths();
@@ -2385,7 +2341,8 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, const OverloadData &
s << "numArgs == " << invalidArgsLength.at(i);
}
s << ")\n" << indent
- << "goto " << cpythonFunctionName(rfunc) << "_TypeError;\n" << outdent;
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n"
+ << outdent;
}
s << '\n';
@@ -2396,7 +2353,7 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, const OverloadData &
funcName = rfunc->name();
QString argsVar = overloadData.hasVarargs() ? u"nonvarargs"_s : u"args"_s;
- s << "if (!";
+ s << "if (";
if (usesNamedArguments) {
s << "PyArg_ParseTuple(" << argsVar << ", \"|" << QByteArray(maxArgs, 'O')
<< ':' << funcName << '"';
@@ -2406,7 +2363,7 @@ void CppGenerator::writeArgumentsInitializer(TextStream &s, const OverloadData &
}
for (int i = 0; i < maxArgs; i++)
s << ", &(" << PYTHON_ARGS << '[' << i << "])";
- s << "))\n" << indent << errorReturn << outdent << '\n';
+ s << ") == 0)\n" << indent << errorReturn << outdent << '\n';
}
void CppGenerator::writeCppSelfConversion(TextStream &s, const GeneratorContext &context,
@@ -2417,23 +2374,15 @@ void CppGenerator::writeCppSelfConversion(TextStream &s, const GeneratorContext
return;
}
- static const QString pythonSelfVar = u"self"_s;
if (useWrapperClass)
s << "static_cast<" << className << " *>(";
- s << cpythonWrapperCPtr(context.metaClass(), pythonSelfVar);
+ s << cpythonWrapperCPtr(context.metaClass(), PYTHON_SELF_VAR);
if (useWrapperClass)
s << ')';
}
-void CppGenerator::writeSmartPointerCppSelfConversion(TextStream &s,
- const GeneratorContext &context)
-{
- Q_ASSERT(context.forSmartPointer());
- s << cpythonWrapperCPtr(context.preciseType(), u"self"_s);
-}
-
-static inline void writeCppSelfVarDef(TextStream &s,
- CppGenerator::CppSelfDefinitionFlags flags = {})
+void CppGenerator::writeCppSelfVarDef(TextStream &s,
+ CppSelfDefinitionFlags flags)
{
if (flags.testFlag(CppGenerator::CppSelfAsReference))
s << "auto &" << CPP_SELF_VAR << " = *";
@@ -2441,22 +2390,10 @@ static inline void writeCppSelfVarDef(TextStream &s,
s << "auto *" << CPP_SELF_VAR << " = ";
}
-void CppGenerator::writeSmartPointerCppSelfDefinition(TextStream &s,
- const GeneratorContext &context,
- ErrorReturn errorReturn,
- CppSelfDefinitionFlags flags)
-{
- Q_ASSERT(context.forSmartPointer());
- writeInvalidPyObjectCheck(s, u"self"_s, errorReturn);
- writeCppSelfVarDef(s, flags);
- writeSmartPointerCppSelfConversion(s, context);
- s << ";\n";
-}
-
void CppGenerator::writeCppSelfDefinition(TextStream &s,
const GeneratorContext &context,
ErrorReturn errorReturn,
- CppSelfDefinitionFlags flags) const
+ CppSelfDefinitionFlags flags)
{
Q_ASSERT(!(flags.testFlag(CppSelfAsReference) && flags.testFlag(HasStaticOverload)));
if (context.forSmartPointer()) {
@@ -2464,7 +2401,7 @@ void CppGenerator::writeCppSelfDefinition(TextStream &s,
return;
}
- const AbstractMetaClass *metaClass = context.metaClass();
+ AbstractMetaClassCPtr metaClass = context.metaClass();
const auto cppWrapper = context.metaClass()->cppWrapper();
// In the Python method, use the wrapper to access the protected
// functions.
@@ -2472,10 +2409,9 @@ void CppGenerator::writeCppSelfDefinition(TextStream &s,
&& cppWrapper.testFlag(AbstractMetaClass::CppProtectedHackWrapper);
Q_ASSERT(!useWrapperClass || context.useWrapper());
const QString className = useWrapperClass
- ? context.wrapperName()
- : (u"::"_s + metaClass->qualifiedCppName());
+ ? context.wrapperName() : getFullTypeName(metaClass);
- writeInvalidPyObjectCheck(s, u"self"_s, errorReturn);
+ writeInvalidPyObjectCheck(s, PYTHON_SELF_VAR, errorReturn);
if (flags.testFlag(CppSelfAsReference)) {
writeCppSelfVarDef(s, flags);
@@ -2489,76 +2425,71 @@ void CppGenerator::writeCppSelfDefinition(TextStream &s,
// PYSIDE-131: The single case of a class method for now: tr().
writeCppSelfVarDef(s, flags);
writeCppSelfConversion(s, context, className, useWrapperClass);
- s << ";\n";
- writeUnusedVariableCast(s, CPP_SELF_VAR);
+ s << ";\n" << sbkUnusedVariableCast(CPP_SELF_VAR);
}
return;
}
- s << className << " *" << CPP_SELF_VAR << " = nullptr;\n";
- writeUnusedVariableCast(s, CPP_SELF_VAR);
+ s << className << " *" << CPP_SELF_VAR << " = nullptr;\n"
+ << sbkUnusedVariableCast(CPP_SELF_VAR);
// Checks if the underlying C++ object is valid.
- s << "if (self)\n";
- {
- Indentation indent(s);
- s << CPP_SELF_VAR << " = ";
- writeCppSelfConversion(s, context, className, useWrapperClass);
- s << ";\n";
- }
+ s << "if (self)\n" << indent
+ << CPP_SELF_VAR << " = ";
+ writeCppSelfConversion(s, context, className, useWrapperClass);
+ s << ";\n"<< outdent;
}
void CppGenerator::writeCppSelfDefinition(TextStream &s,
const AbstractMetaFunctionCPtr &func,
const GeneratorContext &context,
ErrorReturn errorReturn,
- CppSelfDefinitionFlags flags) const
+ CppSelfDefinitionFlags flags)
{
if (!func->ownerClass() || func->isConstructor())
return;
if (func->isOperatorOverload() && func->isBinaryOperator()) {
QString checkFunc = cpythonCheckFunction(func->ownerClass()->typeEntry());
- s << "bool isReverse = " << checkFunc << PYTHON_ARG << ")\n";
- {
- Indentation indent1(s, 4);
- s << "&& !" << checkFunc << "self);\n";
- }
- s << "if (isReverse)\n";
- Indentation indent(s);
- s << "std::swap(self, " << PYTHON_ARG << ");\n";
+ s << "bool isReverse = " << checkFunc << PYTHON_ARG << ")\n"
+ << " && !" << checkFunc << "self);\n"
+ << "if (isReverse)\n" << indent
+ << "std::swap(self, " << PYTHON_ARG << ");\n" << outdent;
}
writeCppSelfDefinition(s, context, errorReturn, flags);
}
-void CppGenerator::writeErrorSection(TextStream &s, const OverloadData &overloadData,
- ErrorReturn errorReturn)
+QString CppGenerator::returnErrorWrongArguments(const OverloadData &overloadData,
+ ErrorReturn errorReturn)
{
const auto rfunc = overloadData.referenceFunction();
- s << '\n' << cpythonFunctionName(rfunc) << "_TypeError:\n";
- Indentation indentation(s);
QString argsVar = overloadData.pythonFunctionWrapperUsesListOfArguments()
? u"args"_s : PYTHON_ARG;
- s << "Shiboken::setErrorAboutWrongArguments(" << argsVar << ", fullName, errInfo);\n"
- << errorReturn;
+ switch (errorReturn) {
+ case ErrorReturn::Default:
+ return u"Shiboken::returnWrongArguments("_s + argsVar + u", fullName, errInfo)"_s;
+ case ErrorReturn::Zero:
+ return u"Shiboken::returnWrongArguments_Zero("_s + argsVar + u", fullName, errInfo)"_s;
+ case ErrorReturn::MinusOne:
+ return u"Shiboken::returnWrongArguments_MinusOne("_s + argsVar + u", fullName, errInfo)"_s;
+ case ErrorReturn::Void:
+ Q_ASSERT(false);
+ }
+ return {};
}
void CppGenerator::writeFunctionReturnErrorCheckSection(TextStream &s,
ErrorReturn errorReturn,
bool hasReturnValue)
{
- s << "if (PyErr_Occurred()";
+ s << "if (" << shibokenErrorsOccurred;
if (hasReturnValue)
- s << " || !" << PYTHON_RETURN_VAR;
- s << ") {\n";
- {
- Indentation indent(s);
- if (hasReturnValue)
- s << "Py_XDECREF(" << PYTHON_RETURN_VAR << ");\n";
- s << errorReturn;
- }
- s << "}\n";
+ s << " || " << PYTHON_RETURN_VAR << " == nullptr";
+ s << ") {\n" << indent;
+ if (hasReturnValue)
+ s << "Py_XDECREF(" << PYTHON_RETURN_VAR << ");\n";
+ s << errorReturn << outdent << "}\n";
}
void CppGenerator::writeInvalidPyObjectCheck(TextStream &s, const QString &pyObj,
@@ -2604,7 +2535,7 @@ void CppGenerator::writeTypeCheck(TextStream &s, const AbstractMetaType &argType
if (!argType.typeEntry()->isCustom()) {
typeCheck = u'(' + pythonToCppConverterForArgumentName(argumentName)
+ u" = "_s + typeCheck + u"))"_s;
- if (!isNumber && argType.typeEntry()->isCppPrimitive()) {
+ if (!isNumber && isCppPrimitive(argType.typeEntry())) {
typeCheck.prepend(cpythonCheckFunction(argType) + u'('
+ argumentName + u") && "_s);
}
@@ -2662,10 +2593,10 @@ static void checkTypeViability(const AbstractMetaFunctionCPtr &func)
}
void CppGenerator::writeTypeCheck(TextStream &s,
- const QSharedPointer<OverloadDataNode> &overloadData,
+ const std::shared_ptr<OverloadDataNode> &overloadData,
const QString &argumentName)
{
- QSet<const TypeEntry *> numericTypes;
+ QSet<TypeEntryCPtr> numericTypes;
const OverloadDataList &siblings = overloadData->parent()->children();
for (const auto &sibling : siblings) {
for (const auto &func : sibling->overloads()) {
@@ -2689,21 +2620,24 @@ void CppGenerator::writeTypeCheck(TextStream &s,
writeTypeCheck(s, argType, argumentName, numberType, rejectNull);
}
-void CppGenerator::writeArgumentConversion(TextStream &s,
- const AbstractMetaType &argType,
- const QString &argName, const QString &pyArgName,
- ErrorReturn errorReturn,
- const AbstractMetaClass *context,
- const QString &defaultValue,
- bool castArgumentAsUnused) const
+qsizetype CppGenerator::writeArgumentConversion(TextStream &s,
+ const AbstractMetaType &argType,
+ const QString &argName,
+ const QString &pyArgName,
+ ErrorReturn errorReturn,
+ const AbstractMetaClassCPtr &context,
+ const QString &defaultValue,
+ bool castArgumentAsUnused) const
{
+ qsizetype result = 0;
if (argType.typeEntry()->isCustom() || argType.typeEntry()->isVarargs())
- return;
+ return result;
if (argType.isWrapperType())
writeInvalidPyObjectCheck(s, pyArgName, errorReturn);
- writePythonToCppTypeConversion(s, argType, pyArgName, argName, context, defaultValue);
+ result = writePythonToCppTypeConversion(s, argType, pyArgName, argName, context, defaultValue);
if (castArgumentAsUnused)
- writeUnusedVariableCast(s, argName);
+ s << sbkUnusedVariableCast(argName);
+ return result;
}
AbstractMetaType
@@ -2723,12 +2657,12 @@ static inline QString arrayHandleType(const AbstractMetaTypeList &nestedArrayTyp
{
switch (nestedArrayTypes.size()) {
case 1:
- return QStringLiteral("Shiboken::Conversions::ArrayHandle<")
+ return "Shiboken::Conversions::ArrayHandle<"_L1
+ nestedArrayTypes.constLast().minimalSignature() + u'>';
case 2:
- return QStringLiteral("Shiboken::Conversions::Array2Handle<")
+ return "Shiboken::Conversions::Array2Handle<"_L1
+ nestedArrayTypes.constLast().minimalSignature()
- + QStringLiteral(", ")
+ + ", "_L1
+ QString::number(nestedArrayTypes.constFirst().arrayElementCount())
+ u'>';
}
@@ -2767,51 +2701,24 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
const AbstractMetaType &type,
const QString &pyIn,
const QString &cppOut,
- const AbstractMetaClass *context,
+ const AbstractMetaClassCPtr &context,
const QString &defaultValue) const
{
- const TypeEntry *typeEntry = type.typeEntry();
+ TypeEntryCPtr typeEntry = type.typeEntry();
if (typeEntry->isCustom() || typeEntry->isVarargs())
return 0;
- qsizetype indirections = -type.indirectionsV().size();
+ const auto arg = GeneratorArgument::fromMetaType(type);
+ const bool isPrimitive = arg.type == GeneratorArgument::Type::Primitive;
QString cppOutAux = cppOut + u"_local"_s;
- const bool isPrimitive = typeEntry->isPrimitive();
- const bool isEnum = typeEntry->isEnum();
- const bool isFlags = typeEntry->isFlags();
- const bool treatAsPointer = type.valueTypeWithCopyConstructorOnlyPassed();
- const bool isContainer = typeEntry->isContainer();
- bool isPointerOrObjectType = (type.isObjectType() || type.isPointer())
- && !type.isUserPrimitive() && !type.isExtendedCppPrimitive()
- && !isEnum && !isFlags;
- const bool isNotContainerEnumOrFlags = !isContainer
- && !isEnum && !isFlags;
- 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. For containers, the PyList/container
- // conversion is by value whereas opaque containers use pointer conversion.
- // For a container passed by pointer, a local variable is also needed.
- const bool valueOrPointer = mayHaveImplicitConversion
- || type.generateOpaqueContainer()
- || (isContainer && indirections != 0);
-
- const AbstractMetaTypeList &nestedArrayTypes = type.nestedArrayTypes();
- const bool isCppPrimitiveArray = !nestedArrayTypes.isEmpty()
- && nestedArrayTypes.constLast().isCppPrimitive();
- QString typeName = isCppPrimitiveArray
- ? arrayHandleType(nestedArrayTypes)
- : getFullTypeNameWithoutModifiers(type);
+ QString typeName = arg.type == GeneratorArgument::Type::CppPrimitiveArray
+ ? arrayHandleType(type.nestedArrayTypes())
+ : getFullTypeNameWithoutModifiers(type);
bool isProtectedEnum = false;
- if (isEnum && avoidProtectedHack()) {
+ if (arg.type == GeneratorArgument::Type::Enum && avoidProtectedHack()) {
auto metaEnum = api().findAbstractMetaEnum(type.typeEntry());
if (metaEnum.has_value() && metaEnum->isProtected()) {
typeName = wrapperName(context) + u"::"_s
@@ -2821,19 +2728,21 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
}
s << typeName;
- if (isCppPrimitiveArray) {
+ switch (arg.conversion) {
+ case GeneratorArgument::Conversion::CppPrimitiveArray:
s << ' ' << cppOut;
- } else if (valueOrPointer) {
- ++indirections;
+ break;
+ case GeneratorArgument::Conversion::ValueOrPointer: {
// Generate either value conversion for &cppOutAux or pointer
// conversion for &cppOut
s << ' ' << cppOutAux;
// No default value for containers which can also be passed by pointer.
- if (!isContainer)
+ if (arg.type != GeneratorArgument::Type::Container || type.indirections() == 0)
writeMinimalConstructorExpression(s, api(), type, isPrimitive, defaultValue);
s << ";\n" << typeName << " *" << cppOut << " = &" << cppOutAux;
- } else if (treatAsPointer || isPointerOrObjectType) {
- ++indirections;
+ }
+ break;
+ case GeneratorArgument::Conversion::Pointer: {
s << " *" << cppOut;
if (!defaultValue.isEmpty()) {
const bool needsConstCast = !isNullPtr(defaultValue)
@@ -2846,7 +2755,9 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
if (needsConstCast)
s << ')';
}
- } else {
+ }
+ break;
+ case GeneratorArgument::Conversion::Default:
s << ' ' << cppOut;
if (isProtectedEnum && avoidProtectedHack()) {
s << " = ";
@@ -2854,11 +2765,14 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
s << "{}";
else
s << defaultValue;
- } else if (type.isUserPrimitive() || isEnum || isFlags) {
+ } else if (type.isUserPrimitive()
+ || arg.type == GeneratorArgument::Type::Enum
+ || arg.type == GeneratorArgument::Type::Flags) {
writeMinimalConstructorExpression(s, api(), typeEntry, isPrimitive, defaultValue);
- } else if (!type.isContainer() && !type.isSmartPointer()) {
+ } else if ((!type.isContainer() || type.indirections() == 0) && !type.isSmartPointer()) {
writeMinimalConstructorExpression(s, api(), type, isPrimitive, defaultValue);
}
+ break;
}
s << ";\n";
@@ -2866,7 +2780,7 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
QString pythonToCppCall = pythonToCppFunc + u'(' + pyIn + u", &"_s
+ cppOut + u')';
- if (!valueOrPointer) {
+ if (arg.conversion != GeneratorArgument::Conversion::ValueOrPointer) {
// pythonToCppFunc may be 0 when less parameters are passed and
// the defaultValue takes effect.
if (!defaultValue.isEmpty())
@@ -2874,7 +2788,7 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
s << pythonToCppCall << ";\n";
if (!defaultValue.isEmpty())
s << outdent;
- return indirections;
+ return arg.indirections;
}
// pythonToCppFunc may be 0 when less parameters are passed and
@@ -2892,7 +2806,7 @@ qsizetype CppGenerator::writePythonToCppTypeConversion(TextStream &s,
else
s << "}\n" << outdent;
- return indirections;
+ return arg.indirections;
}
static void addConversionRuleCodeSnippet(CodeSnipList &snippetList, QString &rule,
@@ -2950,7 +2864,9 @@ void CppGenerator::writeNoneReturn(TextStream &s, const AbstractMetaFunctionCPtr
}
}
-void CppGenerator::writeOverloadedFunctionDecisor(TextStream &s, const OverloadData &overloadData) const
+void CppGenerator::writeOverloadedFunctionDecisor(TextStream &s,
+ const OverloadData &overloadData,
+ ErrorReturn errorReturn) const
{
s << "// Overloaded function decisor\n";
const auto rfunc = overloadData.referenceFunction();
@@ -2960,7 +2876,7 @@ void CppGenerator::writeOverloadedFunctionDecisor(TextStream &s, const OverloadD
s << "// " << i << ": ";
if (func->isStatic())
s << "static ";
- if (const auto *decl = func->declaringClass())
+ if (const auto &decl = func->declaringClass())
s << decl->name() << "::";
s << func->signatureComment() << '\n';
}
@@ -2977,8 +2893,9 @@ void CppGenerator::writeOverloadedFunctionDecisor(TextStream &s, const OverloadD
}
s << "// Function signature not found.\n"
- << "if (overloadId == -1) goto "
- << cpythonFunctionName(overloadData.referenceFunction()) << "_TypeError;\n\n";
+ << "if (overloadId == -1)\n" << indent
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n\n"
+ << outdent;
}
void CppGenerator::writeOverloadedFunctionDecisorEngine(TextStream &s,
@@ -3040,21 +2957,17 @@ void CppGenerator::writeOverloadedFunctionDecisorEngine(TextStream &s,
if (hasDefaultCall) {
isFirst = false;
int numArgs = node->argPos() + 1;
- s << "if (numArgs == " << numArgs << ") {\n";
- {
- Indentation indent(s);
- auto func = referenceFunction;
- for (const auto &child : children) {
- const auto defValFunc = child->getFunctionWithDefaultValue();
- if (!defValFunc.isNull()) {
- func = defValFunc;
- break;
- }
+ s << "if (numArgs == " << numArgs << ") {\n" << indent;
+ auto func = referenceFunction;
+ for (const auto &child : children) {
+ const auto defValFunc = child->getFunctionWithDefaultValue();
+ if (defValFunc) {
+ func = defValFunc;
+ break;
}
- s << "overloadId = " << overloadData.functionNumber(func)
- << "; // " << func->minimalSignature() << '\n';
}
- s << '}';
+ s << "overloadId = " << overloadData.functionNumber(func)
+ << "; // " << func->minimalSignature() << '\n' << outdent << '}';
}
for (auto child : children) {
@@ -3074,7 +2987,7 @@ void CppGenerator::writeOverloadedFunctionDecisorEngine(TextStream &s,
int sequenceArgCount = 0;
while (od && !od->argType().isVarargs()) {
const bool typeReplacedByPyObject = od->isTypeModified()
- && od->modifiedArgType().name() == cPyObjectT();
+ && od->modifiedArgType().name() == cPyObjectT;
if (!typeReplacedByPyObject) {
if (usePyArgs)
pyArgName = pythonArgsAt(od->argPos());
@@ -3082,13 +2995,12 @@ void CppGenerator::writeOverloadedFunctionDecisorEngine(TextStream &s,
auto func = od->referenceFunction();
if (func->isConstructor() && func->arguments().size() == 1) {
- const AbstractMetaClass *ownerClass = func->ownerClass();
- const ComplexTypeEntry *baseContainerType = ownerClass->typeEntry()->baseContainerType();
+ AbstractMetaClassCPtr ownerClass = func->ownerClass();
+ ComplexTypeEntryCPtr baseContainerType = ownerClass->typeEntry()->baseContainerType();
if (baseContainerType && baseContainerType == func->arguments().constFirst().type().typeEntry()
&& ownerClass->isCopyable()) {
- tck << '!' << cpythonCheckFunction(ownerClass->typeEntry()) << pyArgName << ")\n";
- Indentation indent(s);
- tck << "&& ";
+ tck << '!' << cpythonCheckFunction(ownerClass->typeEntry())
+ << pyArgName << ")\n" << indent << "&& " << outdent;
}
}
writeTypeCheck(tck, od, pyArgName);
@@ -3136,15 +3048,11 @@ void CppGenerator::writeOverloadedFunctionDecisorEngine(TextStream &s,
if (typeChecks.isEmpty()) {
s << "true";
} else {
- Indentation indent(s);
- s << typeChecks.join(u"\n&& "_s);
- }
- s << ") {\n";
- {
- Indentation indent(s);
- writeOverloadedFunctionDecisorEngine(s, overloadData, child.data());
+ s << indent << typeChecks.join(u"\n&& "_s) << outdent;
}
- s << "}";
+ s << ") {\n" << indent;
+ writeOverloadedFunctionDecisorEngine(s, overloadData, child.get());
+ s << outdent << '}';
}
s << '\n';
}
@@ -3155,36 +3063,33 @@ void CppGenerator::writeFunctionCalls(TextStream &s, const OverloadData &overloa
{
const AbstractMetaFunctionCList &overloads = overloadData.overloads();
s << "// Call function/method\n"
- << (overloads.size() > 1 ? "switch (overloadId) " : "") << "{\n";
- {
- Indentation indent(s);
- if (overloads.size() == 1) {
- writeSingleFunctionCall(s, overloadData, overloads.constFirst(), context,
- errorReturn);
- } else {
- for (qsizetype i = 0; i < overloads.size(); ++i) {
- const auto func = overloads.at(i);
- s << "case " << i << ": // " << func->signature() << "\n{\n";
- {
- Indentation indent(s);
- writeSingleFunctionCall(s, overloadData, func, context, errorReturn);
- s << "break;\n";
- }
- s << "}\n";
- }
+ << (overloads.size() > 1 ? "switch (overloadId) " : "") << "{\n" << indent;
+ if (overloads.size() == 1) {
+ writeSingleFunctionCall(s, overloadData, overloads.constFirst(), context,
+ errorReturn);
+ } else {
+ for (qsizetype i = 0; i < overloads.size(); ++i) {
+ const auto func = overloads.at(i);
+ s << "case " << i << ": // " << func->signature() << "\n{\n" << indent;
+ writeSingleFunctionCall(s, overloadData, func, context, errorReturn);
+ s << "break;\n" << outdent << "}\n";
}
}
- s << "}\n";
+ s << outdent << "}\n";
}
static void writeDeprecationWarning(TextStream &s,
const GeneratorContext &context,
- const AbstractMetaFunctionCPtr &func)
+ const AbstractMetaFunctionCPtr &func,
+ CppGenerator::ErrorReturn errorReturn)
{
s << "Shiboken::Warnings::warnDeprecated(\"";
- if (auto *cls = context.metaClass())
+ if (const auto cls = context.metaClass())
s << cls->name() << "\", ";
- s << '"' << func->signature().replace(u"::"_s, u"."_s) << "\");\n";
+ // Check error in case "warning-as-error" is set.
+ s << '"' << func->signature().replace(u"::"_s, u"."_s) << "\");\n"
+ << "if (" << shibokenErrorsOccurred << ")\n"
+ << indent << errorReturn << outdent;
}
void CppGenerator::writeSingleFunctionCall(TextStream &s,
@@ -3194,7 +3099,7 @@ void CppGenerator::writeSingleFunctionCall(TextStream &s,
ErrorReturn errorReturn) const
{
if (func->isDeprecated())
- writeDeprecationWarning(s, context, func);
+ writeDeprecationWarning(s, context, func, errorReturn);
if (func->functionType() == AbstractMetaFunction::EmptyFunction) {
s << "Shiboken::Errors::setPrivateMethod(\""
@@ -3206,22 +3111,24 @@ void CppGenerator::writeSingleFunctionCall(TextStream &s,
const bool usePyArgs = overloadData.pythonFunctionWrapperUsesListOfArguments();
// Handle named arguments.
- writeNamedArgumentResolution(s, func, usePyArgs, overloadData);
+ writeNamedArgumentResolution(s, func, usePyArgs, overloadData, errorReturn);
bool injectCodeCallsFunc = injectedCodeCallsCppFunction(context, func);
bool mayHaveUnunsedArguments = !func->isUserAdded() && func->hasInjectedCode() && injectCodeCallsFunc;
int removedArgs = 0;
- for (qsizetype argIdx = 0; argIdx < func->arguments().size(); ++argIdx) {
+
+ const auto argCount = func->arguments().size();
+ QList<qsizetype> indirections(argCount, 0);
+ for (qsizetype argIdx = 0; argIdx < argCount; ++argIdx) {
const bool hasConversionRule =
func->hasConversionRule(TypeSystem::NativeCode, int(argIdx + 1));
const AbstractMetaArgument &arg = func->arguments().at(argIdx);
if (arg.isModifiedRemoved()) {
if (!arg.defaultValueExpression().isEmpty()) {
- const QString cppArgRemoved = CPP_ARG_REMOVED
- + QString::number(argIdx);
+ const QString cppArgRemoved = CPP_ARG_REMOVED(argIdx);
s << getFullTypeName(arg.type()) << ' ' << cppArgRemoved;
- s << " = " << arg.defaultValueExpression() << ";\n";
- writeUnusedVariableCast(s, cppArgRemoved);
+ s << " = " << arg.defaultValueExpression() << ";\n"
+ << sbkUnusedVariableCast(cppArgRemoved);
} else if (!injectCodeCallsFunc && !func->isUserAdded() && !hasConversionRule) {
// When an argument is removed from a method signature and no other means of calling
// the method are provided (as with code injection) the generator must abort.
@@ -3240,21 +3147,21 @@ void CppGenerator::writeSingleFunctionCall(TextStream &s,
continue;
auto argType = getArgumentType(func, argIdx);
int argPos = argIdx - removedArgs;
- QString argName = CPP_ARG + QString::number(argPos);
QString pyArgName = usePyArgs ? pythonArgsAt(argPos) : PYTHON_ARG;
- writeArgumentConversion(s, argType, argName, pyArgName, errorReturn,
- func->implementingClass(), arg.defaultValueExpression(),
- func->isUserAdded());
+ indirections[argIdx] =
+ writeArgumentConversion(s, argType, CPP_ARG_N(argPos), pyArgName, errorReturn,
+ func->implementingClass(), arg.defaultValueExpression(),
+ func->isUserAdded());
}
s << '\n';
int numRemovedArgs = OverloadData::numberOfRemovedArguments(func);
- s << "if (!PyErr_Occurred()) {\n" << indent;
+ s << "if (Shiboken::Errors::occurred() == nullptr) {\n" << indent;
writeMethodCall(s, func, context,
overloadData.pythonFunctionWrapperUsesListOfArguments(),
- func->arguments().size() - numRemovedArgs, errorReturn);
+ func->arguments().size() - numRemovedArgs, indirections, errorReturn);
if (!func->isConstructor())
writeNoneReturn(s, func, overloadData.hasNonVoidReturnType());
@@ -3276,8 +3183,8 @@ QString CppGenerator::pythonToCppFunctionName(const AbstractMetaType &sourceType
{
return pythonToCppFunctionName(fixedCppTypeName(sourceType), fixedCppTypeName(targetType));
}
-QString CppGenerator::pythonToCppFunctionName(const CustomConversion::TargetToNativeConversion *toNative,
- const TypeEntry *targetType)
+QString CppGenerator::pythonToCppFunctionName(const TargetToNativeConversion &toNative,
+ const TypeEntryCPtr &targetType)
{
return pythonToCppFunctionName(fixedCppTypeName(toNative), fixedCppTypeName(targetType));
}
@@ -3291,8 +3198,8 @@ QString CppGenerator::convertibleToCppFunctionName(const AbstractMetaType &sourc
{
return convertibleToCppFunctionName(fixedCppTypeName(sourceType), fixedCppTypeName(targetType));
}
-QString CppGenerator::convertibleToCppFunctionName(const CustomConversion::TargetToNativeConversion *toNative,
- const TypeEntry *targetType)
+QString CppGenerator::convertibleToCppFunctionName(const TargetToNativeConversion &toNative,
+ const TypeEntryCPtr &targetType)
{
return convertibleToCppFunctionName(fixedCppTypeName(toNative), fixedCppTypeName(targetType));
}
@@ -3302,9 +3209,10 @@ void CppGenerator::writeCppToPythonFunction(TextStream &s, const QString &code,
{
QString prettyCode = code;
- processCodeSnip(prettyCode);
+ const QString funcName = cppToPythonFunctionName(sourceTypeName, targetTypeName);
+ processCodeSnip(prettyCode, funcName);
- s << "static PyObject *" << cppToPythonFunctionName(sourceTypeName, targetTypeName)
+ s << "static PyObject *" << funcName
<< "(const void *cppIn)\n{\n" << indent << prettyCode
<< ensureEndl << outdent << "}\n";
}
@@ -3333,28 +3241,39 @@ static void replaceCppToPythonVariables(QString &code, const QString &typeName,
code.replace(u"%out"_s, u"pyOut"_s);
}
-void CppGenerator::writeCppToPythonFunction(TextStream &s, const CustomConversion *customConversion) const
+void CppGenerator::writeCppToPythonFunction(TextStream &s,
+ const CustomConversionPtr &customConversion) const
{
QString code = customConversion->nativeToTargetConversion();
- auto *ownerType = customConversion->ownerType();
+ auto ownerType = customConversion->ownerType();
const bool constRef = !ownerType->isPrimitive(); // PyCapsule needs a non-const ref
replaceCppToPythonVariables(code, getFullTypeName(ownerType), constRef);
writeCppToPythonFunction(s, code, fixedCppTypeName(customConversion->ownerType()));
}
+
+QString CppGenerator::containerNativeToTargetTypeName(const ContainerTypeEntryCPtr &type)
+{
+ QString result = type->targetLangApiName();
+ if (result != cPyObjectT) {
+ result = containerCpythonBaseName(type);
+ if (result == cPySequenceT)
+ result = cPyListT;
+ }
+ return result;
+}
+
void CppGenerator::writeCppToPythonFunction(TextStream &s, const AbstractMetaType &containerType) const
{
- const CustomConversion *customConversion = containerType.typeEntry()->customConversion();
- if (!customConversion) {
+ Q_ASSERT(containerType.typeEntry()->isContainer());
+ auto cte = std::static_pointer_cast<const ContainerTypeEntry>(containerType.typeEntry());
+ if (!cte->hasCustomConversion()) {
QString m;
QTextStream(&m) << "Can't write the C++ to Python conversion function for container type '"
<< containerType.typeEntry()->qualifiedCppName()
<< "' - no conversion rule was defined for it in the type system.";
throw Exception(m);
}
- if (!containerType.typeEntry()->isContainer()) {
- writeCppToPythonFunction(s, customConversion);
- return;
- }
+ const auto customConversion = cte->customConversion();
QString code = customConversion->nativeToTargetConversion();
for (qsizetype i = 0; i < containerType.instantiations().size(); ++i) {
const AbstractMetaType &type = containerType.instantiations().at(i);
@@ -3364,16 +3283,18 @@ void CppGenerator::writeCppToPythonFunction(TextStream &s, const AbstractMetaTyp
code.replace(u"%INTYPE_"_s + QString::number(i), typeName);
}
replaceCppToPythonVariables(code, getFullTypeNameWithoutModifiers(containerType), true);
- processCodeSnip(code);
- writeCppToPythonFunction(s, code, fixedCppTypeName(containerType));
+ processCodeSnip(code, containerType.typeEntry()->qualifiedCppName());
+ writeCppToPythonFunction(s, code, fixedCppTypeName(containerType),
+ containerNativeToTargetTypeName(cte));
}
void CppGenerator::writePythonToCppFunction(TextStream &s, const QString &code, const QString &sourceTypeName,
const QString &targetTypeName) const
{
QString prettyCode = code;
- processCodeSnip(prettyCode);
- s << "static void " << pythonToCppFunctionName(sourceTypeName, targetTypeName)
+ const QString funcName = pythonToCppFunctionName(sourceTypeName, targetTypeName);
+ processCodeSnip(prettyCode, funcName);
+ s << "static void " << funcName
<< "(PyObject *pyIn, void *cppOut)\n{\n" << indent << prettyCode
<< ensureEndl << outdent << "}\n";
}
@@ -3391,16 +3312,15 @@ void CppGenerator::writeIsPythonConvertibleToCppFunction(TextStream &s,
s << "static PythonToCppFunc " << convertibleToCppFunctionName(sourceTypeName, targetTypeName);
s << "(PyObject *pyIn)\n{\n" << indent;
if (acceptNoneAsCppNull) {
- s << "if (pyIn == Py_None)\n";
- Indentation indent(s);
- s << "return Shiboken::Conversions::nonePythonToCppNullPtr;\n";
- }
- s << "if (" << condition << ")\n";
- {
- Indentation indent(s);
- s << "return " << pythonToCppFuncName << ";\n";
+ s << "if (pyIn == Py_None)\n" << indent
+ << "return Shiboken::Conversions::nonePythonToCppNullPtr;\n" << outdent;
+ } else {
+ if (!condition.contains(u"pyIn"))
+ s << sbkUnusedVariableCast("pyIn");
}
- s << "return {};\n" << outdent << "}\n";
+ s << "if (" << condition << ")\n" << indent
+ << "return " << pythonToCppFuncName << ";\n" << outdent
+ << "return {};\n" << outdent << "}\n";
}
void CppGenerator::writePythonToCppConversionFunctions(TextStream &s,
@@ -3422,7 +3342,9 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s,
? targetType.cppSignature()
: getFullTypeName(targetType.typeEntry());
c << "*reinterpret_cast<" << fullTypeName << " *>(cppOut) = "
- << fullTypeName << '(' << conversion << ");";
+ << fullTypeName << '('
+ << (sourceType.isUniquePointer() ? stdMove(conversion) : conversion)
+ << ");";
QString sourceTypeName = fixedCppTypeName(sourceType);
QString targetTypeName = fixedCppTypeName(targetType);
writePythonToCppFunction(s, c.toString(), sourceTypeName, targetTypeName);
@@ -3435,16 +3357,16 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s,
}
void CppGenerator::writePythonToCppConversionFunctions(TextStream &s,
- const CustomConversion::TargetToNativeConversion *toNative,
- const TypeEntry *targetType) const
+ const TargetToNativeConversion &toNative,
+ const TypeEntryCPtr &targetType) const
{
// Python to C++ conversion function.
- QString code = toNative->conversion();
+ QString code = toNative.conversion();
QString inType;
- if (toNative->sourceType())
- inType = cpythonTypeNameExt(toNative->sourceType());
+ if (toNative.sourceType())
+ inType = cpythonTypeNameExt(toNative.sourceType());
else
- inType = u'(' + toNative->sourceTypeName() + u"_TypeF())"_s;
+ inType = u'(' + toNative.sourceTypeName() + u"_TypeF())"_s;
code.replace(u"%INTYPE"_s, inType);
code.replace(u"%OUTTYPE"_s, targetType->qualifiedCppName());
code.replace(u"%in"_s, u"pyIn"_s);
@@ -3456,46 +3378,45 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s,
writePythonToCppFunction(s, code, sourceTypeName, targetTypeName);
// Python to C++ convertible check function.
- QString typeCheck = toNative->sourceTypeCheck();
+ QString typeCheck = toNative.sourceTypeCheck();
if (typeCheck.isEmpty()) {
- QString pyTypeName = toNative->sourceTypeName();
+ QString pyTypeName = toNative.sourceTypeName();
if (pyTypeName == u"Py_None" || pyTypeName == u"PyNone")
typeCheck = u"%in == Py_None"_s;
- else if (pyTypeName == u"SbkEnumType")
- typeCheck = u"Shiboken::isShibokenEnum(%in)"_s;
else if (pyTypeName == u"SbkObject")
typeCheck = u"Shiboken::Object::checkType(%in)"_s;
}
if (typeCheck.isEmpty()) {
- if (!toNative->sourceType() || toNative->sourceType()->isPrimitive()) {
+ if (!toNative.sourceType() || toNative.sourceType()->isPrimitive()) {
QString m;
QTextStream(&m) << "User added implicit conversion for C++ type '" << targetType->qualifiedCppName()
<< "' must provide either an input type check function or a non primitive type entry.";
throw Exception(m);
}
typeCheck = u"PyObject_TypeCheck(%in, "_s
- + cpythonTypeNameExt(toNative->sourceType()) + u')';
+ + cpythonTypeNameExt(toNative.sourceType()) + u')';
}
typeCheck.replace(u"%in"_s, u"pyIn"_s);
- processCodeSnip(typeCheck);
+ processCodeSnip(typeCheck, targetType->qualifiedCppName());
writeIsPythonConvertibleToCppFunction(s, sourceTypeName, targetTypeName, typeCheck);
}
void CppGenerator::writePythonToCppConversionFunctions(TextStream &s, const AbstractMetaType &containerType) const
{
- const CustomConversion *customConversion = containerType.typeEntry()->customConversion();
- if (!customConversion) {
- //qFatal
- return;
- }
- const CustomConversion::TargetToNativeConversions &toCppConversions = customConversion->targetToNativeConversions();
- if (toCppConversions.isEmpty()) {
- //qFatal
- return;
- }
+ Q_ASSERT(containerType.typeEntry()->isContainer());
+ const auto cte = std::static_pointer_cast<const ContainerTypeEntry>(containerType.typeEntry());
+ const auto customConversion = cte->customConversion();
+ for (const auto &conv : customConversion->targetToNativeConversions())
+ writePythonToCppConversionFunction(s, containerType, conv);
+}
+
+void CppGenerator::writePythonToCppConversionFunction(TextStream &s,
+ const AbstractMetaType &containerType,
+ const TargetToNativeConversion &conv) const
+{
// Python to C++ conversion function.
QString cppTypeName = getFullTypeNameWithoutModifiers(containerType);
- QString code = toCppConversions.constFirst()->conversion();
+ QString code = conv.conversion();
const QString line = u"auto &cppOutRef = *reinterpret_cast<"_s
+ cppTypeName + u" *>(cppOut);"_s;
CodeSnipAbstract::prependCode(&code, line);
@@ -3503,7 +3424,8 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s, const Abst
const AbstractMetaType &type = containerType.instantiations().at(i);
QString typeName = getFullTypeName(type);
// Containers of opaque containers are not handled here.
- if (type.shouldDereferenceArgument() > 0 && !type.generateOpaqueContainer()) {
+ const auto generatorArg = GeneratorArgument::fromMetaType(type);
+ if (generatorArg.indirections > 0 && !type.generateOpaqueContainer()) {
for (int pos = 0; ; ) {
const QRegularExpressionMatch match = convertToCppRegEx().match(code, pos);
if (!match.hasMatch())
@@ -3522,7 +3444,8 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s, const Abst
code.replace(u"%in"_s, u"pyIn"_s);
code.replace(u"%out"_s, u"cppOutRef"_s);
QString typeName = fixedCppTypeName(containerType);
- writePythonToCppFunction(s, code, typeName, typeName);
+ const QString &sourceTypeName = conv.sourceTypeName();
+ writePythonToCppFunction(s, code, sourceTypeName, typeName);
// Python to C++ convertible check function.
QString typeCheck = cpythonCheckFunction(containerType);
@@ -3530,7 +3453,7 @@ void CppGenerator::writePythonToCppConversionFunctions(TextStream &s, const Abst
typeCheck = u"false"_s;
else
typeCheck = typeCheck + u"pyIn)"_s;
- writeIsPythonConvertibleToCppFunction(s, typeName, typeName, typeCheck);
+ writeIsPythonConvertibleToCppFunction(s, sourceTypeName, typeName, typeCheck);
s << '\n';
}
@@ -3562,82 +3485,87 @@ void CppGenerator::writeSetPythonToCppPointerConversion(TextStream &s,
converterVar, pythonToCppFunc, isConvertibleFunc);
}
-void CppGenerator::writeNamedArgumentResolution(TextStream &s, const AbstractMetaFunctionCPtr &func,
- bool usePyArgs, const OverloadData &overloadData)
+// PYSIDE-1986: Some QObject derived classes, (QVBoxLayout) do not have default
+// arguments, which breaks setting properties by named arguments. Force the
+// handling code to be generated nevertheless for applicable widget classes,
+// so that the mechanism of falling through to the error handling to set
+// the properties works nevertheless.
+static bool forceQObjectNamedArguments(const AbstractMetaFunctionCPtr &func)
+{
+ if (func->functionType() != AbstractMetaFunction::ConstructorFunction)
+ return false;
+ const auto owner = func->ownerClass();
+ Q_ASSERT(owner);
+ if (!isQObject(owner))
+ return false;
+ const QString &name = owner->name();
+ return name == u"QVBoxLayout" || name == u"QHBoxLayout"
+ || name == u"QSplitterHandle" || name == u"QSizeGrip";
+}
+
+void CppGenerator::writeNamedArgumentResolution(TextStream &s,
+ const AbstractMetaFunctionCPtr &func,
+ bool usePyArgs,
+ const OverloadData &overloadData,
+ ErrorReturn errorReturn)
{
const AbstractMetaArgumentList &args = OverloadData::getArgumentsWithDefaultValues(func);
- if (args.isEmpty()) {
+ const bool hasDefaultArguments = !args.isEmpty();
+ const bool force = !hasDefaultArguments && usePySideExtensions()
+ && forceQObjectNamedArguments(func);
+ if (!hasDefaultArguments && !force) {
if (overloadData.hasArgumentWithDefaultValue()) {
// PySide-535: Allow for empty dict instead of nullptr in PyPy
- s << "if (kwds && PyDict_Size(kwds) > 0) {\n";
- {
- Indentation indent(s);
- s << "errInfo.reset(kwds);\n"
- << "Py_INCREF(errInfo.object());\n"
- << "goto " << cpythonFunctionName(func) << "_TypeError;\n";
- }
- s << "}\n";
+ s << "if (kwds != nullptr && PyDict_Size(kwds) > 0) {\n" << indent
+ << "errInfo.reset(kwds);\n"
+ << "Py_INCREF(errInfo.object());\n"
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n"
+ << outdent << "}\n";
}
return;
}
// PySide-535: Allow for empty dict instead of nullptr in PyPy
- s << "if (kwds && PyDict_Size(kwds) > 0) {\n";
- {
- Indentation indent(s);
- s << "PyObject *value{};\n"
- << "Shiboken::AutoDecRef kwds_dup(PyDict_Copy(kwds));\n";
- for (const AbstractMetaArgument &arg : args) {
- const int pyArgIndex = arg.argumentIndex()
- - OverloadData::numberOfRemovedArguments(func, arg.argumentIndex());
- QString pyArgName = usePyArgs ? pythonArgsAt(pyArgIndex)
- : PYTHON_ARG;
- QString pyKeyName = u"key_"_s + arg.name();
- s << "static PyObject *const " << pyKeyName
- << " = Shiboken::String::createStaticString(\"" << arg.name() << "\");\n"
- << "if (PyDict_Contains(kwds, " << pyKeyName << ")) {\n";
- {
- Indentation indent(s);
- s << "value = PyDict_GetItem(kwds, " << pyKeyName << ");\n"
- << "if (value && " << pyArgName << ") {\n";
- {
- Indentation indent(s);
- s << "errInfo.reset(" << pyKeyName << ");\n"
- << "Py_INCREF(errInfo.object());\n"
- << "goto " << cpythonFunctionName(func) << "_TypeError;\n";
- }
- s << "}\nif (value) {\n";
- {
- Indentation indent(s);
- s << pyArgName << " = value;\nif (!";
- const auto &type = arg.modifiedType();
- writeTypeCheck(s, type, pyArgName, isNumber(type.typeEntry()), {});
- s << ")\n";
- {
- Indentation indent(s);
- s << "goto " << cpythonFunctionName(func) << "_TypeError;\n";
- }
- }
- s << "}\nPyDict_DelItem(kwds_dup, " << pyKeyName << ");\n";
- }
- s << "}\n";
- }
- // PYSIDE-1305: Handle keyword args correctly.
- // Normal functions handle their parameters immediately.
- // For constructors that are QObject, we need to delay that
- // until extra keyword signals and properties are handled.
- s << "if (PyDict_Size(kwds_dup) > 0) {\n";
- {
- Indentation indent(s);
- s << "errInfo.reset(kwds_dup.release());\n";
- if (!(func->isConstructor() && func->ownerClass()->isQObject()))
- s << "goto " << cpythonFunctionName(func) << "_TypeError;\n";
- else
- s << "// fall through to handle extra keyword signals and properties\n";
- }
- s << "}\n";
- }
- s << "}\n";
+ s << "if (kwds && PyDict_Size(kwds) > 0) {\n" << indent;
+ if (!force)
+ s << "PyObject *value{};\n";
+ s << "Shiboken::AutoDecRef kwds_dup(PyDict_Copy(kwds));\n";
+ for (const AbstractMetaArgument &arg : args) {
+ const int pyArgIndex = arg.argumentIndex()
+ - OverloadData::numberOfRemovedArguments(func, arg.argumentIndex());
+ QString pyArgName = usePyArgs ? pythonArgsAt(pyArgIndex)
+ : PYTHON_ARG;
+ QString pyKeyName = u"key_"_s + arg.name();
+ s << "static PyObject *const " << pyKeyName
+ << " = Shiboken::String::createStaticString(\"" << arg.name() << "\");\n"
+ << "if (PyDict_Contains(kwds, " << pyKeyName << ") != 0) {\n" << indent
+ << "value = PyDict_GetItem(kwds, " << pyKeyName << ");\n"
+ << "if (value != nullptr && " << pyArgName << " != nullptr ) {\n"
+ << indent << "errInfo.reset(" << pyKeyName << ");\n"
+ << "Py_INCREF(errInfo.object());\n"
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n"
+ << outdent << "}\nif (value != nullptr) {\n" << indent
+ << pyArgName << " = value;\nif (!";
+ const auto &type = arg.modifiedType();
+ writeTypeCheck(s, type, pyArgName, isNumber(type.typeEntry()), {});
+ s << ")\n" << indent
+ << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n"
+ << outdent << outdent
+ << "}\nPyDict_DelItem(kwds_dup, " << pyKeyName << ");\n"
+ << outdent << "}\n";
+ }
+ // PYSIDE-1305: Handle keyword args correctly.
+ // Normal functions handle their parameters immediately.
+ // For constructors that are QObject, we need to delay that
+ // until extra keyword signals and properties are handled.
+ s << "if (PyDict_Size(kwds_dup) > 0) {\n" << indent
+ << "errInfo.reset(kwds_dup.release());\n";
+ if (!(func->isConstructor() && isQObject(func->ownerClass())))
+ s << "return " << returnErrorWrongArguments(overloadData, errorReturn) << ";\n";
+ else
+ s << "// fall through to handle extra keyword signals and properties\n";
+ s << outdent << "}\n"
+ << outdent << "}\n";
}
QString CppGenerator::argumentNameFromIndex(const ApiExtractorResult &api,
@@ -3645,7 +3573,7 @@ QString CppGenerator::argumentNameFromIndex(const ApiExtractorResult &api,
{
switch (argIndex) {
case -1:
- return u"self"_s;
+ return PYTHON_SELF_VAR;
case 0:
return PYTHON_RETURN_VAR;
case 1: { // Single argument?
@@ -3658,7 +3586,7 @@ QString CppGenerator::argumentNameFromIndex(const ApiExtractorResult &api,
return pythonArgsAt(argIndex - 1);
}
-const AbstractMetaClass *
+AbstractMetaClassCPtr
CppGenerator::argumentClassFromIndex(const ApiExtractorResult &api,
const AbstractMetaFunctionCPtr &func, int argIndex)
{
@@ -3680,25 +3608,45 @@ CppGenerator::argumentClassFromIndex(const ApiExtractorResult &api,
type = type.instantiations().constFirst();
}
- auto *te = type.typeEntry();
+ auto te = type.typeEntry();
if (type.isVoid() || !te->isComplex())
throw Exception(msgInvalidArgumentModification(func, argIndex));
- auto *result = AbstractMetaClass::findClass(api.classes(), te);
+ const auto result = AbstractMetaClass::findClass(api.classes(), te);
if (!result)
throw Exception(msgClassNotFound(te));
return result;
}
+const char tryBlock[] = R"(
+PyObject *errorType{};
+PyObject *errorString{};
+try {
+)";
+
const char defaultExceptionHandling[] = R"(} catch (const std::exception &e) {
- PyErr_SetString(PyExc_RuntimeError, e.what());
+ errorType = PyExc_RuntimeError;
+ errorString = Shiboken::String::fromCString(e.what());
} catch (...) {
- PyErr_SetString(PyExc_RuntimeError, "An unknown exception was caught");
+ errorType = PyExc_RuntimeError;
+ errorString = Shiboken::Messages::unknownException();
}
)";
+const char propagateException[] = R"(
+if (errorType != nullptr)
+ PyErr_SetObject(errorType, errorString);
+)";
+
+static QString explicitConversion(const QString &v, const AbstractMetaType &t)
+{
+ return t.plainType().cppSignature() + u'(' + v + u')';
+}
+
void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr &func,
const GeneratorContext &context, bool usesPyArgs,
- int maxArgs, ErrorReturn errorReturn) const
+ int maxArgs,
+ const QList<qsizetype> &argumentIndirections,
+ ErrorReturn errorReturn) const
{
s << "// " << func->minimalSignature() << (func->isReverseOperator() ? " [reverse operator]": "") << '\n';
if (func->isConstructor()) {
@@ -3747,6 +3695,8 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
writeConversionRule(s, func, TypeSystem::NativeCode, usesPyArgs);
+ bool generateExceptionHandling = false;
+
if (!func->isUserAdded()) {
QStringList userArgs;
if (func->functionType() != AbstractMetaFunction::CopyConstructorFunction) {
@@ -3765,24 +3715,24 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
if (hasConversionRule)
userArgs << arg.name() + CONV_RULE_OUT_VAR_SUFFIX;
else if (!arg.defaultValueExpression().isEmpty())
- userArgs.append(CPP_ARG_REMOVED + QString::number(i));
+ userArgs.append(CPP_ARG_REMOVED(i));
} else {
if (hasConversionRule) {
userArgs.append(arg.name() + CONV_RULE_OUT_VAR_SUFFIX);
} else {
const int idx = arg.argumentIndex() - removedArgs;
- const auto deRef = arg.type().shouldDereferenceArgument();
- QString argName;
- if (deRef > 0)
- argName += QString(deRef, u'*');
- argName += CPP_ARG + QString::number(idx);
+ const auto deRef = argumentIndirections.at(i);
+ QString argName = AbstractMetaType::dereferencePrefix(deRef)
+ + CPP_ARG_N(idx);
userArgs.append(argName);
}
}
// "Pass unique ptr by value" pattern: Apply std::move()
auto type = arg.type();
- if (type.isUniquePointer() && type.passByValue())
+ if (type.useStdMove())
userArgs.last() = stdMove(userArgs.constLast());
+ else if (type.viewOn() != nullptr)
+ userArgs.last() = explicitConversion(userArgs.constLast(), type);
}
// If any argument's default value was modified the method must be called
@@ -3792,7 +3742,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
QStringList otherArgs;
bool otherArgsModified = false;
bool argsClear = true;
- for (int i = func->arguments().size() - 1; i >= maxArgs + removedArgs; i--) {
+ for (auto i = func->arguments().size() - 1; i >= maxArgs + removedArgs; i--) {
const AbstractMetaArgument &arg = func->arguments().at(i);
const bool defValModified = arg.hasModifiedDefaultValueExpression();
const bool hasConversionRule =
@@ -3804,7 +3754,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
if (hasConversionRule)
otherArgs.prepend(arg.name() + CONV_RULE_OUT_VAR_SUFFIX);
else
- otherArgs.prepend(CPP_ARG_REMOVED + QString::number(i));
+ otherArgs.prepend(CPP_ARG_REMOVED(i));
}
if (otherArgsModified)
userArgs << otherArgs;
@@ -3821,10 +3771,8 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
firstArg += CPP_SELF_VAR;
firstArg += u')';
QString secondArg = CPP_ARG0;
- if (!func->isUnaryOperator()) {
- auto deRef = func->arguments().constFirst().type().shouldDereferenceArgument();
- AbstractMetaType::applyDereference(&secondArg, deRef);
- }
+ if (!func->isUnaryOperator())
+ AbstractMetaType::applyDereference(&secondArg, argumentIndirections.at(0));
if (func->isUnaryOperator())
std::swap(firstArg, secondArg);
@@ -3854,29 +3802,21 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
Q_ASSERT(owner == context.metaClass());
if (func->functionType() == AbstractMetaFunction::CopyConstructorFunction
&& maxArgs == 1) {
- mc << "new ::" << context.effectiveClassName()
+ mc << "new " << globalScopePrefix(context) << context.effectiveClassName()
<< "(*" << CPP_ARG0 << ')';
} else {
const QString ctorCall = context.effectiveClassName() + u'('
+ userArgs.join(u", "_s) + u')';
- if (usePySideExtensions() && owner->isQObject()) {
+ if (usePySideExtensions() && isQObject(owner)) {
s << "void *addr = PySide::nextQObjectMemoryAddr();\n";
- uva << "if (addr) {\n";
- {
- Indentation indent(uva);
-
- uva << "cptr = new (addr) ::" << ctorCall << ";\n"
- << "PySide::setNextQObjectMemoryAddr(nullptr);"
- << '\n';
- }
- uva << "} else {\n";
- {
- Indentation indent(uva);
- uva << "cptr = new ::" << ctorCall << ";\n";
- }
- uva << "}\n";
+ uva << "if (addr != nullptr) {\n" << indent
+ << "cptr = new (addr) " << globalScopePrefix(context) << ctorCall
+ << ";\nPySide::setNextQObjectMemoryAddr(nullptr);\n" << outdent
+ << "} else {\n" << indent
+ << "cptr = new " << globalScopePrefix(context) << ctorCall << ";\n"
+ << outdent << "}\n";
} else {
- mc << "new ::" << ctorCall;
+ mc << "new " << globalScopePrefix(context) << ctorCall;
}
}
} else {
@@ -3890,7 +3830,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
const bool hasWrapper = shouldGenerateCppWrapper(ownerClass);
if (!avoidProtectedHack() || !func->isProtected() || !hasWrapper) {
if (func->isStatic()) {
- mc << "::" << methodCallClassName << "::";
+ mc << m_gsp << methodCallClassName << "::";
} else {
const QString cppSelfVar = CPP_SELF_VAR;
const QString selfVarCast = func->ownerClass() == func->implementingClass()
@@ -3899,7 +3839,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
+ u" *>("_s + cppSelfVar + u')';
if (func->isConstant()) {
if (avoidProtectedHack()) {
- mc << "const_cast<const ::";
+ mc << "const_cast<const " << globalScopePrefix(context);
if (ownerClass->cppWrapper().testFlag(AbstractMetaClass::CppProtectedHackWrapper)) {
// PYSIDE-500: Need a special wrapper cast when inherited
const QString selfWrapCast = ownerClass == func->implementingClass()
@@ -3914,7 +3854,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
mc << " *>(" << selfVarCast << ")->";
}
} else {
- mc << "const_cast<const ::" << methodCallClassName;
+ mc << "const_cast<const " << m_gsp << methodCallClassName;
mc << " *>(" << selfVarCast << ")->";
}
} else {
@@ -3930,13 +3870,13 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
if (!func->isStatic()) {
const bool directInheritance = context.metaClass() == ownerClass;
mc << (directInheritance ? "static_cast" : "reinterpret_cast")
- << "<::" << wrapperName(ownerClass) << " *>(" << CPP_SELF_VAR << ")->";
+ << '<' << wrapperName(ownerClass) << " *>("
+ << CPP_SELF_VAR << ")->";
}
if (!func->isAbstract())
mc << (func->isProtected() ? wrapperName(func->ownerClass()) :
- u"::"_s
- + methodCallClassName) << "::";
+ m_gsp + methodCallClassName) << "::";
mc << func->originalName() << "_protected";
}
} else {
@@ -3961,9 +3901,9 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
if (!injectedCodeCallsCppFunction(context, func)) {
const bool allowThread = func->allowThread();
- const bool generateExceptionHandling = func->generateExceptionHandling();
+ generateExceptionHandling = func->generateExceptionHandling();
if (generateExceptionHandling) {
- s << "try {\n" << indent;
+ s << tryBlock << indent;
if (allowThread) {
s << "Shiboken::ThreadStateSaver threadSaver;\n"
<< "threadSaver.save();\n";
@@ -4043,8 +3983,8 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
if (generateExceptionHandling) { // "catch" code
s << outdent << defaultExceptionHandling;
}
- }
- }
+ } // !injected code calls C++ function
+ } // !userAdded
if (func->hasInjectedCode() && !func->isConstructor())
writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd,
@@ -4071,7 +4011,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
if (!ownership_mods.isEmpty()) {
s << '\n' << "// Ownership transferences.\n";
- for (const ArgumentModification &arg_mod : qAsConst(ownership_mods)) {
+ for (const ArgumentModification &arg_mod : std::as_const(ownership_mods)) {
const int argIndex = arg_mod.index();
const QString pyArgName = argumentNameFromIndex(api(), func, argIndex);
@@ -4087,8 +4027,8 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
s << "Shiboken::Object::";
if (ownership == TypeSystem::TargetLangOwnership) {
s << "getOwnership(" << pyArgName << ");";
- } else if (auto *ac = argumentClassFromIndex(api(), func, argIndex);
- ac->hasVirtualDestructor()) {
+ } else if (auto ac = argumentClassFromIndex(api(), func, argIndex);
+ ac && ac->hasVirtualDestructor()) {
s << "releaseOwnership(" << pyArgName << ");";
} else {
s << "invalidate(" << pyArgName << ");";
@@ -4097,7 +4037,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
}
} else if (!refcount_mods.isEmpty()) {
- for (const ArgumentModification &arg_mod : qAsConst(refcount_mods)) {
+ for (const ArgumentModification &arg_mod : std::as_const(refcount_mods)) {
ReferenceCount refCount = arg_mod.referenceCounts().constFirst();
if (refCount.action != ReferenceCount::Set
&& refCount.action != ReferenceCount::Remove
@@ -4128,14 +4068,17 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr
}
}
writeParentChildManagement(s, func, usesPyArgs, !hasReturnPolicy);
+
+ if (generateExceptionHandling)
+ s << propagateException;
}
-QStringList CppGenerator::getAncestorMultipleInheritance(const AbstractMetaClass *metaClass)
+QStringList CppGenerator::getAncestorMultipleInheritance(const AbstractMetaClassCPtr &metaClass)
{
QStringList result;
const auto &baseClases = metaClass->typeSystemBaseClasses();
if (!baseClases.isEmpty()) {
- for (const AbstractMetaClass *baseClass : baseClases) {
+ for (const auto &baseClass : baseClases) {
QString offset;
QTextStream(&offset) << "reinterpret_cast<uintptr_t>(static_cast<const "
<< baseClass->qualifiedCppName() << " *>(class_ptr)) - base";
@@ -4148,255 +4091,203 @@ QStringList CppGenerator::getAncestorMultipleInheritance(const AbstractMetaClass
result.append(offset);
}
- for (const AbstractMetaClass *baseClass : baseClases)
+ for (const auto &baseClass : baseClases)
result.append(getAncestorMultipleInheritance(baseClass));
}
return result;
}
-void CppGenerator::writeMultipleInheritanceInitializerFunction(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::writeMultipleInheritanceInitializerFunction(TextStream &s,
+ const AbstractMetaClassCPtr &metaClass)
{
QString className = metaClass->qualifiedCppName();
const QStringList ancestors = getAncestorMultipleInheritance(metaClass);
- s << "static int mi_offsets[] = { ";
- for (int i = 0; i < ancestors.size(); i++)
- s << "-1, ";
- s << "-1 };\n"
- << "int *\n"
+ s << "int *\n"
<< multipleInheritanceInitializerFunctionName(metaClass) << "(const void *cptr)\n"
- << "{\n" << indent
- << "if (mi_offsets[0] == -1) {\n";
- {
- Indentation indent(s);
- s << "std::set<int> offsets;\n"
- << "const auto *class_ptr = reinterpret_cast<const " << className << " *>(cptr);\n"
- << "const auto base = reinterpret_cast<uintptr_t>(class_ptr);\n";
-
- for (const QString &ancestor : ancestors)
- s << "offsets.insert(int(" << ancestor << "));\n";
-
- s << "\noffsets.erase(0);\n\n"
- << "std::copy(offsets.cbegin(), offsets.cend(), mi_offsets);\n";
- }
- s << "}\nreturn mi_offsets;\n" << outdent << "}\n";
-}
-
-void CppGenerator::writeSpecialCastFunction(TextStream &s, const AbstractMetaClass *metaClass)
+ << "{\n" << indent;
+ s << "static int mi_offsets[] = {-2";
+ for (qsizetype i = 0; i < ancestors.size(); i++)
+ s << ", 0";
+ s << "};\n"
+ << "if (mi_offsets[0] == -2) {\n" << indent
+ << "const auto *class_ptr = reinterpret_cast<const " << className << " *>(cptr);\n"
+ << "const auto base = reinterpret_cast<uintptr_t>(class_ptr);\n"
+ << "int *p = mi_offsets;\n";
+
+ for (const QString &ancestor : ancestors)
+ s << "*p++ = int(" << ancestor << ");\n";
+ s << "std::sort(mi_offsets, p);\n"
+ << "auto *end = std::unique(mi_offsets, p);\n"
+ << "*end++ = -1;\n"
+ << "if (mi_offsets[0] == 0)\n"
+ << indent
+ << "std::memmove(&mi_offsets[0], &mi_offsets[1], (end - mi_offsets - 1) * sizeof(int));\n"
+ << outdent << outdent
+ << "}\nreturn mi_offsets;\n" << outdent << "}\n";
+}
+
+void CppGenerator::writeSpecialCastFunction(TextStream &s, const AbstractMetaClassCPtr &metaClass)
{
QString className = metaClass->qualifiedCppName();
s << "static void * " << cpythonSpecialCastFunctionName(metaClass)
<< "(void *obj, PyTypeObject *desiredType)\n{\n" << indent
- << "auto me = reinterpret_cast< ::" << className << " *>(obj);\n";
+ << "auto me = reinterpret_cast< " << m_gsp << className << " *>(obj);\n";
bool firstClass = true;
const auto &allAncestors = metaClass->allTypeSystemAncestors();
- for (const AbstractMetaClass *baseClass : allAncestors) {
+ for (const auto &baseClass : allAncestors) {
if (!firstClass)
s << "else ";
- s << "if (desiredType == " << cpythonTypeNameExt(baseClass->typeEntry()) << ")\n";
- Indentation indent(s);
- s << "return static_cast< ::" << baseClass->qualifiedCppName() << " *>(me);\n";
+ s << "if (desiredType == " << cpythonTypeNameExt(baseClass->typeEntry())
+ << ")\n" << indent
+ << "return static_cast< " << getFullTypeName(baseClass) << " *>(me);\n"
+ << outdent;
firstClass = false;
}
s << "return me;\n" << outdent << "}\n\n";
}
void CppGenerator::writePrimitiveConverterInitialization(TextStream &s,
- const CustomConversion *customConversion)
+ const CustomConversionPtr &customConversion)
{
- const TypeEntry *type = customConversion->ownerType();
+ TypeEntryCPtr type = customConversion->ownerType();
QString converter = converterObject(type);
s << "// Register converter for type '" << type->qualifiedTargetLangName() << "'.\n"
<< converter << " = Shiboken::Conversions::createConverter(";
if (!type->hasTargetLangApiType())
s << "nullptr";
- else if (type->targetLangApiName() == cPyObjectT())
+ else if (type->targetLangApiName() == cPyObjectT)
s << "&PyBaseObject_Type";
else
s << '&' << type->targetLangApiName() << "_Type";
QString typeName = fixedCppTypeName(type);
s << ", " << cppToPythonFunctionName(typeName, typeName) << ");\n"
- << "Shiboken::Conversions::registerConverterName(" << converter << ", \""
- << type->qualifiedCppName() << "\");\n";
+ << registerConverterName(type->qualifiedCppName(), converter);
writeCustomConverterRegister(s, customConversion, converter);
}
-void CppGenerator::writeEnumConverterInitialization(TextStream &s, const AbstractMetaEnum &metaEnum)
+static void registerConverterInScopes(TextStream &s, QStringView signature,
+ QAnyStringView varName = converterVar)
{
- if (metaEnum.isPrivate() || metaEnum.isAnonymous())
- return;
- writeEnumConverterInitialization(s, metaEnum.typeEntry());
+ while (true) {
+ s << registerConverterName(signature, varName);
+ const auto qualifierPos = signature.indexOf("::"_L1);
+ if (qualifierPos == -1)
+ break;
+ signature = signature.sliced(qualifierPos + 2);
+ }
}
-void CppGenerator::writeEnumConverterInitialization(TextStream &s, const TypeEntry *enumType)
+void CppGenerator::writeEnumConverterInitialization(TextStream &s, const AbstractMetaEnum &metaEnum)
{
- if (!enumType)
+ if (metaEnum.isPrivate() || metaEnum.isAnonymous())
return;
- QString enumFlagName = enumType->isFlags() ? u"flag"_s : u"enum"_s;
- QString enumPythonVar = enumType->isFlags() ? u"FType"_s : u"EType"_s;
-
- const FlagsTypeEntry *flags = nullptr;
- if (enumType->isFlags())
- flags = static_cast<const FlagsTypeEntry *>(enumType);
+ EnumTypeEntryCPtr enumType = metaEnum.typeEntry();
+ Q_ASSERT(enumType);
- s << "// Register converter for " << enumFlagName << " '" << enumType->qualifiedCppName()
- << "'.\n{\n";
- {
- Indentation indent(s);
- QString typeName = fixedCppTypeName(enumType);
- s << "SbkConverter *converter = Shiboken::Conversions::createConverter("
- << enumPythonVar << ',' << '\n';
- {
- Indentation indent(s);
- s << cppToPythonFunctionName(typeName, typeName) << ");\n";
- }
+ static const char enumPythonVar[] = "EType";
- if (flags) {
- QString enumTypeName = fixedCppTypeName(flags->originator());
- QString toCpp = pythonToCppFunctionName(enumTypeName, typeName);
- QString isConv = convertibleToCppFunctionName(enumTypeName, typeName);
- writeAddPythonToCppConversion(s, u"converter"_s, toCpp, isConv);
- }
+ s << "// Register converter for enum '" << enumType->qualifiedCppName()
+ << "'.\n{\n" << indent;
- QString toCpp = pythonToCppFunctionName(typeName, typeName);
- QString isConv = convertibleToCppFunctionName(typeName, typeName);
- writeAddPythonToCppConversion(s, u"converter"_s, toCpp, isConv);
+ const QString typeName = fixedCppTypeName(enumType);
+ s << "SbkConverter *converter = Shiboken::Conversions::createConverter("
+ << enumPythonVar << ',' << '\n' << indent
+ << cppToPythonFunctionName(typeName, typeName) << ");\n" << outdent;
- if (flags) {
- QString toCpp = pythonToCppFunctionName(u"number"_s, typeName);
- QString isConv = convertibleToCppFunctionName(u"number"_s, typeName);
- writeAddPythonToCppConversion(s, u"converter"_s, toCpp, isConv);
- }
-
- s << "Shiboken::Enum::setTypeConverter(" << enumPythonVar
- << ", converter, " << (enumType->isFlags() ? "true" : "false") << ");\n";
-
- QString signature = enumType->qualifiedCppName();
- // Replace "QFlags<Class::Option>" by "Class::Options"
- if (flags && signature.startsWith(u"QFlags<") && signature.endsWith(u'>')) {
- signature.chop(1);
- signature.remove(0, 7);
- const int lastQualifierPos = signature.lastIndexOf(u"::");
- if (lastQualifierPos != -1) {
- signature.replace(lastQualifierPos + 2, signature.size() - lastQualifierPos - 2,
- flags->flagsName());
- } else {
- signature = flags->flagsName();
- }
- }
-
- while (true) {
- s << "Shiboken::Conversions::registerConverterName(converter, \""
- << signature << "\");\n";
- const int qualifierPos = signature.indexOf(u"::");
- if (qualifierPos != -1)
- signature.remove(0, qualifierPos + 2);
- else
- break;
- }
- if (flags) {
- // PYSIDE-1673: Also register "QFlags<Class::Option>" purely for
- // the purpose of finding the converter by QVariant::typeName()
- // in the QVariant conversion code.
- s << "Shiboken::Conversions::registerConverterName(converter, \""
- << flags->name() << "\");\n";
- }
+ const QString toCpp = pythonToCppFunctionName(typeName, typeName);
+ const QString isConv = convertibleToCppFunctionName(typeName, typeName);
+ writeAddPythonToCppConversion(s, u"converter"_s, toCpp, isConv);
+ s << "Shiboken::Enum::setTypeConverter(" << enumPythonVar
+ << ", converter);\n";
- }
- s << "}\n";
+ registerConverterInScopes(s, enumType->qualifiedCppName());
+ if (auto flags = enumType->flags())
+ s << "// Register converter for flag '" << flags->qualifiedCppName() << "'.\n"
+ << registerConverterName(flags->name()) // QMetaType
+ << registerConverterName(flags->originalName()); // Signals with flags
- if (!flags)
- writeEnumConverterInitialization(s, static_cast<const EnumTypeEntry *>(enumType)->flags());
+ s << outdent << "}\n";
}
-QString CppGenerator::writeContainerConverterInitialization(TextStream &s, const AbstractMetaType &type) const
+QString CppGenerator::writeContainerConverterInitialization(TextStream &s,
+ const AbstractMetaType &type,
+ const ApiExtractorResult &api)
{
- QByteArray cppSignature = QMetaObject::normalizedSignature(type.cppSignature().toUtf8());
+ const auto cppSignature =
+ QString::fromUtf8(QMetaObject::normalizedSignature(type.cppSignature().toUtf8()));
s << "// Register converter for type '" << cppSignature << "'.\n";
- QString converter = converterObject(type);
+ const QString converter = converterObject(type);
s << converter << " = Shiboken::Conversions::createConverter(";
- if (type.typeEntry()->targetLangApiName() == cPyObjectT()) {
+
+ Q_ASSERT(type.typeEntry()->isContainer());
+ const auto typeEntry = std::static_pointer_cast<const ContainerTypeEntry>(type.typeEntry());
+
+ const QString targetTypeName = containerNativeToTargetTypeName(typeEntry);
+ if (targetTypeName == cPyObjectT) {
s << "&PyBaseObject_Type";
} else {
- QString baseName = cpythonBaseName(type.typeEntry());
- if (baseName == cPySequenceT())
- baseName = cPyListT();
- s << '&' << baseName << "_Type";
+ s << '&' << targetTypeName << "_Type";
}
- QString typeName = fixedCppTypeName(type);
- s << ", " << cppToPythonFunctionName(typeName, typeName) << ");\n";
- QString toCpp = pythonToCppFunctionName(typeName, typeName);
- QString isConv = convertibleToCppFunctionName(typeName, typeName);
- s << "Shiboken::Conversions::registerConverterName(" << converter << ", \"" << cppSignature << "\");\n";
- if (usePySideExtensions() && cppSignature.startsWith("const ") && cppSignature.endsWith("&")) {
- cppSignature.chop(1);
- cppSignature.remove(0, sizeof("const ") / sizeof(char) - 1);
- s << "Shiboken::Conversions::registerConverterName(" << converter << ", \"" << cppSignature << "\");\n";
- }
- const QString converterObj = converterObject(type);
- writeAddPythonToCppConversion(s, converterObj, toCpp, isConv);
- return converterObj;
-}
-
-void CppGenerator::writeSmartPointerConverterInitialization(TextStream &s, const AbstractMetaType &type) const
-{
- const QByteArray cppSignature = type.cppSignature().toUtf8();
- auto writeConversionRegister = [&s](const AbstractMetaType &sourceType, const QString &targetTypeName, const QString &targetConverter)
- {
- const QString sourceTypeName = fixedCppTypeName(sourceType);
- const QString toCpp = pythonToCppFunctionName(sourceTypeName, targetTypeName);
- const QString isConv = convertibleToCppFunctionName(sourceTypeName, targetTypeName);
- writeAddPythonToCppConversion(s, targetConverter, toCpp, isConv);
- };
-
- const auto classes = findSmartPointeeBaseClasses(api(), type);
- if (classes.isEmpty())
- return;
+ const QString typeName = fixedCppTypeName(type);
+ s << ", " << cppToPythonFunctionName(typeName, targetTypeName) << ");\n";
- auto *smartPointerTypeEntry = static_cast<const SmartPointerTypeEntry *>(type.typeEntry());
+ s << registerConverterName(cppSignature, converter);
+ if (usePySideExtensions() && cppSignature.startsWith("const "_L1)
+ && cppSignature.endsWith(u'&')) {
+ auto underlyingType = QStringView{cppSignature}.sliced(6, cppSignature.size() - 7);
+ s << registerConverterName(underlyingType, converter);
+ }
- s << "// Register SmartPointer converter for type '" << cppSignature << "'." << '\n'
- << "///////////////////////////////////////////////////////////////////////////////////////\n\n";
+ for (const auto &conv : typeEntry->customConversion()->targetToNativeConversions()) {
+ const QString &sourceTypeName = conv.sourceTypeName();
+ QString toCpp = pythonToCppFunctionName(sourceTypeName, typeName);
+ QString isConv = convertibleToCppFunctionName(sourceTypeName, typeName);
+ writeAddPythonToCppConversion(s, converter, toCpp, isConv);
+ }
- for (auto *base : classes) {
- auto *baseTe = base->typeEntry();
- if (auto opt = findSmartPointerInstantiation(smartPointerTypeEntry, baseTe)) {
- const auto smartTargetType = opt.value();
- s << "// Convert to SmartPointer derived class: ["
- << smartTargetType.cppSignature() << "]\n";
- const QString converter = u"Shiboken::Conversions::getConverter(\""_s
- + smartTargetType.cppSignature() + u"\")"_s;
- writeConversionRegister(type, fixedCppTypeName(smartTargetType), converter);
- } else {
- s << "// Class not found:" << type.instantiations().at(0).cppSignature();
+ auto typedefItPair = api.typedefTargetToName().equal_range(type.cppSignature());
+ if (typedefItPair.first != typedefItPair.second) {
+ auto *typeDb = TypeDatabase::instance();
+ s << "// Register converters for type aliases of " << cppSignature << "'.\n";
+ for (auto it = typedefItPair.first; it != typedefItPair.second; ++it) {
+ if (!typeDb->findType(it.value()))
+ s << registerConverterName(it.value(), converter);
}
}
- s << "///////////////////////////////////////////////////////////////////////////////////////" << '\n' << '\n';
+ return converter;
}
-void CppGenerator::writeExtendedConverterInitialization(TextStream &s, const TypeEntry *externalType,
+QString CppGenerator::typeInitStruct(const TypeEntryCPtr &te)
+{
+ return cppApiVariableName(te->targetLangPackage()) + u'['
+ + getTypeIndexVariableName(te) + u']';
+}
+
+void CppGenerator::writeExtendedConverterInitialization(TextStream &s,
+ const TypeEntryCPtr &externalType,
const AbstractMetaClassCList &conversions)
{
s << "// Extended implicit conversions for " << externalType->qualifiedTargetLangName()
<< ".\n";
- for (const AbstractMetaClass *sourceClass : conversions) {
- const QString converterVar = cppApiVariableName(externalType->targetLangPackage()) + u'['
- + getTypeIndexVariableName(externalType) + u']';
+ for (const auto &sourceClass : conversions) {
QString sourceTypeName = fixedCppTypeName(sourceClass->typeEntry());
QString targetTypeName = fixedCppTypeName(externalType);
QString toCpp = pythonToCppFunctionName(sourceTypeName, targetTypeName);
QString isConv = convertibleToCppFunctionName(sourceTypeName, targetTypeName);
- writeAddPythonToCppConversion(s, converterVar, toCpp, isConv);
+ if (!externalType->isPrimitive())
+ s << cpythonTypeNameExt(externalType) << ";\n";
+ writeAddPythonToCppConversion(s, typeInitStruct(externalType), toCpp, isConv);
}
}
-QString CppGenerator::multipleInheritanceInitializerFunctionName(const AbstractMetaClass *metaClass)
+QString CppGenerator::multipleInheritanceInitializerFunctionName(const AbstractMetaClassCPtr &metaClass)
{
return cpythonBaseName(metaClass->typeEntry()) + u"_mi_init"_s;
}
-bool CppGenerator::supportsMappingProtocol(const AbstractMetaClass *metaClass)
+bool CppGenerator::supportsMappingProtocol(const AbstractMetaClassCPtr &metaClass)
{
for (const auto &m : mappingProtocols()) {
if (metaClass->hasFunction(m.name))
@@ -4406,7 +4297,7 @@ bool CppGenerator::supportsMappingProtocol(const AbstractMetaClass *metaClass)
return false;
}
-bool CppGenerator::supportsNumberProtocol(const AbstractMetaClass *metaClass) const
+bool CppGenerator::supportsNumberProtocol(const AbstractMetaClassCPtr &metaClass)
{
return metaClass->hasArithmeticOperatorOverload()
|| metaClass->hasIncDecrementOperatorOverload()
@@ -4415,18 +4306,18 @@ bool CppGenerator::supportsNumberProtocol(const AbstractMetaClass *metaClass) co
|| hasBoolCast(metaClass);
}
-bool CppGenerator::supportsSequenceProtocol(const AbstractMetaClass *metaClass)
+bool CppGenerator::supportsSequenceProtocol(const AbstractMetaClassCPtr &metaClass)
{
for (const auto &seq : sequenceProtocols()) {
if (metaClass->hasFunction(seq.name))
return true;
}
- const ComplexTypeEntry *baseType = metaClass->typeEntry()->baseContainerType();
+ ComplexTypeEntryCPtr baseType = metaClass->typeEntry()->baseContainerType();
return baseType && baseType->isContainer();
}
-bool CppGenerator::shouldGenerateGetSetList(const AbstractMetaClass *metaClass) const
+bool CppGenerator::shouldGenerateGetSetList(const AbstractMetaClassCPtr &metaClass)
{
for (const AbstractMetaField &f : metaClass->fields()) {
if (!f.isStatic())
@@ -4444,29 +4335,24 @@ bool CppGenerator::shouldGenerateGetSetList(const AbstractMetaClass *metaClass)
struct pyTypeSlotEntry
{
- explicit pyTypeSlotEntry(const char *name, const QString &function) :
+ explicit pyTypeSlotEntry(QAnyStringView name, QAnyStringView function) :
m_name(name), m_function(function) {}
- const char *m_name;
- const QString &m_function;
+ QAnyStringView m_name;
+ QAnyStringView m_function;
};
TextStream &operator<<(TextStream &str, const pyTypeSlotEntry &e)
{
- str << '{' << e.m_name << ',';
- const int padding = qMax(0, 18 - int(strlen(e.m_name)));
- for (int p = 0; p < padding; ++p)
- str << ' ';
- if (e.m_function.isEmpty())
- str << NULL_PTR;
- else
- str << "reinterpret_cast<void *>(" << e.m_function << ')';
- str << "},\n";
+ if (!e.m_function.isEmpty()) {
+ str << '{' << e.m_name << ',' << Pad(' ', qMax(0, 18 - e.m_name.size()))
+ << "reinterpret_cast<void *>(" << e.m_function << ")},\n";
+ }
return str;
}
void CppGenerator::writeClassDefinition(TextStream &s,
- const AbstractMetaClass *metaClass,
+ const AbstractMetaClassCPtr &metaClass,
const GeneratorContext &classContext)
{
QString tp_init;
@@ -4475,7 +4361,6 @@ void CppGenerator::writeClassDefinition(TextStream &s,
QString tp_hash;
QString tp_call;
const QString className = chopType(cpythonTypeName(metaClass));
- QString baseClassName;
AbstractMetaFunctionCList ctors;
const auto &allCtors = metaClass->queryFunctions(FunctionQueryOption::AnyConstructor);
for (const auto &f : allCtors) {
@@ -4485,13 +4370,10 @@ void CppGenerator::writeClassDefinition(TextStream &s,
}
}
- if (!metaClass->baseClass())
- baseClassName = u"SbkObject_TypeF()"_s;
-
bool onlyPrivCtor = !metaClass->hasNonPrivateConstructor();
const bool isQApp = usePySideExtensions()
- && metaClass->inheritsFrom(u"QCoreApplication"_s);
+ && inheritsFrom(metaClass, u"QCoreApplication"_s);
QString tp_flags = u"Py_TPFLAGS_DEFAULT"_s;
if (!metaClass->attributes().testFlag(AbstractMetaClass::FinalCppClass))
@@ -4543,25 +4425,31 @@ void CppGenerator::writeClassDefinition(TextStream &s,
if (generateRichComparison(classContext))
tp_richcompare = cpythonBaseName(metaClass) + u"_richcompare"_s;
+ const bool isSmartPointer = classContext.forSmartPointer();
QString tp_getset;
- if (shouldGenerateGetSetList(metaClass) && !classContext.forSmartPointer())
+ if (shouldGenerateGetSetList(metaClass) && !isSmartPointer)
tp_getset = cpythonGettersSettersDefinitionName(metaClass);
// search for special functions
clearTpFuncs();
for (const auto &func : metaClass->functions()) {
- if (m_tpFuncs.contains(func->name()))
- m_tpFuncs[func->name()] = cpythonFunctionName(func);
+ // Special non-operator functions identified by name
+ auto it = m_tpFuncs.find(func->name());
+ if (it != m_tpFuncs.end())
+ it.value() = cpythonFunctionName(func);
+ else if ( it = m_nbFuncs.find(func->name()); it != m_nbFuncs.end() )
+ it.value() = cpythonFunctionName(func);
}
- if (m_tpFuncs.value(reprFunction()).isEmpty()
- && metaClass->hasToStringCapability()) {
- m_tpFuncs[reprFunction()] = writeReprFunction(s,
- classContext,
- metaClass->toStringCapabilityIndirections());
+ if (m_tpFuncs.value(REPR_FUNCTION).isEmpty()
+ && (isSmartPointer || metaClass->hasToStringCapability())) {
+ const QString name = isSmartPointer
+ ? writeSmartPointerReprFunction(s, classContext)
+ : writeReprFunction(s, classContext, metaClass->toStringCapabilityIndirections());
+ m_tpFuncs[REPR_FUNCTION] = name;
}
// class or some ancestor has multiple inheritance
- const AbstractMetaClass *miClass = getMultipleInheritingClass(metaClass);
+ const auto miClass = getMultipleInheritingClass(metaClass);
if (miClass) {
if (metaClass == miClass)
writeMultipleInheritanceInitializerFunction(s, metaClass);
@@ -4575,8 +4463,8 @@ void CppGenerator::writeClassDefinition(TextStream &s,
if (hasHashFunction(metaClass))
tp_hash = u'&' + cpythonBaseName(metaClass) + u"_HashFunc"_s;
- const auto callOp = metaClass->findFunction(u"operator()");
- if (!callOp.isNull() && !callOp->isModifiedRemoved())
+ const auto callOp = metaClass->findFunction("operator()");
+ if (callOp && !callOp->isModifiedRemoved())
tp_call = u'&' + cpythonFunctionName(callOp);
const QString typePtr = u"_"_s + className
@@ -4587,7 +4475,7 @@ void CppGenerator::writeClassDefinition(TextStream &s,
<< "}\n\nstatic PyType_Slot " << className << "_slots[] = {\n" << indent
<< "{Py_tp_base, nullptr}, // inserted by introduceWrapperType\n"
<< pyTypeSlotEntry("Py_tp_dealloc", tp_dealloc)
- << pyTypeSlotEntry("Py_tp_repr", m_tpFuncs.value(reprFunction()))
+ << pyTypeSlotEntry("Py_tp_repr", m_tpFuncs.value(REPR_FUNCTION))
<< pyTypeSlotEntry("Py_tp_hash", tp_hash)
<< pyTypeSlotEntry("Py_tp_call", tp_call)
<< pyTypeSlotEntry("Py_tp_str", m_tpFuncs.value(u"__str__"_s))
@@ -4611,7 +4499,6 @@ void CppGenerator::writeClassDefinition(TextStream &s,
writeTypeAsMappingDefinition(s, metaClass);
}
if (supportsNumberProtocol(metaClass)) {
- // This one must come last. See the function itself.
s << "// type supports number protocol\n";
writeTypeAsNumberDefinition(s, metaClass);
}
@@ -4626,12 +4513,12 @@ void CppGenerator::writeClassDefinition(TextStream &s,
}
void CppGenerator::writeMappingMethods(TextStream &s,
- const AbstractMetaClass *metaClass,
+ const AbstractMetaClassCPtr &metaClass,
const GeneratorContext &context) const
{
for (const auto & m : mappingProtocols()) {
const auto func = metaClass->findFunction(m.name);
- if (func.isNull())
+ if (!func)
continue;
QString funcName = cpythonFunctionName(func);
CodeSnipList snips = func->injectedCodeSnips(TypeSystem::CodeSnipPositionAny, TypeSystem::TargetLangCode);
@@ -4648,14 +4535,14 @@ void CppGenerator::writeMappingMethods(TextStream &s,
}
void CppGenerator::writeSequenceMethods(TextStream &s,
- const AbstractMetaClass *metaClass,
+ const AbstractMetaClassCPtr &metaClass,
const GeneratorContext &context) const
{
bool injectedCode = false;
for (const auto &seq : sequenceProtocols()) {
const auto func = metaClass->findFunction(seq.name);
- if (func.isNull())
+ if (!func)
continue;
injectedCode = true;
QString funcName = cpythonFunctionName(func);
@@ -4679,25 +4566,25 @@ void CppGenerator::writeSequenceMethods(TextStream &s,
static const QHash<QString, QString> &sqFuncs()
{
static const QHash<QString, QString> result = {
- {u"__concat__"_s, u"sq_concat"_s},
- {u"__contains__"_s, u"sq_contains"_s},
- {u"__getitem__"_s, u"sq_item"_s},
- {u"__getslice__"_s, u"sq_slice"_s},
- {u"__len__"_s, u"sq_length"_s},
- {u"__setitem__"_s, u"sq_ass_item"_s},
- {u"__setslice__"_s, u"sq_ass_slice"_s}
+ {u"__concat__"_s, u"Py_sq_concat"_s},
+ {u"__contains__"_s, u"Py_sq_contains"_s},
+ {u"__getitem__"_s, u"Py_sq_item"_s},
+ {u"__getslice__"_s, u"Py_sq_slice"_s},
+ {u"__len__"_s, u"Py_sq_length"_s},
+ {u"__setitem__"_s, u"Py_sq_ass_item"_s},
+ {u"__setslice__"_s, u"Py_sq_ass_slice"_s}
};
return result;
}
void CppGenerator::writeTypeAsSequenceDefinition(TextStream &s,
- const AbstractMetaClass *metaClass)
+ const AbstractMetaClassCPtr &metaClass)
{
bool hasFunctions = false;
QMap<QString, QString> funcs;
for (const auto &seq : sequenceProtocols()) {
const auto func = metaClass->findFunction(seq.name);
- if (!func.isNull()) {
+ if (func) {
funcs.insert(seq.name, u'&' + cpythonFunctionName(func));
hasFunctions = true;
}
@@ -4715,38 +4602,34 @@ void CppGenerator::writeTypeAsSequenceDefinition(TextStream &s,
for (auto it = sqFuncs().cbegin(), end = sqFuncs().cend(); it != end; ++it) {
const QString &sqName = it.key();
auto fit = funcs.constFind(sqName);
- if (fit != funcs.constEnd()) {
- s << "{Py_" << it.value() << ", reinterpret_cast<void *>("
- << fit.value() << ")},\n";
- }
+ if (fit != funcs.constEnd())
+ s << pyTypeSlotEntry(it.value(), fit.value());
}
}
void CppGenerator::writeTypeAsMappingDefinition(TextStream &s,
- const AbstractMetaClass *metaClass)
+ const AbstractMetaClassCPtr &metaClass)
{
// Sequence protocol structure members names
static const QHash<QString, QString> mpFuncs{
- {u"__mlen__"_s, u"mp_length"_s},
- {u"__mgetitem__"_s, u"mp_subscript"_s},
- {u"__msetitem__"_s, u"mp_ass_subscript"_s},
+ {u"__mlen__"_s, u"Py_mp_length"_s},
+ {u"__mgetitem__"_s, u"Py_mp_subscript"_s},
+ {u"__msetitem__"_s, u"Py_mp_ass_subscript"_s},
};
QMap<QString, QString> funcs;
for (const auto &m : mappingProtocols()) {
const auto func = metaClass->findFunction(m.name);
- if (!func.isNull()) {
+ if (func) {
const QString entry = u"reinterpret_cast<void *>(&"_s
+ cpythonFunctionName(func) + u')';
funcs.insert(m.name, entry);
- } else {
- funcs.insert(m.name, NULL_PTR);
}
}
for (auto it = mpFuncs.cbegin(), end = mpFuncs.cend(); it != end; ++it) {
const auto fit = funcs.constFind(it.key());
if (fit != funcs.constEnd())
- s << "{Py_" << it.value() << ", " << fit.value() << "},\n";
+ s << pyTypeSlotEntry(it.value(), fit.value());
}
}
@@ -4754,105 +4637,102 @@ void CppGenerator::writeTypeAsMappingDefinition(TextStream &s,
static const QHash<QString, QString> &nbFuncs()
{
static const QHash<QString, QString> result = {
- {u"__add__"_s, u"nb_add"_s},
- {u"__sub__"_s, u"nb_subtract"_s},
- {u"__mul__"_s, u"nb_multiply"_s},
- {u"__div__"_s, u"nb_divide"_s},
- {u"__mod__"_s, u"nb_remainder"_s},
- {u"__neg__"_s, u"nb_negative"_s},
- {u"__pos__"_s, u"nb_positive"_s},
- {u"__invert__"_s, u"nb_invert"_s},
- {u"__lshift__"_s, u"nb_lshift"_s},
- {u"__rshift__"_s, u"nb_rshift"_s},
- {u"__and__"_s, u"nb_and"_s},
- {u"__xor__"_s, u"nb_xor"_s},
- {u"__or__"_s, u"nb_or"_s},
- {u"__iadd__"_s, u"nb_inplace_add"_s},
- {u"__isub__"_s, u"nb_inplace_subtract"_s},
- {u"__imul__"_s, u"nb_inplace_multiply"_s},
- {u"__idiv__"_s, u"nb_inplace_divide"_s},
- {u"__imod__"_s, u"nb_inplace_remainder"_s},
- {u"__ilshift__"_s, u"nb_inplace_lshift"_s},
- {u"__irshift__"_s, u"nb_inplace_rshift"_s},
- {u"__iand__"_s, u"nb_inplace_and"_s},
- {u"__ixor__"_s, u"nb_inplace_xor"_s},
- {u"__ior__"_s, u"nb_inplace_or"_s},
- {boolT(), u"nb_nonzero"_s}
+ {u"__abs__"_s, u"Py_nb_absolute"_s},
+ {u"__add__"_s, u"Py_nb_add"_s},
+ {u"__sub__"_s, u"Py_nb_subtract"_s},
+ {u"__mul__"_s, u"Py_nb_multiply"_s},
+ {u"__div__"_s, u"Py_nb_true_divide"_s},
+ {u"__mod__"_s, u"Py_nb_remainder"_s},
+ {u"__neg__"_s, u"Py_nb_negative"_s},
+ {u"__pos__"_s, u"Py_nb_positive"_s},
+ {u"__pow__"_s, u"Py_nb_power"_s},
+ {u"__invert__"_s, u"Py_nb_invert"_s},
+ {u"__lshift__"_s, u"Py_nb_lshift"_s},
+ {u"__rshift__"_s, u"Py_nb_rshift"_s},
+ {u"__and__"_s, u"Py_nb_and"_s},
+ {u"__xor__"_s, u"Py_nb_xor"_s},
+ {u"__or__"_s, u"Py_nb_or"_s},
+ {u"__iadd__"_s, u"Py_nb_inplace_add"_s},
+ {u"__isub__"_s, u"Py_nb_inplace_subtract"_s},
+ {u"__imul__"_s, u"Py_nb_inplace_multiply"_s},
+ {u"__imod__"_s, u"Py_nb_inplace_remainder"_s},
+ {u"__ilshift__"_s, u"Py_nb_inplace_lshift"_s},
+ {u"__irshift__"_s, u"Py_nb_inplace_rshift"_s},
+ {u"__iand__"_s, u"Py_nb_inplace_and"_s},
+ {u"__ixor__"_s, u"Py_nb_inplace_xor"_s},
+ {u"__ior__"_s, u"Py_nb_inplace_or"_s},
+ {u"__bool__"_s, u"Py_nb_bool"_s},
+ {u"__int__"_s, u"Py_nb_int"_s},
+ {u"__float__"_s, u"Py_nb_float"_s}
};
return result;
}
-void CppGenerator::writeTypeAsNumberDefinition(TextStream &s, const AbstractMetaClass *metaClass) const
+void CppGenerator::writeTypeAsNumberDefinition(TextStream &s, const AbstractMetaClassCPtr &metaClass) const
{
QMap<QString, QString> nb;
- const QList<AbstractMetaFunctionCList> opOverloads =
- filterGroupedOperatorFunctions(metaClass,
- OperatorQueryOption::ArithmeticOp
- | OperatorQueryOption::IncDecrementOp
- | OperatorQueryOption::LogicalOp
- | OperatorQueryOption::BitwiseOp);
-
- for (const AbstractMetaFunctionCList &opOverload : opOverloads) {
+ const QList<AbstractMetaFunctionCList> opOverloads = numberProtocolOperators(metaClass);
+ for (const auto &opOverload : opOverloads) {
const auto rfunc = opOverload.at(0);
QString opName = ShibokenGenerator::pythonOperatorFunctionName(rfunc);
nb[opName] = cpythonFunctionName(rfunc);
}
+ for (auto it = m_nbFuncs.cbegin(), end = m_nbFuncs.cend(); it != end; ++it) {
+ if (!it.value().isEmpty())
+ nb.insert(it.key(), it.value());
+ }
+
QString baseName = cpythonBaseName(metaClass);
if (hasBoolCast(metaClass))
- nb.insert(boolT(), baseName + u"___nb_bool"_s);
+ nb.insert(u"__bool__"_s, baseName + u"___nb_bool"_s);
for (auto it = nbFuncs().cbegin(), end = nbFuncs().cend(); it != end; ++it) {
const QString &nbName = it.key();
- if (nbName == u"__div__" || nbName == u"__idiv__")
- continue; // excludeFromPy3K
const auto nbIt = nb.constFind(nbName);
- if (nbIt != nb.constEnd()) {
- const QString fixednbName = nbName == boolT()
- ? u"nb_bool"_s : it.value();
- s << "{Py_" << fixednbName << ", reinterpret_cast<void *>("
- << nbIt.value() << ")},\n";
- }
- }
-
- auto nbIt = nb.constFind(u"__div__"_s);
- if (nbIt != nb.constEnd())
- s << "{Py_nb_true_divide, reinterpret_cast<void *>(" << nbIt.value() << ")},\n";
-
- nbIt = nb.constFind(u"__idiv__"_s);
- if (nbIt != nb.constEnd()) {
- s << "// This function is unused in Python 3. We reference it here.\n"
- << "{0, reinterpret_cast<void *>(" << nbIt.value() << ")},\n"
- << "// This list is ending at the first 0 entry.\n"
- << "// Therefore, we need to put the unused functions at the very end.\n";
+ if (nbIt != nb.constEnd())
+ s << pyTypeSlotEntry(it.value(), nbIt.value());
}
}
-void CppGenerator::writeTpTraverseFunction(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::writeTpTraverseFunction(TextStream &s, const AbstractMetaClassCPtr &metaClass)
{
QString baseName = cpythonBaseName(metaClass);
s << "static int " << baseName
<< "_traverse(PyObject *self, visitproc visit, void *arg)\n{\n" << indent
- << "return SbkObject_TypeF()->tp_traverse(self, visit, arg);\n"
+ << "auto traverseProc = "
+ << pyTypeGetSlot("traverseproc", sbkObjectTypeF, "Py_tp_traverse") << ";\n"
+ << "return traverseProc(self, visit, arg);\n"
<< outdent << "}\n";
}
-void CppGenerator::writeTpClearFunction(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::writeTpClearFunction(TextStream &s, const AbstractMetaClassCPtr &metaClass)
{
QString baseName = cpythonBaseName(metaClass);
s << "static int " << baseName << "_clear(PyObject *self)\n{\n" << indent
- << "return reinterpret_cast<PyTypeObject *>(SbkObject_TypeF())->tp_clear(self);\n"
+ << "auto clearProc = "
+ << pyTypeGetSlot("inquiry", sbkObjectTypeF, "Py_tp_clear") << ";\n"
+ << "return clearProc(self);\n"
<< outdent << "}\n";
}
-void CppGenerator::writeCopyFunction(TextStream &s, const GeneratorContext &context) const
+QString CppGenerator::writeCopyFunction(TextStream &s,
+ TextStream &definitionStream,
+ TextStream &signatureStream,
+ const GeneratorContext &context)
{
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
const QString className = chopType(cpythonTypeName(metaClass));
- s << "static PyObject *" << className << "___copy__(PyObject *self)\n"
- << "{\n" << indent;
+ const QString funcName = className + u"__copy__"_s;
+
+ signatureStream << fullPythonClassName(metaClass) << ".__copy__()\n";
+ definitionStream << PyMethodDefEntry{u"__copy__"_s, funcName, {"METH_NOARGS"_ba}, {}}
+ << ",\n";
+
+ s << "static PyObject *" << funcName << "(PyObject *self)\n"
+ << "{\n" << indent;
writeCppSelfDefinition(s, context, ErrorReturn::Default, CppSelfDefinitionFlag::CppSelfAsReference);
QString conversionCode;
if (!context.forSmartPointer())
@@ -4865,16 +4745,18 @@ void CppGenerator::writeCopyFunction(TextStream &s, const GeneratorContext &cont
writeFunctionReturnErrorCheckSection(s, ErrorReturn::Default);
s << "return " << PYTHON_RETURN_VAR << ";\n" << outdent
<< "}\n\n";
+
+ return funcName;
}
static inline void writeGetterFunctionStart(TextStream &s, const QString &funcName)
{
- s << "static PyObject *" << funcName << "(PyObject *self, void *)\n"
+ s << "static PyObject *" << funcName << "(PyObject *self, void * /* closure */)\n"
<< "{\n" << indent;
}
QString CppGenerator::cppFieldAccess(const AbstractMetaField &metaField,
- const GeneratorContext &context) const
+ const GeneratorContext &context)
{
QString result;
QTextStream str(&result);
@@ -4888,7 +4770,7 @@ QString CppGenerator::cppFieldAccess(const AbstractMetaField &metaField,
void CppGenerator::writeGetterFunction(TextStream &s,
const AbstractMetaField &metaField,
- const GeneratorContext &context) const
+ const GeneratorContext &context)
{
writeGetterFunctionStart(s, cpythonGetterFunctionName(metaField));
@@ -4925,34 +4807,31 @@ void CppGenerator::writeGetterFunction(TextStream &s,
if (newWrapperSameObject) {
// Special case colocated field with same address (first field in a struct)
s << "if (reinterpret_cast<void *>("
- << cppField
- << ") == reinterpret_cast<void *>("
- << CPP_SELF_VAR << ")) {\n";
- {
- Indentation indent(s);
- s << "pyOut = reinterpret_cast<PyObject *>(Shiboken::Object::findColocatedChild("
- << "reinterpret_cast<SbkObject *>(self), "
- << cpythonTypeNameExt(fieldType)
- << "));\n";
- s << "if (pyOut) {\n";
- {
- Indentation indent(s);
- s << "Py_IncRef(pyOut);\n"
- << "return pyOut;\n";
- }
- s << "}\n";
- }
+ << cppField << ") == reinterpret_cast<void *>("
+ << CPP_SELF_VAR << ")) {\n" << indent
+ << "pyOut = reinterpret_cast<PyObject *>(Shiboken::Object::findColocatedChild("
+ << "reinterpret_cast<SbkObject *>(self), "
+ << cpythonTypeNameExt(fieldType) << "));\n"
+ << "if (pyOut != nullptr) {\n" << indent
+ << "Py_IncRef(pyOut);\n"
+ << "return pyOut;\n"
+ << outdent << "}\n";
// Check if field wrapper has already been created.
- s << "} else if (Shiboken::BindingManager::instance().hasWrapper(" << cppField << ")) {" << "\n";
- {
- Indentation indent(s);
- s << "pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper("
- << cppField << "));" << "\n"
- << "Py_IncRef(pyOut);" << "\n"
- << "return pyOut;" << "\n";
- }
- s << "}\n";
- // Create and register new wrapper
+ s << outdent << "} else if (Shiboken::BindingManager::instance().hasWrapper("
+ << cppField << ")) {" << "\n" << indent
+ << "pyOut = reinterpret_cast<PyObject *>(Shiboken::BindingManager::instance().retrieveWrapper("
+ << cppField << "));" << "\n"
+ << "Py_IncRef(pyOut);" << "\n"
+ << "return pyOut;" << "\n"
+ << outdent << "}\n";
+ // Create and register new wrapper. We force a pointer conversion also
+ // for wrapped value types so that they refer to the struct member,
+ // avoiding any trouble copying them. Add a parent relationship to
+ // properly notify if the struct is deleted (see protected_test.py,
+ // testProtectedValueTypeProperty()). Note that this has currently
+ // unsolved issues when using temporary Python lists of structs
+ // which can cause elements to be reported deleted in expressions like
+ // "foo.list_of_structs[2].field".
s << "pyOut = "
<< "Shiboken::Object::newObject(" << cpythonTypeNameExt(fieldType)
<< ", " << cppField << ", false, true);\n"
@@ -4965,30 +4844,29 @@ void CppGenerator::writeGetterFunction(TextStream &s,
}
// Write a getter for QPropertySpec
-void CppGenerator::writeGetterFunction(TextStream &s, const QPropertySpec &property,
- const GeneratorContext &context) const
+void CppGenerator::writeGetterFunction(TextStream &s,
+ const QPropertySpec &property,
+ const GeneratorContext &context)
{
writeGetterFunctionStart(s, cpythonGetterFunctionName(property, context.metaClass()));
writeCppSelfDefinition(s, context);
- const QString value = QStringLiteral("value");
+ const QString value = "value"_L1;
s << "auto " << value << " = " << CPP_SELF_VAR << "->" << property.read() << "();\n"
- << "auto pyResult = ";
+ << "auto *pyResult = ";
writeToPythonConversion(s, property.type(), context.metaClass(), value);
- s << ";\nif (PyErr_Occurred() || !pyResult) {\n";
- {
- Indentation indent(s);
- s << "Py_XDECREF(pyResult);\nreturn {};\n";
- }
- s << "}\nreturn pyResult;\n" << outdent << "}\n\n";
+ s << ";\nif (" << shibokenErrorsOccurred << " || pyResult == nullptr) {\n"
+ << indent << "Py_XDECREF(pyResult);\nreturn {};\n" << outdent
+ << "}\nreturn pyResult;\n" << outdent << "}\n\n";
}
// Write setter function preamble (type checks on "pyIn")
-void CppGenerator::writeSetterFunctionPreamble(TextStream &s, const QString &name,
+void CppGenerator::writeSetterFunctionPreamble(TextStream &s,
+ const QString &name,
const QString &funcName,
const AbstractMetaType &type,
- const GeneratorContext &context) const
+ const GeneratorContext &context)
{
- s << "static int " << funcName << "(PyObject *self, PyObject *pyIn, void *)\n"
+ s << "static int " << funcName << "(PyObject *self, PyObject *pyIn, void * /* closure */)\n"
<< "{\n" << indent;
writeCppSelfDefinition(s, context, ErrorReturn::Zero);
@@ -5010,7 +4888,7 @@ void CppGenerator::writeSetterFunctionPreamble(TextStream &s, const QString &nam
void CppGenerator::writeSetterFunction(TextStream &s,
const AbstractMetaField &metaField,
- const GeneratorContext &context) const
+ const GeneratorContext &context)
{
const AbstractMetaType &fieldType = metaField.type();
writeSetterFunctionPreamble(s, metaField.name(), cpythonSetterFunctionName(metaField),
@@ -5042,278 +4920,138 @@ void CppGenerator::writeSetterFunction(TextStream &s,
}
// Write a setter for QPropertySpec
-void CppGenerator::writeSetterFunction(TextStream &s, const QPropertySpec &property,
- const GeneratorContext &context) const
+void CppGenerator::writeSetterFunction(TextStream &s,
+ const QPropertySpec &property,
+ const GeneratorContext &context)
{
- writeSetterFunctionPreamble(s, property.name(),
+ writeSetterFunctionPreamble(s,
+ property.name(),
cpythonSetterFunctionName(property, context.metaClass()),
property.type(), context);
s << "auto cppOut = " << CPP_SELF_VAR << "->" << property.read() << "();\n"
<< PYTHON_TO_CPP_VAR << "(pyIn, &cppOut);\n"
- << "if (PyErr_Occurred())\n";
- {
- Indentation indent(s);
- s << "return -1;\n";
- }
- s << CPP_SELF_VAR << "->" << property.write() << "(cppOut);\n"
+ << "if (" << shibokenErrorsOccurred << ")\n" << indent
+ << "return -1;\n" << outdent
+ << CPP_SELF_VAR << "->" << property.write() << "(cppOut);\n"
<< "return 0;\n" << outdent << "}\n\n";
}
void CppGenerator::writeRichCompareFunctionHeader(TextStream &s,
const QString &baseName,
- const GeneratorContext &context) const
+ const GeneratorContext &context)
{
s << "static PyObject * ";
s << baseName << "_richcompare(PyObject *self, PyObject *" << PYTHON_ARG
<< ", int op)\n{\n" << indent;
writeCppSelfDefinition(s, context, ErrorReturn::Default, CppSelfDefinitionFlag::CppSelfAsReference);
- writeUnusedVariableCast(s, CPP_SELF_VAR);
- s << "PyObject *" << PYTHON_RETURN_VAR << "{};\n"
- << PYTHON_TO_CPPCONVERSION_STRUCT << ' ' << PYTHON_TO_CPP_VAR << ";\n";
- writeUnusedVariableCast(s, PYTHON_TO_CPP_VAR);
- s << '\n';
+ s << sbkUnusedVariableCast(CPP_SELF_VAR)
+ << "PyObject *" << PYTHON_RETURN_VAR << "{};\n"
+ << PYTHON_TO_CPPCONVERSION_STRUCT << ' ' << PYTHON_TO_CPP_VAR << ";\n"
+ << sbkUnusedVariableCast(PYTHON_TO_CPP_VAR) << '\n';
}
-static const char richCompareComment[] =
- "// PYSIDE-74: By default, we redirect to object's tp_richcompare (which is `==`, `!=`).\n";
-
void CppGenerator::writeRichCompareFunction(TextStream &s,
const GeneratorContext &context) const
{
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
QString baseName = cpythonBaseName(metaClass);
writeRichCompareFunctionHeader(s, baseName, context);
- s << "switch (op) {\n";
- {
- Indentation indent(s);
- const QList<AbstractMetaFunctionCList> &groupedFuncs =
- filterGroupedOperatorFunctions(metaClass, OperatorQueryOption::ComparisonOp);
- for (const AbstractMetaFunctionCList &overloads : groupedFuncs) {
- const auto rfunc = overloads[0];
-
- const auto op = rfunc->comparisonOperatorType().value();
- s << "case " << AbstractMetaFunction::pythonRichCompareOpCode(op) << ':' << '\n';
-
- Indentation indent(s);
-
- int alternativeNumericTypes = 0;
- for (const auto &func : overloads) {
- if (!func->isStatic() &&
- ShibokenGenerator::isNumber(func->arguments().at(0).type().typeEntry()))
- alternativeNumericTypes++;
- }
+ s << "switch (op) {\n" << indent;
+ const QList<AbstractMetaFunctionCList> &groupedFuncs =
+ filterGroupedOperatorFunctions(metaClass, OperatorQueryOption::ComparisonOp);
+ for (const AbstractMetaFunctionCList &overloads : groupedFuncs) {
+ const auto rfunc = overloads[0];
- bool first = true;
- OverloadData overloadData(overloads, api());
- const OverloadDataList &nextOverloads = overloadData.children();
- for (const auto &od : nextOverloads) {
- const auto func = od->referenceFunction();
- if (func->isStatic())
- continue;
- auto argType = getArgumentType(func, 0);
- if (!first) {
- s << " else ";
- } else {
- first = false;
- }
- s << "if (";
- writeTypeCheck(s, argType, PYTHON_ARG,
- alternativeNumericTypes == 1 || isPyInt(argType));
- s << ") {\n";
- {
- Indentation indent(s);
- s << "// " << func->signature() << '\n';
- writeArgumentConversion(s, argType, CPP_ARG0,
- PYTHON_ARG, ErrorReturn::Default,
- metaClass,
- QString(), func->isUserAdded());
-
- // If the function is user added, use the inject code
- bool generateOperatorCode = true;
- if (func->isUserAdded()) {
- CodeSnipList snips = func->injectedCodeSnips();
- if (!snips.isEmpty()) {
- writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionAny,
- TypeSystem::TargetLangCode, func,
- false /* uses PyArgs */, &func->arguments().constLast());
- generateOperatorCode = false;
- }
- }
- if (generateOperatorCode) {
- if (!func->isVoid())
- s << func->type().cppSignature() << " " << CPP_RETURN_VAR << " = ";
- // expression
- if (func->isPointerOperator())
- s << '&';
- s << CPP_SELF_VAR << ' '
- << AbstractMetaFunction::cppComparisonOperator(op) << " (";
- if (auto deRef = argType.shouldDereferenceArgument(); deRef > 0)
- s << QByteArray(deRef, '*');
- s << CPP_ARG0 << ");\n"
- << PYTHON_RETURN_VAR << " = ";
- if (!func->isVoid()) {
- writeToPythonConversion(s, func->type(), metaClass,
- CPP_RETURN_VAR);
- } else {
- s << "Py_None;\n" << "Py_INCREF(Py_None)";
- }
- s << ";\n";
- }
- }
- s << '}';
- }
+ const auto op = rfunc->comparisonOperatorType().value();
+ s << "case " << AbstractMetaFunction::pythonRichCompareOpCode(op)
+ << ":\n" << indent;
+
+ int alternativeNumericTypes = 0;
+ for (const auto &func : overloads) {
+ if (!func->isStatic() &&
+ ShibokenGenerator::isNumber(func->arguments().at(0).type().typeEntry()))
+ alternativeNumericTypes++;
+ }
- s << " else {\n";
- if (op == AbstractMetaFunction::OperatorEqual ||
- op == AbstractMetaFunction::OperatorNotEqual) {
- Indentation indent(s);
- s << PYTHON_RETURN_VAR << " = "
- << (op == AbstractMetaFunction::OperatorEqual ? "Py_False" : "Py_True") << ";\n"
- << "Py_INCREF(" << PYTHON_RETURN_VAR << ");\n";
+ bool first = true;
+ OverloadData overloadData(overloads, api());
+ const OverloadDataList &nextOverloads = overloadData.children();
+ for (const auto &od : nextOverloads) {
+ const auto func = od->referenceFunction();
+ if (func->isStatic())
+ continue;
+ auto argType = getArgumentType(func, 0);
+ if (!first) {
+ s << " else ";
} else {
- Indentation indent(s);
- s << "goto " << baseName << "_RichComparison_TypeError;\n";
+ first = false;
}
- s << "}\n\n";
-
- s << "break;\n";
- }
- s << "default:\n";
- {
- Indentation indent(s);
- s << richCompareComment
- << "return FallbackRichCompare(self, " << PYTHON_ARG << ", op);\n"
- << "goto " << baseName << "_RichComparison_TypeError;\n";
+ s << "if (";
+ writeTypeCheck(s, argType, PYTHON_ARG,
+ alternativeNumericTypes == 1 || isPyInt(argType));
+ s << ") {\n" << indent
+ << "// " << func->signature() << '\n';
+ writeArgumentConversion(s, argType, CPP_ARG0,
+ PYTHON_ARG, ErrorReturn::Default,
+ metaClass,
+ QString(), func->isUserAdded());
+ // If the function is user added, use the inject code
+ bool generateOperatorCode = true;
+ if (func->isUserAdded()) {
+ CodeSnipList snips = func->injectedCodeSnips();
+ if (!snips.isEmpty()) {
+ writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionAny,
+ TypeSystem::TargetLangCode, func,
+ false /* uses PyArgs */, &func->arguments().constLast());
+ generateOperatorCode = false;
+ }
+ }
+ if (generateOperatorCode) {
+ if (!func->isVoid())
+ s << func->type().cppSignature() << " " << CPP_RETURN_VAR << " = ";
+ // expression
+ if (func->isPointerOperator())
+ s << '&';
+ s << CPP_SELF_VAR << ' '
+ << AbstractMetaFunction::cppComparisonOperator(op) << " (";
+ auto generatorArg = GeneratorArgument::fromMetaType(argType);
+ if (generatorArg.indirections != 0)
+ s << QByteArray(generatorArg.indirections, '*');
+ s << CPP_ARG0 << ");\n"
+ << PYTHON_RETURN_VAR << " = ";
+ if (!func->isVoid()) {
+ writeToPythonConversion(s, func->type(), metaClass,
+ CPP_RETURN_VAR);
+ } else {
+ s << "Py_None;\n" << "Py_INCREF(Py_None)";
+ }
+ s << ";\n";
+ }
+ s << outdent << '}';
}
- }
- s << "}\n\n";
-
- writeRichCompareFunctionFooter(s, baseName);
-}
-
-void CppGenerator::writeRichCompareFunctionFooter(TextStream &s,
- const QString &baseName)
-{
- s << "if (" << PYTHON_RETURN_VAR << " && !PyErr_Occurred())\n";
- {
- Indentation indent(s);
- s << "return " << PYTHON_RETURN_VAR << ";\n";
- }
- s << baseName << "_RichComparison_TypeError:\n"
- << "Shiboken::Errors::setOperatorNotImplemented();\n"
- << ErrorReturn::Default << '\n' << outdent << "}\n\n";
-}
-
-using ComparisonOperatorList = QList<AbstractMetaFunction::ComparisonOperatorType>;
-
-// Return the available comparison operators for smart pointers
-static ComparisonOperatorList smartPointeeComparisons(const GeneratorContext &context)
-{
- Q_ASSERT(context.forSmartPointer());
- auto *te = context.preciseType().instantiations().constFirst().typeEntry();
- if (te->isExtendedCppPrimitive()) { // Primitive pointee types have all
- return {AbstractMetaFunction::OperatorEqual,
- AbstractMetaFunction::OperatorNotEqual,
- AbstractMetaFunction::OperatorLess,
- AbstractMetaFunction::OperatorLessEqual,
- AbstractMetaFunction::OperatorGreater,
- AbstractMetaFunction::OperatorGreaterEqual};
- }
-
- auto *pointeeClass = context.pointeeClass();
- if (!pointeeClass)
- return {};
-
- ComparisonOperatorList result;
- const auto &comparisons =
- pointeeClass->operatorOverloads(OperatorQueryOption::SymmetricalComparisonOp);
- for (const auto &f : comparisons) {
- const auto ct = f->comparisonOperatorType().value();
- if (!result.contains(ct))
- result.append(ct);
- }
- return result;
-}
-
-void CppGenerator::writeSmartPointerRichCompareFunction(TextStream &s,
- const GeneratorContext &context) const
-{
- static const char selfPointeeVar[] = "cppSelfPointee";
- static const char cppArg0PointeeVar[] = "cppArg0Pointee";
- const AbstractMetaClass *metaClass = context.metaClass();
- QString baseName = cpythonBaseName(metaClass);
- writeRichCompareFunctionHeader(s, baseName, context);
-
- s << "if (";
- writeTypeCheck(s, context.preciseType(), PYTHON_ARG);
- s << ") {\n" << indent;
- writeArgumentConversion(s, context.preciseType(), CPP_ARG0,
- PYTHON_ARG, ErrorReturn::Default, metaClass);
-
- const auto *te = context.preciseType().typeEntry();
- Q_ASSERT(te->isSmartPointer());
- const auto *ste = static_cast<const SmartPointerTypeEntry *>(te);
-
- s << "const auto *" << selfPointeeVar << " = " << CPP_SELF_VAR
- << '.' << ste->getter() << "();\n";
- s << "const auto *" << cppArg0PointeeVar << " = " << CPP_ARG0
- << '.' << ste->getter() << "();\n";
-
- // If we have an object without any comparisons, only generate a simple
- // equality check by pointee address
- auto availableOps = smartPointeeComparisons(context);
- const bool comparePointeeAddressOnly = availableOps.isEmpty();
- if (comparePointeeAddressOnly) {
- availableOps << AbstractMetaFunction::OperatorEqual
- << AbstractMetaFunction::OperatorNotEqual;
- } else {
- // For value types with operators, we complain about nullptr
- s << "if (" << selfPointeeVar << " == nullptr || " << cppArg0PointeeVar
- << " == nullptr) {\n" << indent
- << "PyErr_SetString(PyExc_NotImplementedError, \"nullptr passed to comparison.\");\n"
- << ErrorReturn::Default << '\n' << outdent << "}\n";
- }
-
- s << "bool " << CPP_RETURN_VAR << "= false;\n"
- << "switch (op) {\n";
- for (auto op : availableOps) {
- s << "case " << AbstractMetaFunction::pythonRichCompareOpCode(op) << ":\n"
- << indent << CPP_RETURN_VAR << " = ";
- if (comparePointeeAddressOnly) {
- s << selfPointeeVar << ' ' << AbstractMetaFunction::cppComparisonOperator(op)
- << ' ' << cppArg0PointeeVar << ";\n";
+ s << " else {\n";
+ if (op == AbstractMetaFunction::OperatorEqual ||
+ op == AbstractMetaFunction::OperatorNotEqual) {
+ s << indent << PYTHON_RETURN_VAR << " = "
+ << (op == AbstractMetaFunction::OperatorEqual ? "Py_False" : "Py_True") << ";\n"
+ << "Py_INCREF(" << PYTHON_RETURN_VAR << ");\n" << outdent;
} else {
- // Shortcut for equality: Check pointee address
- if (op == AbstractMetaFunction::OperatorEqual
- || op == AbstractMetaFunction::OperatorLessEqual
- || op == AbstractMetaFunction::OperatorGreaterEqual) {
- s << selfPointeeVar << " == " << cppArg0PointeeVar << " || ";
- }
- // Generate object's comparison
- s << "*" << selfPointeeVar << ' '
- << AbstractMetaFunction::cppComparisonOperator(op) << " *"
- << cppArg0PointeeVar << ";\n";
+ s << indent << "return Shiboken::returnFromRichCompare("
+ << PYTHON_RETURN_VAR << ");\n" << outdent;
}
- s << "break;\n" << outdent;
+ s << "}\n\n";
+ s << "break;\n" << outdent;
}
- if (availableOps.size() < 6) {
- s << "default:\n" << indent
- << richCompareComment
- << "return FallbackRichCompare(self, " << PYTHON_ARG << ", op);\n" << outdent;
- }
- s << "}\n" << PYTHON_RETURN_VAR << " = " << CPP_RETURN_VAR
- << " ? Py_True : Py_False;\n"
- << "Py_INCREF(" << PYTHON_RETURN_VAR << ");\n";
-
- s << outdent << "} else {\n" << indent
- << "goto " << baseName << "_RichComparison_TypeError;\n"
- << outdent << "}\n";
-
- writeRichCompareFunctionFooter(s, baseName);
+ s << "default:\n" << indent
+ << richCompareComment
+ << "return FallbackRichCompare(self, " << PYTHON_ARG << ", op);\n"
+ << outdent << outdent << "}\n\n"
+ << "return Shiboken::returnFromRichCompare(" << PYTHON_RETURN_VAR << ");\n" << outdent
+ << "}\n\n";
}
// Return a flag combination for PyMethodDef
@@ -5334,9 +5072,9 @@ QByteArrayList CppGenerator::methodDefinitionParameters(const OverloadData &over
}
// METH_STATIC causes a crash when used for global functions (also from
// invisible namespaces).
- auto *ownerClass = overloadData.referenceFunction()->ownerClass();
+ const auto ownerClass = overloadData.referenceFunction()->ownerClass();
if (ownerClass
- && !invisibleTopNamespaces().contains(const_cast<AbstractMetaClass *>(ownerClass))) {
+ && !invisibleTopNamespaces().contains(std::const_pointer_cast<AbstractMetaClass>(ownerClass))) {
if (overloadData.hasStaticFunction())
result.append(QByteArrayLiteral("METH_STATIC"));
if (overloadData.hasClassMethod())
@@ -5356,7 +5094,7 @@ QList<PyMethodDefEntry>
QList<PyMethodDefEntry> result;
result.reserve(names.size());
for (const auto &name : names)
- result.append({name, funcName, parameters});
+ result.append({name, funcName, parameters, {}});
return result;
}
@@ -5395,12 +5133,6 @@ QString CppGenerator::signatureParameter(const AbstractMetaArgument &arg) const
if (size > 1)
s << ']';
- if (!arg.defaultValueExpression().isEmpty()) {
- s << '=';
- QString e = arg.defaultValueExpression();
- e.replace(u"::"_s, u"."_s);
- s << e;
- }
return result;
}
@@ -5417,71 +5149,80 @@ void CppGenerator::writeSignatureInfo(TextStream &s, const OverloadData &overloa
// PYSIDE-1328: `self`-ness cannot be computed in Python because there are mixed cases.
// Toplevel functions like `PySide6.QtCore.QEnum` are always self-less.
if (!(f->isStatic()) && f->ownerClass())
- args << u"self"_s;
+ args << PYTHON_SELF_VAR;
const auto &arguments = f->arguments();
for (qsizetype i = 0, size = arguments.size(); i < size; ++i) {
- QString t = f->pyiTypeReplaced(i + 1);
- if (t.isEmpty()) {
- t = signatureParameter(arguments.at(i));
- } else {
- t.prepend(u':');
- t.prepend(arguments.at(i).name());
+ const auto n = i + 1;
+ const auto &arg = arguments.at(i);
+ if (!f->argumentRemoved(n)) {
+ QString t = f->pyiTypeReplaced(n);
+ if (t.isEmpty()) {
+ t = signatureParameter(arg);
+ } else {
+ t.prepend(u':');
+ t.prepend(arg.name());
+ }
+ QString defaultValue = arg.defaultValueExpression();
+ if (!defaultValue.isEmpty())
+ t += u'=' + defaultValue.replace(u"::"_s, u"."_s);
+ args.append(t);
}
- args.append(t);
}
// mark the multiple signatures as such, to make it easier to generate different code
if (multiple)
s << idx-- << ':';
s << funcName << '(' << args.join(u',') << ')';
- if (!f->isVoid()) {
- QString t = f->pyiTypeReplaced(0);
- if (t.isEmpty())
- t = f->type().pythonSignature();
- s << "->" << t;
- }
+
+ QString returnType = f->pyiTypeReplaced(0); // pyi or modified type
+ if (returnType.isEmpty() && !f->isVoid())
+ returnType = f->type().pythonSignature();
+ if (!returnType.isEmpty())
+ s << "->" << returnType;
+
s << '\n';
}
}
-void CppGenerator::writeEnumsInitialization(TextStream &s, AbstractMetaEnumList &enums,
- ErrorReturn errorReturn) const
+void CppGenerator::writeEnumsInitialization(TextStream &s, AbstractMetaEnumList &enums)
{
if (enums.isEmpty())
return;
- bool preambleWrittenE = false;
- bool preambleWrittenF = false;
- for (const AbstractMetaEnum &cppEnum : qAsConst(enums)) {
+ bool preambleWritten = false;
+ bool etypeUsed = false;
+
+ for (const AbstractMetaEnum &cppEnum : std::as_const(enums)) {
if (cppEnum.isPrivate())
continue;
- if (!preambleWrittenE) {
+ if (!preambleWritten) {
s << "// Initialization of enums.\n"
+ << "Shiboken::AutoDecRef tpDict{};\n"
<< "PyTypeObject *EType{};\n\n";
- preambleWrittenE = true;
+ preambleWritten = true;
}
- if (!preambleWrittenF && cppEnum.typeEntry()->flags()) {
- s << "// Initialization of enums, flags part.\n"
- << "PyTypeObject *FType{};\n\n";
- preambleWrittenF = true;
- }
- writeEnumInitialization(s, cppEnum, errorReturn);
+ ConfigurableScope configScope(s, cppEnum.typeEntry());
+ etypeUsed |= writeEnumInitialization(s, cppEnum);
}
+ if (preambleWritten && !etypeUsed)
+ s << sbkUnusedVariableCast("EType");
}
-static QString mangleName(QString name)
+static qsizetype maxLineLength(const QStringList &list)
{
- if (name == u"None" || name == u"False" || name == u"True")
- name += u'_';
- return name;
+ qsizetype result = 0;
+ for (const auto &s : list) {
+ if (auto len = s.size(); len > result)
+ result = len;
+ }
+ return result;
}
-void CppGenerator::writeEnumInitialization(TextStream &s, const AbstractMetaEnum &cppEnum,
- ErrorReturn errorReturn) const
+bool CppGenerator::writeEnumInitialization(TextStream &s, const AbstractMetaEnum &cppEnum)
{
- const AbstractMetaClass *enclosingClass = cppEnum.targetLangEnclosingClass();
- bool hasUpperEnclosingClass = enclosingClass
- && enclosingClass->targetLangEnclosingClass() != nullptr;
- const EnumTypeEntry *enumTypeEntry = cppEnum.typeEntry();
+ const auto enclosingClass = cppEnum.targetLangEnclosingClass();
+ const bool hasUpperEnclosingClass = enclosingClass
+ && enclosingClass->targetLangEnclosingClass();
+ EnumTypeEntryCPtr enumTypeEntry = cppEnum.typeEntry();
QString enclosingObjectVariable;
if (enclosingClass)
enclosingObjectVariable = cpythonTypeName(enclosingClass);
@@ -5494,102 +5235,115 @@ void CppGenerator::writeEnumInitialization(TextStream &s, const AbstractMetaEnum
s << (cppEnum.isAnonymous() ? "anonymous enum identified by enum value" : "enum");
s << " '" << cppEnum.name() << "'.\n";
- QString enumVarTypeObj = cpythonTypeNameExt(enumTypeEntry);
- if (!cppEnum.isAnonymous()) {
- int packageLevel = packageName().count(u'.') + 1;
- FlagsTypeEntry *flags = enumTypeEntry->flags();
- if (flags) {
- // The following could probably be made nicer:
- // We need 'flags->flagsName()' with the full module/class path.
- QString fullPath = getClassTargetFullName(cppEnum);
- fullPath.truncate(fullPath.lastIndexOf(u'.') + 1);
- s << "FType = PySide::QFlags::create(\""
- << packageLevel << ':' << fullPath << flags->flagsName() << "\", \n" << indent
- << cpythonEnumName(cppEnum) << "_number_slots);\n" << outdent
- << cpythonTypeNameExt(flags) << " = FType;\n";
- }
+ const QString userType = cppEnum.typeEntry()->cppType();
+ const bool isSigned = cppEnum.isSigned() && !userType.contains(u"unsigned"_s);
+ const bool isAccessible = !avoidProtectedHack() || !cppEnum.isProtected();
+ const auto enumValues = cppEnum.nonRejectedValues();
- s << "EType = Shiboken::Enum::"
- << ((enclosingClass
- || hasUpperEnclosingClass) ? "createScopedEnum" : "createGlobalEnum")
- << '(' << enclosingObjectVariable << ',' << '\n';
- {
- Indentation indent(s);
- s << '"' << cppEnum.name() << "\",\n"
- << '"' << packageLevel << ':' << getClassTargetFullName(cppEnum) << "\",\n"
- << '"' << cppEnum.qualifiedCppName() << '"';
- if (flags)
- s << ",\nFType";
- s << ");\n";
+ const QString prefix = cppEnum.name();
+
+ const QString intType = userType.isEmpty() ? cppEnum.underlyingType() : userType;
+
+ // Create a list of values
+ const QString initializerValues = prefix + u"_InitializerValues"_s;
+ const QString initializerName = prefix + u"_Initializer"_s;
+
+ // Build maybe array of enum names.
+ if (cppEnum.enumKind() != AnonymousEnum) {
+ s << "const char *" << initializerName << "[] = {\n" << indent;
+ for (const auto &enumValue : enumValues) {
+ QString name = mangleName(enumValue.name());
+ s << '\"' << name << "\",\n";
}
- s << "if (!EType)\n"
- << indent << errorReturn << outdent << '\n';
+ s << "nullptr};\n" << outdent;
}
- for (const AbstractMetaEnumValue &enumValue : cppEnum.values()) {
- if (enumTypeEntry->isEnumValueRejected(enumValue.name()))
- continue;
+ int targetHexLen = 0;
+ QString usedIntType = userType;
+ if (usedIntType.isEmpty()) {
+ const int usedBits = cppEnum.usedBits();
+ targetHexLen = usedBits / 4;
+ usedIntType = AbstractMetaEnum::intTypeForSize(usedBits, cppEnum.isSigned());
+ }
- QString enumValueText;
- if (!avoidProtectedHack() || !cppEnum.isProtected()) {
- enumValueText = u"(long) "_s;
- if (cppEnum.enclosingClass())
- enumValueText += cppEnum.enclosingClass()->qualifiedCppName() + u"::"_s;
- // Fully qualify the value which is required for C++ 11 enum classes.
- if (!cppEnum.isAnonymous())
- enumValueText += cppEnum.name() + u"::"_s;
- enumValueText += enumValue.name();
- } else {
- enumValueText += enumValue.value().toString();
- }
+ if (usedIntType != intType)
+ s << "// \"" << usedIntType << "\" used instead of \"" << intType << "\"\n";
- const QString mangledName = mangleName(enumValue.name());
- switch (cppEnum.enumKind()) {
- case AnonymousEnum:
+ // Calculating formatting columns
+ QString enumValuePrefix;
+ if (isAccessible) {
+ if (cppEnum.enclosingClass())
+ enumValuePrefix += cppEnum.enclosingClass()->qualifiedCppName() + u"::"_s;
+ if (!cppEnum.isAnonymous())
+ enumValuePrefix += cppEnum.name() + u"::"_s;
+ }
+
+ // Build array of enum values
+ if (enumValues.isEmpty()) {
+ s << "const " << usedIntType << " *" << initializerValues << "{};\n";
+ } else {
+ QStringList values;
+ values.reserve(enumValues.size());
+ s << "constexpr " << usedIntType << ' ' << initializerValues << "[] = {\n" << indent;
+ for (qsizetype idx = 0, last = enumValues.size() - 1; idx <= last; ++idx) {
+ const auto &enumValue = enumValues.at(idx);
+ QString line = usedIntType + u'(' + (isAccessible
+ ? enumValuePrefix + enumValue.name()
+ : enumValue.value().toString()) + u')';
+ if (idx != last)
+ line += u',';
+ values.append(line);
+ }
+
+ const auto len = maxLineLength(values) + 1;
+ for (qsizetype idx = 0, size = enumValues.size(); idx < size; ++idx) {
+ const auto &enumValue = enumValues.at(idx).value();
+ const char *numberSpace = enumValue.isNegative() ? " " : " ";
+ s << values.at(idx) << Pad(' ', len - values.at(idx).size())
+ << "//" << numberSpace << enumValue.toHex(targetHexLen)
+ << numberSpace << enumValue.toString() << '\n';
+ }
+ s << "};\n" << outdent;
+ }
+
+ // Build initialization of anonymous enums
+ if (cppEnum.enumKind() == AnonymousEnum) {
+ int idx = 0;
+ for (const auto &enumValue : enumValues) {
+ const QString mangledName = mangleName(enumValue.name());
+ const QString pyValue = initializerValues + u'[' + QString::number(idx++) + u']';
if (enclosingClass || hasUpperEnclosingClass) {
- s << "{\n";
- {
- Indentation indentation(s);
- s << "PyObject *anonEnumItem = PyLong_FromLong(" << enumValueText << ");\n"
- << "if (PyDict_SetItemString(reinterpret_cast<PyTypeObject *>("
- << enclosingObjectVariable
- << ")->tp_dict, \"" << mangledName << "\", anonEnumItem) < 0)\n"
- << indent << errorReturn << outdent
- << "Py_DECREF(anonEnumItem);\n";
- }
- s << "}\n";
+ s << "tpDict.reset(PepType_GetDict(reinterpret_cast<PyTypeObject *>("
+ << enclosingObjectVariable << ")));\n"
+ << "PyDict_SetItemString(tpDict.object(), \"" << mangledName << "\",\n"
+ << indent << (isSigned ? "PyLong_FromLongLong" : "PyLong_FromUnsignedLongLong")
+ << "(" << pyValue << "));\n" << outdent;
} else {
- s << "if (PyModule_AddIntConstant(module, \"" << mangledName << "\", ";
- s << enumValueText << ") < 0)\n" << indent << errorReturn << outdent;
+ s << "PyModule_AddObject(module, \"" << mangledName << "\",\n" << indent
+ << (isSigned ? "PyLong_FromLongLong" : "PyLong_FromUnsignedLongLong") << "("
+ << pyValue << "));\n" << outdent;
}
- break;
- case CEnum: {
- s << "if (!Shiboken::Enum::";
- s << ((enclosingClass || hasUpperEnclosingClass) ? "createScopedEnumItem"
- : "createGlobalEnumItem");
- s << '(' << "EType" << ',' << '\n';
- Indentation indent(s);
- s << enclosingObjectVariable << ", \"" << mangledName << "\", "
- << enumValueText << "))\n" << errorReturn;
- }
- break;
- case EnumClass: {
- s << "if (!Shiboken::Enum::createScopedEnumItem("
- << "EType" << ",\n";
- Indentation indentation(s);
- s << "EType" << ", \"" << mangledName << "\", "
- << enumValueText << "))\n" << errorReturn;
- }
- break;
}
}
- s << "// PYSIDE-1735: Resolving the whole enum class at the end for API compatibility.\n"
- << "EType = morphLastEnumToPython();\n"
- << enumVarTypeObj << " = EType;\n";
+
+ bool etypeUsed = false;
+
+ QString enumVarTypeObj = cpythonTypeNameExtSet(enumTypeEntry);
+ if (!cppEnum.isAnonymous()) {
+ int packageLevel = packageName().count(u'.') + 1;
+ s << "EType = Shiboken::Enum::"
+ << "createPythonEnum"
+ << '(' << enclosingObjectVariable << ",\n" << indent
+ << '"' << packageLevel << ':' << getClassTargetFullName(cppEnum) << "\",\n"
+ << initializerName << ", " << initializerValues << ");\n" << outdent
+ << enumVarTypeObj << " = EType;\n";
+ etypeUsed = true;
+ }
+
if (cppEnum.typeEntry()->flags()) {
s << "// PYSIDE-1735: Mapping the flags class to the same enum class.\n"
- << cpythonTypeNameExt(cppEnum.typeEntry()->flags()) << " =\n"
- << indent << "mapFlagsToSameEnum(FType, EType);\n" << outdent;
+ << cpythonTypeNameExtSet(cppEnum.typeEntry()->flags()) << " =\n"
+ << indent << "EType;\n" << outdent;
}
writeEnumConverterInitialization(s, cppEnum);
@@ -5597,9 +5351,11 @@ void CppGenerator::writeEnumInitialization(TextStream &s, const AbstractMetaEnum
if (cppEnum.typeEntry()->flags())
s << "/flags";
s << ".\n\n";
+
+ return etypeUsed;
}
-void CppGenerator::writeSignalInitialization(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::writeSignalInitialization(TextStream &s, const AbstractMetaClassCPtr &metaClass)
{
// Try to check something and print some warnings
const auto &signalFuncs = metaClass->cppSignalFunctions();
@@ -5621,134 +5377,11 @@ void CppGenerator::writeSignalInitialization(TextStream &s, const AbstractMetaCl
}
}
- s << "PySide::Signal::registerSignals(pyType, &::"
+ s << "PySide::Signal::registerSignals(pyType, &" << m_gsp
<< metaClass->qualifiedCppName() << "::staticMetaObject);\n";
}
-void CppGenerator::writeFlagsToLong(TextStream &s, const AbstractMetaEnum &cppEnum)
-{
- FlagsTypeEntry *flagsEntry = cppEnum.typeEntry()->flags();
- if (!flagsEntry)
- return;
- s << "static PyObject *" << cpythonEnumName(cppEnum) << "_long(PyObject *self)\n"
- << "{\n" << indent
- << "int val;\n";
- AbstractMetaType flagsType = AbstractMetaType::fromTypeEntry(flagsEntry);
- s << cpythonToCppConversionFunction(flagsType) << "self, &val);\n"
- << "return Shiboken::Conversions::copyToPython(Shiboken::Conversions::PrimitiveTypeConverter<int>(), &val);\n"
- << outdent << "}\n";
-}
-
-void CppGenerator::writeFlagsNonZero(TextStream &s, const AbstractMetaEnum &cppEnum)
-{
- FlagsTypeEntry *flagsEntry = cppEnum.typeEntry()->flags();
- if (!flagsEntry)
- return;
- s << "static int " << cpythonEnumName(cppEnum) << "__nonzero(PyObject *self)\n";
- s << "{\n" << indent << "int val;\n";
- AbstractMetaType flagsType = AbstractMetaType::fromTypeEntry(flagsEntry);
- s << cpythonToCppConversionFunction(flagsType) << "self, &val);\n"
- << "return val != 0;\n"
- << outdent << "}\n";
-}
-
-void CppGenerator::writeFlagsMethods(TextStream &s, const AbstractMetaEnum &cppEnum)
-{
- writeFlagsBinaryOperator(s, cppEnum, u"and"_s, u"&"_s);
- writeFlagsBinaryOperator(s, cppEnum, u"or"_s, u"|"_s);
- writeFlagsBinaryOperator(s, cppEnum, u"xor"_s, u"^"_s);
-
- writeFlagsUnaryOperator(s, cppEnum, u"invert"_s, u"~"_s);
- writeFlagsToLong(s, cppEnum);
- writeFlagsNonZero(s, cppEnum);
-
- s << '\n';
-}
-
-void CppGenerator::writeFlagsNumberMethodsDefinition(TextStream &s, const AbstractMetaEnum &cppEnum)
-{
- QString cpythonName = cpythonEnumName(cppEnum);
-
- s << "static PyType_Slot " << cpythonName << "_number_slots[] = {\n" << indent
- << "{Py_nb_bool, reinterpret_cast<void *>(" << cpythonName << "__nonzero)},\n"
- << "{Py_nb_invert, reinterpret_cast<void *>(" << cpythonName << "___invert__)},\n"
- << "{Py_nb_and, reinterpret_cast<void *>(" << cpythonName << "___and__)},\n"
- << "{Py_nb_xor, reinterpret_cast<void *>(" << cpythonName << "___xor__)},\n"
- << "{Py_nb_or, reinterpret_cast<void *>(" << cpythonName << "___or__)},\n"
- << "{Py_nb_int, reinterpret_cast<void *>(" << cpythonName << "_long)},\n"
- << "{Py_nb_index, reinterpret_cast<void *>(" << cpythonName << "_long)},\n"
- << "{0, " << NULL_PTR << "} // sentinel\n" << outdent
- << "};\n\n";
-}
-
-void CppGenerator::writeFlagsNumberMethodsDefinitions(TextStream &s,
- const AbstractMetaEnumList &enums)
-{
- for (const AbstractMetaEnum &e : enums) {
- if (!e.isAnonymous() && !e.isPrivate() && e.typeEntry()->flags()) {
- writeFlagsMethods(s, e);
- writeFlagsNumberMethodsDefinition(s, e);
- s << '\n';
- }
- }
-}
-
-void CppGenerator::writeFlagsBinaryOperator(TextStream &s, const AbstractMetaEnum &cppEnum,
- const QString &pyOpName, const QString &cppOpName)
-{
- FlagsTypeEntry *flagsEntry = cppEnum.typeEntry()->flags();
- Q_ASSERT(flagsEntry);
-
- s << "PyObject *" << cpythonEnumName(cppEnum) << "___" << pyOpName
- << "__(PyObject *self, PyObject *" << PYTHON_ARG << ")\n{\n" << indent;
-
- AbstractMetaType flagsType = AbstractMetaType::fromTypeEntry(flagsEntry);
- s << "::" << flagsEntry->originalName() << " cppResult, " << CPP_SELF_VAR
- << ", cppArg;\n"
- << CPP_SELF_VAR << " = static_cast<::" << flagsEntry->originalName()
- << ">(int(PyLong_AsLong(self)));\n"
- // PYSIDE-1436: Need to error check self as well because operators are used
- // sometimes with swapped args.
- << "if (PyErr_Occurred())\n" << indent
- << "return nullptr;\n" << outdent
- << "cppArg = static_cast<" << flagsEntry->originalName()
- << ">(int(PyLong_AsLong(" << PYTHON_ARG << ")));\n"
- << "if (PyErr_Occurred())\n" << indent
- << "return nullptr;\n" << outdent
- << "cppResult = " << CPP_SELF_VAR << " " << cppOpName << " cppArg;\n"
- << "return ";
- writeToPythonConversion(s, flagsType, nullptr, u"cppResult"_s);
- s << ";\n" << outdent << "}\n\n";
-}
-
-void CppGenerator::writeFlagsUnaryOperator(TextStream &s, const AbstractMetaEnum &cppEnum,
- const QString &pyOpName,
- const QString &cppOpName, bool boolResult)
-{
- FlagsTypeEntry *flagsEntry = cppEnum.typeEntry()->flags();
- Q_ASSERT(flagsEntry);
-
- s << "PyObject *" << cpythonEnumName(cppEnum) << "___" << pyOpName
- << "__(PyObject *self, PyObject *" << PYTHON_ARG << ")\n{\n" << indent;
-
- AbstractMetaType flagsType = AbstractMetaType::fromTypeEntry(flagsEntry);
- s << "::" << flagsEntry->originalName() << " " << CPP_SELF_VAR << ";\n"
- << cpythonToCppConversionFunction(flagsType) << "self, &" << CPP_SELF_VAR
- << ");\n";
- if (boolResult)
- s << "bool";
- else
- s << "::" << flagsEntry->originalName();
- s << " cppResult = " << cppOpName << CPP_SELF_VAR << ";\n"
- << "return ";
- if (boolResult)
- s << "PyBool_FromLong(cppResult)";
- else
- writeToPythonConversion(s, flagsType, nullptr, u"cppResult"_s);
- s << ";\n" << outdent << "}\n\n";
-}
-
-QString CppGenerator::getSimpleClassInitFunctionName(const AbstractMetaClass *metaClass)
+QString CppGenerator::getSimpleClassInitFunctionName(const AbstractMetaClassCPtr &metaClass)
{
QString initFunctionName;
// Disambiguate namespaces per module to allow for extending them.
@@ -5759,7 +5392,8 @@ QString CppGenerator::getSimpleClassInitFunctionName(const AbstractMetaClass *me
return initFunctionName;
}
-QString CppGenerator::getSimpleClassStaticFieldsInitFunctionName(const AbstractMetaClass *metaClass)
+QString
+ CppGenerator::getSimpleClassStaticFieldsInitFunctionName(const AbstractMetaClassCPtr &metaClass)
{
return u"init_"_s + getSimpleClassInitFunctionName(metaClass)
+ u"StaticFields"_s;
@@ -5790,8 +5424,8 @@ void CppGenerator::writeSignatureStrings(TextStream &s,
}
// Return the class name for which to invoke the destructor
-QString CppGenerator::destructorClassName(const AbstractMetaClass *metaClass,
- const GeneratorContext &classContext) const
+QString CppGenerator::destructorClassName(const AbstractMetaClassCPtr &metaClass,
+ const GeneratorContext &classContext)
{
if (metaClass->isNamespace() || metaClass->hasPrivateDestructor())
return {};
@@ -5808,14 +5442,69 @@ QString CppGenerator::destructorClassName(const AbstractMetaClass *metaClass,
return metaClass->qualifiedCppName();
}
+// Return the base type entries for introduceWrapperType()
+static ComplexTypeEntryCList pyBaseTypeEntries(const AbstractMetaClassCPtr &metaClass)
+{
+ ComplexTypeEntryCList result;
+ if (metaClass->isNamespace()) {
+ if (auto extended = metaClass->extendedNamespace())
+ result.append(extended->typeEntry());
+ return result;
+ }
+
+ const auto &baseClasses = metaClass->typeSystemBaseClasses();
+ for (auto base : baseClasses) {
+ for (; base != nullptr; base = base->baseClass()) { // Find a type that is not disabled.
+ const auto ct = base->typeEntry()->codeGeneration();
+ if (ct == TypeEntry::GenerateCode || ct == TypeEntry::GenerateForSubclass)
+ break;
+ }
+ result.append(base->typeEntry());
+ }
+ return result;
+}
+
+// Return the base type strings for introduceWrapperType()
+QStringList CppGenerator::pyBaseTypes(const AbstractMetaClassCPtr &metaClass)
+{
+ const auto &baseEntries = pyBaseTypeEntries(metaClass);
+ QStringList result;
+ for (const auto &baseEntry : baseEntries)
+ result.append("reinterpret_cast<PyObject *>("_L1 + cpythonTypeNameExt(baseEntry) + u')');
+ if (result.isEmpty()) // no base classes -> SbkObjectType.
+ result.append(sbkObjectTypeF);
+ return result;
+}
+
+void CppGenerator::writeInitInheritance(TextStream &s) const
+{
+ s << "static void " << initInheritanceFunction << "()\n{\n" << indent
+ << "auto &bm = Shiboken::BindingManager::instance();\n"
+ << sbkUnusedVariableCast("bm");
+ for (const auto &cls : api().classes()){
+ auto te = cls->typeEntry();
+ if (shouldGenerate(te)) {
+ const auto &baseEntries = pyBaseTypeEntries(cls);
+ if (!baseEntries.isEmpty()) {
+ const QString childTypeInitStruct = typeInitStruct(cls->typeEntry());
+ for (const auto &baseEntry : baseEntries) {
+ s << "bm.addClassInheritance(&" << typeInitStruct(baseEntry) << ",\n"
+ << Pad(' ', 23) << '&' << childTypeInitStruct << ");\n";
+ }
+ }
+ }
+ }
+ s << outdent << "}\n\n";
+}
+
void CppGenerator::writeClassRegister(TextStream &s,
- const AbstractMetaClass *metaClass,
+ const AbstractMetaClassCPtr &metaClass,
const GeneratorContext &classContext,
const QString &signatures) const
{
- const ComplexTypeEntry *classTypeEntry = metaClass->typeEntry();
+ ComplexTypeEntryCPtr classTypeEntry = metaClass->typeEntry();
- const AbstractMetaClass *enc = metaClass->targetLangEnclosingClass();
+ AbstractMetaClassCPtr enc = metaClass->targetLangEnclosingClass();
QString enclosingObjectVariable = enc ? u"enclosingClass"_s : u"module"_s;
QString pyTypeName = cpythonTypeName(metaClass);
@@ -5823,103 +5512,84 @@ void CppGenerator::writeClassRegister(TextStream &s,
// PYSIDE-510: Create a signatures string for the introspection feature.
writeSignatureStrings(s, signatures, initFunctionName, "functions");
- s << "void init_" << initFunctionName;
- s << "(PyObject *" << enclosingObjectVariable << ")\n{\n" << indent;
+ s << "PyTypeObject *init_" << initFunctionName
+ << "(PyObject *" << enclosingObjectVariable << ")\n{\n" << indent;
+
+ const QString globalTypeVarExpr = !classContext.forSmartPointer()
+ ? cpythonTypeNameExtSet(classTypeEntry)
+ : cpythonTypeNameExtSet(classContext.preciseType());
+ s << "if (" << globalTypeVarExpr << " != nullptr)\n" << indent
+ << "return " << globalTypeVarExpr << ";\n\n" << outdent;
// Multiple inheritance
QString pyTypeBasesVariable = chopType(pyTypeName) + u"_Type_bases"_s;
- const auto &baseClasses = metaClass->typeSystemBaseClasses();
- if (metaClass->baseClassNames().size() > 1) {
- s << "PyObject *" << pyTypeBasesVariable
- << " = PyTuple_Pack(" << baseClasses.size() << ',' << '\n';
- Indentation indent(s);
- for (int i = 0, size = baseClasses.size(); i < size; ++i) {
- if (i)
- s << ",\n";
- s << "reinterpret_cast<PyObject *>("
- << cpythonTypeNameExt(baseClasses.at(i)->typeEntry()) << ')';
- }
- s << ");\n\n";
+ const QStringList pyBases = pyBaseTypes(metaClass);
+ s << "Shiboken::AutoDecRef " << pyTypeBasesVariable << "(PyTuple_Pack("
+ << pyBases.size() << ",\n" << indent;
+ for (qsizetype i = 0, size = pyBases.size(); i < size; ++i) {
+ if (i)
+ s << ",\n";
+ s << pyBases.at(i);
}
+ s << "));\n\n" << outdent;
// Create type and insert it in the module or enclosing class.
const QString typePtr = u"_"_s + chopType(pyTypeName)
+ u"_Type"_s;
- s << typePtr << " = Shiboken::ObjectType::introduceWrapperType(\n";
- {
- Indentation indent(s);
- // 1:enclosingObject
- s << enclosingObjectVariable << ",\n";
-
- // 2:typeName
- s << "\"" << metaClass->name() << "\",\n";
-
- // 3:originalName
- s << "\"";
- if (!classContext.forSmartPointer()) {
- s << metaClass->qualifiedCppName();
- if (classTypeEntry->isObject())
- s << '*';
- } else {
- s << classContext.preciseType().cppSignature();
- }
-
- s << "\",\n";
- // 4:typeSpec
- s << '&' << chopType(pyTypeName) << "_spec,\n";
+ s << typePtr << " = Shiboken::ObjectType::introduceWrapperType(\n" << indent;
+ // 1:enclosingObject
+ s << enclosingObjectVariable << ",\n";
- // 5:cppObjDtor
- QString dtorClassName = destructorClassName(metaClass, classContext);
- if (dtorClassName.isEmpty())
- s << "nullptr,\n";
- else
- s << "&Shiboken::callCppDestructor< ::" << dtorClassName << " >,\n";
-
- // 6:baseType: Find a type that is not disabled.
- auto base = metaClass->isNamespace()
- ? metaClass->extendedNamespace() : metaClass->baseClass();
- if (!metaClass->isNamespace()) {
- for (; base != nullptr; base = base->baseClass()) {
- const auto ct = base->typeEntry()->codeGeneration();
- if (ct == TypeEntry::GenerateCode || ct == TypeEntry::GenerateForSubclass)
- break;
- }
- }
- if (base) {
- s << cpythonTypeNameExt(base->typeEntry()) << ",\n";
- } else {
- s << "0,\n";
- }
+ // 2:typeName
+ s << "\"" << metaClass->name() << "\",\n";
- // 7:baseTypes
- if (metaClass->baseClassNames().size() > 1)
- s << pyTypeBasesVariable << ',' << '\n';
- else
- s << "0,\n";
-
- // 8:wrapperflags
- QByteArrayList wrapperFlags;
- if (enc)
- wrapperFlags.append(QByteArrayLiteral("Shiboken::ObjectType::WrapperFlags::InnerClass"));
- if (metaClass->deleteInMainThread())
- wrapperFlags.append(QByteArrayLiteral("Shiboken::ObjectType::WrapperFlags::DeleteInMainThread"));
- if (wrapperFlags.isEmpty())
- s << '0';
- else
- s << wrapperFlags.join(" | ");
+ // 3:originalName
+ s << "\"";
+ if (!classContext.forSmartPointer()) {
+ s << metaClass->qualifiedCppName();
+ if (classTypeEntry->isObject())
+ s << '*';
+ } else {
+ s << classContext.preciseType().cppSignature();
}
- s << ");\nauto *pyType = " << pyTypeName << "; // references " << typePtr << "\n"
+
+ s << "\",\n";
+ // 4:typeSpec
+ s << '&' << chopType(pyTypeName) << "_spec,\n";
+
+ // 5:cppObjDtor
+ QString dtorClassName = destructorClassName(metaClass, classContext);
+ if (dtorClassName.isEmpty())
+ s << "nullptr,\n";
+ else
+ s << "&Shiboken::callCppDestructor< " << globalScopePrefix(classContext)
+ << dtorClassName << " >,\n";
+
+ // 7:baseTypes
+ s << pyTypeBasesVariable << ".object()," << '\n';
+
+ // 8:wrapperflags
+ QByteArrayList wrapperFlags;
+ if (enc)
+ wrapperFlags.append(QByteArrayLiteral("Shiboken::ObjectType::WrapperFlags::InnerClass"));
+ if (metaClass->deleteInMainThread())
+ wrapperFlags.append(QByteArrayLiteral("Shiboken::ObjectType::WrapperFlags::DeleteInMainThread"));
+ if (classTypeEntry->isValue())
+ wrapperFlags.append("Shiboken::ObjectType::WrapperFlags::Value"_ba);
+ if (wrapperFlags.isEmpty())
+ s << '0';
+ else
+ s << wrapperFlags.join(" | ");
+
+ s << outdent << ");\nauto *pyType = " << pyTypeName << "; // references "
+ << typePtr << "\n"
<< "InitSignatureStrings(pyType, " << initFunctionName << "_SignatureStrings);\n";
if (usePySideExtensions() && !classContext.forSmartPointer())
s << "SbkObjectType_SetPropertyStrings(pyType, "
<< chopType(pyTypeName) << "_PropertyStrings);\n";
-
- if (!classContext.forSmartPointer())
- s << cpythonTypeNameExt(classTypeEntry) << " = pyType;\n\n";
- else
- s << cpythonTypeNameExt(classContext.preciseType()) << " = pyType;\n\n";
+ s << globalTypeVarExpr << " = pyType;\n\n";
// Register conversions for the type.
writeConverterRegister(s, metaClass, classContext);
@@ -5934,7 +5604,7 @@ void CppGenerator::writeClassRegister(TextStream &s,
}
// Fill multiple inheritance data, if needed.
- const AbstractMetaClass *miClass = getMultipleInheritingClass(metaClass);
+ const auto miClass = getMultipleInheritingClass(metaClass);
if (miClass) {
s << "MultipleInheritanceInitFunction func = ";
if (miClass == metaClass) {
@@ -5951,14 +5621,19 @@ void CppGenerator::writeClassRegister(TextStream &s,
// Set typediscovery struct or fill the struct of another one
if (needsTypeDiscoveryFunction(metaClass)) {
- s << "Shiboken::ObjectType::setTypeDiscoveryFunctionV2(" << cpythonTypeName(metaClass)
- << ", &" << cpythonBaseName(metaClass) << "_typeDiscovery);\n\n";
+ s << "Shiboken::ObjectType::setTypeDiscoveryFunctionV2(\n" << indent
+ << cpythonTypeName(metaClass)
+ << ", &" << cpythonBaseName(metaClass) << "_typeDiscovery);" << outdent << "\n\n";
}
AbstractMetaEnumList classEnums = metaClass->enums();
metaClass->getEnumsFromInvisibleNamespacesToBeGenerated(&classEnums);
- writeEnumsInitialization(s, classEnums, ErrorReturn::Void);
+ if (!classContext.forSmartPointer() && !classEnums.isEmpty())
+ s << "// Pass the ..._EnumFlagInfo to the class.\n"
+ << "SbkObjectType_SetEnumFlagInfo(pyType, " << chopType(pyTypeName)
+ << "_EnumFlagInfo);\n\n";
+ writeEnumsInitialization(s, classEnums);
if (metaClass->hasSignals())
writeSignalInitialization(s, metaClass);
@@ -5978,25 +5653,40 @@ void CppGenerator::writeClassRegister(TextStream &s,
writeInitQtMetaTypeFunctionBody(s, classContext);
}
- if (usePySideExtensions() && metaClass->isQObject()) {
+ if (usePySideExtensions() && isQObject(metaClass)) {
s << "Shiboken::ObjectType::setSubTypeInitHook(pyType, &PySide::initQObjectSubType);\n"
- << "PySide::initDynamicMetaObject(pyType, &::"
- << metaClass->qualifiedCppName() << "::staticMetaObject, sizeof(";
- if (shouldGenerateCppWrapper(metaClass))
- s << wrapperName(metaClass);
- else
- s << "::" << metaClass->qualifiedCppName();
- s << "));\n";
+ << "PySide::initDynamicMetaObject(pyType, &" << m_gsp
+ << metaClass->qualifiedCppName() << "::staticMetaObject, sizeof("
+ << (shouldGenerateCppWrapper(metaClass)
+ ? wrapperName(metaClass) : getFullTypeName(metaClass))
+ << "));\n";
}
- s << outdent << "}\n";
+ s << "\nreturn pyType;\n" << outdent << "}\n";
}
-void CppGenerator::writeStaticFieldInitialization(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::writeStaticFieldInitialization(TextStream &s,
+ const AbstractMetaClassCPtr &metaClass)
{
- s << "\nvoid " << getSimpleClassStaticFieldsInitFunctionName(metaClass)
- << "()\n{\n" << indent << "auto dict = reinterpret_cast<PyTypeObject *>("
- << cpythonTypeName(metaClass) << ")->tp_dict;\n";
+ // cpythonTypeName == "Sbk_QRhiShaderResourceBinding_Data_TypeF"
+ QString name = cpythonTypeName(metaClass);
+ const auto parts = QStringView{name}.split(u'_', Qt::SkipEmptyParts);
+ if (parts.size() < 4) {
+ s << "\nPyTypeObject *" << getSimpleClassStaticFieldsInitFunctionName(metaClass)
+ << "(PyObject *module)\n{\n" << indent
+ << "auto *obType = PyObject_GetAttrString(module, \"" << metaClass->name() << "\");\n"
+ << "auto *type = reinterpret_cast<PyTypeObject *>(obType);\n"
+ << "Shiboken::AutoDecRef dict(PepType_GetDict(type));\n";
+ } else {
+ s << "\nPyTypeObject *" << getSimpleClassStaticFieldsInitFunctionName(metaClass)
+ << "(PyObject *module)\n{\n" << indent
+ << "auto *obContainerType = PyObject_GetAttrString(module, \""
+ << parts.at(1) << "\");\n"
+ << "auto *obType = PyObject_GetAttrString(obContainerType, \""
+ << parts.at(2) << "\");\n"
+ << "auto *type = reinterpret_cast<PyTypeObject *>(obType);\n"
+ << "Shiboken::AutoDecRef dict(PepType_GetDict(type));\n";
+ }
for (const AbstractMetaField &field : metaClass->fields()) {
if (field.isStatic()) {
s << "PyDict_SetItemString(dict, \"" << field.name()
@@ -6005,7 +5695,7 @@ void CppGenerator::writeStaticFieldInitialization(TextStream &s, const AbstractM
s << ");\n";
}
}
- s << '\n' << outdent << "}\n";
+ s << "return type;\n" << outdent << "}\n";
}
enum class QtRegisterMetaType
@@ -6013,7 +5703,7 @@ enum class QtRegisterMetaType
None, Pointer, Value
};
-static bool hasQtMetaTypeRegistrationSpec(const AbstractMetaClass *c)
+static bool hasQtMetaTypeRegistrationSpec(const AbstractMetaClassCPtr &c)
{
return c->typeEntry()->qtMetaTypeRegistration() !=
TypeSystem::QtMetaTypeRegistration::Unspecified;
@@ -6022,7 +5712,7 @@ static bool hasQtMetaTypeRegistrationSpec(const AbstractMetaClass *c)
// Returns if and how to register the Qt meta type. By default, "pointer" for
// non-QObject object types and "value" for non-abstract, default-constructible
// value types.
-QtRegisterMetaType qtMetaTypeRegistration(const AbstractMetaClass *c)
+QtRegisterMetaType qtMetaTypeRegistration(const AbstractMetaClassCPtr &c)
{
if (c->isNamespace())
return QtRegisterMetaType::None;
@@ -6041,7 +5731,7 @@ QtRegisterMetaType qtMetaTypeRegistration(const AbstractMetaClass *c)
// Is there a "base" specification in some base class, meaning only the
// base class is to be registered?
- if (auto *base = recurseClassHierarchy(c, hasQtMetaTypeRegistrationSpec)) {
+ if (auto base = recurseClassHierarchy(c, hasQtMetaTypeRegistrationSpec)) {
const auto baseSpec = base->typeEntry()->qtMetaTypeRegistration();
if (baseSpec == TypeSystem::QtMetaTypeRegistration::BaseEnabled)
return QtRegisterMetaType::None;
@@ -6049,7 +5739,7 @@ QtRegisterMetaType qtMetaTypeRegistration(const AbstractMetaClass *c)
// Default.
if (isObject)
- return c->isQObject() ? QtRegisterMetaType::None : QtRegisterMetaType::Pointer;
+ return isQObject(c) ? QtRegisterMetaType::None : QtRegisterMetaType::Pointer;
return !c->isAbstract() && c->isDefaultConstructible()
? QtRegisterMetaType::Value : QtRegisterMetaType::None;
@@ -6057,7 +5747,7 @@ QtRegisterMetaType qtMetaTypeRegistration(const AbstractMetaClass *c)
void CppGenerator::writeInitQtMetaTypeFunctionBody(TextStream &s, const GeneratorContext &context)
{
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
// Gets all class name variants used on different possible scopes
QStringList nameVariants;
if (!context.forSmartPointer())
@@ -6065,7 +5755,7 @@ void CppGenerator::writeInitQtMetaTypeFunctionBody(TextStream &s, const Generato
else
nameVariants << context.preciseType().cppSignature();
- const AbstractMetaClass *enclosingClass = metaClass->enclosingClass();
+ AbstractMetaClassCPtr enclosingClass = metaClass->enclosingClass();
while (enclosingClass) {
if (enclosingClass->typeEntry()->generateCode())
nameVariants << (enclosingClass->name() + u"::"_s + nameVariants.constLast());
@@ -6084,57 +5774,68 @@ void CppGenerator::writeInitQtMetaTypeFunctionBody(TextStream &s, const Generato
case QtRegisterMetaType::None:
break;
case QtRegisterMetaType::Pointer:
- s << "qRegisterMetaType< ::" << className << " *>();\n";
+ s << "qRegisterMetaType< " << m_gsp << className << " *>();\n";
break;
case QtRegisterMetaType::Value:
- for (const QString &name : qAsConst(nameVariants))
- s << "qRegisterMetaType< ::" << className << " >(\"" << name << "\");\n";
+ for (const QString &name : std::as_const(nameVariants))
+ s << "qRegisterMetaType< " << m_gsp << className << " >(\"" << name << "\");\n";
break;
}
for (const AbstractMetaEnum &metaEnum : metaClass->enums()) {
if (!metaEnum.isPrivate() && !metaEnum.isAnonymous()) {
- for (const QString &name : qAsConst(nameVariants)) {
- s << "qRegisterMetaType< ::"
+ for (const QString &name : std::as_const(nameVariants)) {
+ s << "qRegisterMetaType< " << m_gsp
<< metaEnum.typeEntry()->qualifiedCppName() << " >(\""
<< name << "::" << metaEnum.name() << "\");\n";
}
- if (metaEnum.typeEntry()->flags()) {
- QString n = metaEnum.typeEntry()->flags()->originalName();
- s << "qRegisterMetaType< ::" << n << " >(\"" << n << "\");\n";
- }
}
}
}
-void CppGenerator::writeTypeDiscoveryFunction(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::replacePolymorphicIdPlaceHolders(const AbstractMetaClassCPtr &metaClass,
+ QString *id)
+{
+ if (id->contains("%1"_L1)) {
+ QString replacement = " reinterpret_cast< "_L1 + m_gsp + metaClass->qualifiedCppName()
+ + " *>(cptr)"_L1;
+ id->replace("%1"_L1, replacement);
+ }
+ if (id->contains("%B"_L1)) {
+ auto baseClass = metaClass;
+ while (!baseClass->typeEntry()->isPolymorphicBase() && baseClass->baseClass())
+ baseClass = baseClass->baseClass();
+ QString replacement = " reinterpret_cast< "_L1 + m_gsp + baseClass->qualifiedCppName()
+ + " *>(cptr)"_L1;
+ id->replace("%B"_L1, replacement);
+ }
+}
+
+void CppGenerator::writeTypeDiscoveryFunction(TextStream &s,
+ const AbstractMetaClassCPtr &metaClass)
{
QString polymorphicExpr = metaClass->typeEntry()->polymorphicIdValue();
s << "static void *" << cpythonBaseName(metaClass)
- << "_typeDiscovery(void *cptr, PyTypeObject *instanceType)\n{\n" << indent;
+ << "_typeDiscovery(void *cptr, PyTypeObject *instanceType)\n{\n" << indent
+ << sbkUnusedVariableCast("cptr")
+ << sbkUnusedVariableCast("instanceType");
if (!polymorphicExpr.isEmpty()) {
- polymorphicExpr = polymorphicExpr.replace(u"%1"_s,
- u" reinterpret_cast< ::"_s
- + metaClass->qualifiedCppName()
- + u" *>(cptr)"_s);
- s << " if (" << polymorphicExpr << ")\n";
- {
- Indentation indent(s);
- s << "return cptr;\n";
- }
+ replacePolymorphicIdPlaceHolders(metaClass, &polymorphicExpr);
+ s << "if (" << polymorphicExpr << ")\n" << indent
+ << "return cptr;\n" << outdent;
} else if (metaClass->isPolymorphic()) {
const auto &ancestors = metaClass->allTypeSystemAncestors();
- for (auto *ancestor : ancestors) {
- if (ancestor->baseClass())
+ for (const auto &ancestor : ancestors) {
+ if (ancestor->baseClass() && !ancestor->typeEntry()->isPolymorphicBase())
continue;
if (ancestor->isPolymorphic()) {
- s << "if (instanceType == Shiboken::SbkType< ::"
- << ancestor->qualifiedCppName() << " >())\n";
- Indentation indent(s);
- s << "return dynamic_cast< ::" << metaClass->qualifiedCppName()
- << " *>(reinterpret_cast< ::"<< ancestor->qualifiedCppName() << " *>(cptr));\n";
+ s << "if (instanceType == Shiboken::SbkType< " << m_gsp
+ << ancestor->qualifiedCppName() << " >())\n" << indent
+ << "return dynamic_cast< " << getFullTypeName(metaClass)
+ << " *>(reinterpret_cast< "<< getFullTypeName(ancestor)
+ << " *>(cptr));\n" << outdent;
} else {
qCWarning(lcShiboken).noquote().nospace()
<< metaClass->qualifiedCppName() << " inherits from a non polymorphic type ("
@@ -6147,7 +5848,8 @@ void CppGenerator::writeTypeDiscoveryFunction(TextStream &s, const AbstractMetaC
s << "return {};\n" << outdent << "}\n\n";
}
-void CppGenerator::writeSetattroDefinition(TextStream &s, const AbstractMetaClass *metaClass) const
+void CppGenerator::writeSetattroDefinition(TextStream &s,
+ const AbstractMetaClassCPtr &metaClass)
{
s << "static int " << ShibokenGenerator::cpythonSetattroFunctionName(metaClass)
<< "(PyObject *self, PyObject *name, PyObject *value)\n{\n" << indent;
@@ -6157,7 +5859,7 @@ void CppGenerator::writeSetattroDefinition(TextStream &s, const AbstractMetaClas
}
}
-inline void CppGenerator::writeSetattroDefaultReturn(TextStream &s)
+void CppGenerator::writeSetattroDefaultReturn(TextStream &s)
{
s << "return PyObject_GenericSetAttr(self, name, value);\n"
<< outdent << "}\n\n";
@@ -6167,7 +5869,7 @@ void CppGenerator::writeSetattroFunction(TextStream &s, AttroCheck attroCheck,
const GeneratorContext &context) const
{
Q_ASSERT(!context.forSmartPointer());
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
writeSetattroDefinition(s, metaClass);
// PYSIDE-1019: Switch tp_dict before doing tp_setattro.
@@ -6177,58 +5879,36 @@ void CppGenerator::writeSetattroFunction(TextStream &s, AttroCheck attroCheck,
// PYSIDE-803: Detect duck-punching; clear cache if a method is set.
if (attroCheck.testFlag(AttroCheckFlag::SetattroMethodOverride)
&& context.useWrapper()) {
- s << "if (value && PyCallable_Check(value)) {\n" << indent
- << "auto plain_inst = " << cpythonWrapperCPtr(metaClass, u"self"_s) << ";\n"
- << "auto inst = dynamic_cast<" << context.wrapperName() << " *>(plain_inst);\n"
- << "if (inst)\n" << indent
+ s << "if (value != nullptr && PyCallable_Check(value) != 0) {\n" << indent
+ << "auto plain_inst = " << cpythonWrapperCPtr(metaClass, PYTHON_SELF_VAR) << ";\n"
+ << "auto *inst = dynamic_cast<" << context.wrapperName() << " *>(plain_inst);\n"
+ << "if (inst != nullptr)\n" << indent
<< "inst->resetPyMethodCache();\n" << outdent << outdent
<< "}\n";
}
if (attroCheck.testFlag(AttroCheckFlag::SetattroQObject)) {
s << "Shiboken::AutoDecRef pp(reinterpret_cast<PyObject *>(PySide::Property::getObject(self, name)));\n"
- << "if (!pp.isNull())\n";
- Indentation indent(s);
- s << "return PySide::Property::setValue(reinterpret_cast<PySideProperty *>(pp.object()), self, value);\n";
+ << "if (!pp.isNull())\n" << indent
+ << "return PySide::Property::setValue(reinterpret_cast<PySideProperty *>(pp.object()), self, value);\n"
+ << outdent;
}
if (attroCheck.testFlag(AttroCheckFlag::SetattroUser)) {
auto func = AbstractMetaClass::queryFirstFunction(metaClass->functions(),
FunctionQueryOption::SetAttroFunction);
Q_ASSERT(func);
- s << "{\n";
- {
- Indentation indent(s);
- s << "auto " << CPP_SELF_VAR << " = "
- << cpythonWrapperCPtr(metaClass, u"self"_s) << ";\n";
- writeClassCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionAny,
- TypeSystem::TargetLangCode, context);
- }
- s << "}\n";
+ s << "{\n" << indent
+ << "auto " << CPP_SELF_VAR << " = "
+ << cpythonWrapperCPtr(metaClass, PYTHON_SELF_VAR) << ";\n";
+ writeClassCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionAny,
+ TypeSystem::TargetLangCode, context);
+ s << outdent << "}\n";
}
writeSetattroDefaultReturn(s);
}
-static const char smartPtrComment[] =
- "// Try to find the 'name' attribute, by retrieving the PyObject for "
- "the corresponding C++ object held by the smart pointer.\n";
-
-void CppGenerator::writeSmartPointerSetattroFunction(TextStream &s,
- const GeneratorContext &context) const
-{
- Q_ASSERT(context.forSmartPointer());
- writeSetattroDefinition(s, context.metaClass());
- s << smartPtrComment
- << "if (auto *rawObj = PyObject_CallMethod(self, " << SMART_POINTER_GETTER
- << ", 0)) {\n" << indent
- << "if (PyObject_HasAttr(rawObj, name) != 0)\n" << indent
- << "return PyObject_GenericSetAttr(rawObj, name, value);\n" << outdent
- << "Py_DECREF(rawObj);\n" << outdent
- << "}\n";
- writeSetattroDefaultReturn(s);
-}
-
-void CppGenerator::writeGetattroDefinition(TextStream &s, const AbstractMetaClass *metaClass)
+void CppGenerator::writeGetattroDefinition(TextStream &s, const AbstractMetaClassCPtr &metaClass)
{
s << "static PyObject *" << cpythonGetattroFunctionName(metaClass)
<< "(PyObject *self, PyObject *name)\n{\n" << indent;
@@ -6238,10 +5918,10 @@ QString CppGenerator::qObjectGetAttroFunction() const
{
static QString result;
if (result.isEmpty()) {
- auto qobjectClass = AbstractMetaClass::findClass(api().classes(), qObjectT());
+ auto qobjectClass = AbstractMetaClass::findClass(api().classes(), qObjectT);
Q_ASSERT(qobjectClass);
- result = u"PySide::getMetaDataFromQObject("_s
- + cpythonWrapperCPtr(qobjectClass, u"self"_s)
+ result = u"PySide::getHiddenDataFromQObject("_s
+ + cpythonWrapperCPtr(qobjectClass, PYTHON_SELF_VAR)
+ u", self, name)"_s;
}
return result;
@@ -6251,64 +5931,49 @@ void CppGenerator::writeGetattroFunction(TextStream &s, AttroCheck attroCheck,
const GeneratorContext &context) const
{
Q_ASSERT(!context.forSmartPointer());
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
writeGetattroDefinition(s, metaClass);
// PYSIDE-1019: Switch tp_dict before doing tp_getattro.
if (usePySideExtensions())
s << "PySide::Feature::Select(self);\n";
- const QString getattrFunc = usePySideExtensions() && metaClass->isQObject()
+ const QString getattrFunc = usePySideExtensions() && isQObject(metaClass)
? qObjectGetAttroFunction() : u"PyObject_GenericGetAttr(self, name)"_s;
if (attroCheck.testFlag(AttroCheckFlag::GetattroOverloads)) {
s << "// Search the method in the instance dict\n"
<< "auto *ob_dict = SbkObject_GetDict_NoRef(self);\n";
- s << "if (auto *meth = PyDict_GetItem(ob_dict, name)) {\n";
- {
- Indentation indent(s);
- s << "Py_INCREF(meth);\n"
- << "return meth;\n";
- }
- s << "}\n"
+ s << "if (auto *meth = PyDict_GetItem(ob_dict, name)) {\n" << indent
+ << "Py_INCREF(meth);\nreturn meth;\n" << outdent << "}\n"
<< "// Search the method in the type dict\n"
- << "if (Shiboken::Object::isUserType(self)) {\n";
- {
- Indentation indent(s);
- // PYSIDE-772: Perform optimized name mangling.
- s << "Shiboken::AutoDecRef tmp(_Pep_PrivateMangle(self, name));\n"
- << "if (auto *meth = PyDict_GetItem(Py_TYPE(self)->tp_dict, tmp)) {\n";
- {
- Indentation indent(s);
- // PYSIDE-1523: PyFunction_Check is not accepting compiled functions.
- s << "if (strcmp(Py_TYPE(meth)->tp_name, \"compiled_function\") == 0)\n";
- {
- Indentation indent(s);
- s << "return Py_TYPE(meth)->tp_descr_get(meth, self, nullptr);\n";
- }
- s << "return PyFunction_Check(meth) ? PyMethod_New(meth, self)\n"
- << " : " << getattrFunc << ";\n";
- }
- s << "}\n";
- }
- s << "}\n";
+ << "if (Shiboken::Object::isUserType(self)) {\n" << indent;
+ // PYSIDE-772: Perform optimized name mangling.
+ s << "Shiboken::AutoDecRef tmp(_Pep_PrivateMangle(self, name));\n"
+ << "Shiboken::AutoDecRef tpDict(PepType_GetDict(Py_TYPE(self)));\n"
+ << "if (auto *meth = PyDict_GetItem(tpDict.object(), tmp)) {\n" << indent;
+ // PYSIDE-1523: PyFunction_Check is not accepting compiled functions.
+ s << "if (std::strcmp(Py_TYPE(meth)->tp_name, \"compiled_function\") == 0) {\n" << indent
+ << "auto descrGetFunc = "
+ << pyTypeGetSlot("descrgetfunc", "Py_TYPE(meth)", "Py_tp_descr_get") << ";\n"
+ << "return descrGetFunc(meth, self, nullptr);\n" << outdent
+ << "}\nreturn PyFunction_Check(meth) ? PyMethod_New(meth, self)\n"
+ << " : " << getattrFunc << ";\n" << outdent
+ << "}\n" << outdent << "}\n";
const auto &funcs = getMethodsWithBothStaticAndNonStaticMethods(metaClass);
for (const auto &func : funcs) {
QString defName = cpythonMethodDefinitionName(func);
- s << "static PyMethodDef non_static_" << defName << " = {\n";
- {
- Indentation indent(s);
- s << defName << ".ml_name,\n"
- << defName << ".ml_meth,\n"
- << defName << ".ml_flags & (~METH_STATIC),\n"
- << defName << ".ml_doc,\n";
- }
- s << "};\n"
+ s << "static PyMethodDef non_static_" << defName << " = {\n" << indent
+ << defName << ".ml_name,\n"
+ << defName << ".ml_meth,\n"
+ << defName << ".ml_flags & (~METH_STATIC),\n"
+ << defName << ".ml_doc,\n" << outdent
+ << "};\n"
<< "if (Shiboken::String::compare(name, \""
- << func->definitionNames().constFirst() << "\") == 0)\n";
- Indentation indent(s);
- s << "return PyCFunction_NewEx(&non_static_" << defName << ", self, 0);\n";
+ << func->definitionNames().constFirst() << "\") == 0)\n" << indent
+ << "return PyCFunction_NewEx(&non_static_" << defName << ", self, 0);\n"
+ << outdent;
}
}
@@ -6316,64 +5981,17 @@ void CppGenerator::writeGetattroFunction(TextStream &s, AttroCheck attroCheck,
auto func = AbstractMetaClass::queryFirstFunction(metaClass->functions(),
FunctionQueryOption::GetAttroFunction);
Q_ASSERT(func);
- s << "{\n";
- {
- Indentation indent(s);
- s << "auto " << CPP_SELF_VAR << " = "
- << cpythonWrapperCPtr(metaClass, u"self"_s) << ";\n";
- writeClassCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionAny,
- TypeSystem::TargetLangCode, context);
- }
- s << "}\n";
+ s << "{\n" << indent
+ << "auto " << CPP_SELF_VAR << " = "
+ << cpythonWrapperCPtr(metaClass, PYTHON_SELF_VAR) << ";\n";
+ writeClassCodeSnips(s, func->injectedCodeSnips(), TypeSystem::CodeSnipPositionAny,
+ TypeSystem::TargetLangCode, context);
+ s << outdent << "}\n";
}
s << "return " << getattrFunc << ";\n" << outdent << "}\n\n";
}
-void CppGenerator::writeSmartPointerGetattroFunction(TextStream &s,
- const GeneratorContext &context,
- const BoolCastFunctionOptional &boolCast)
-{
- Q_ASSERT(context.forSmartPointer());
- const AbstractMetaClass *metaClass = context.metaClass();
- writeGetattroDefinition(s, metaClass);
- s << "PyObject *tmp = PyObject_GenericGetAttr(self, name);\n"
- << "if (tmp)\n" << indent << "return tmp;\n" << outdent
- << "if (PyErr_ExceptionMatches(PyExc_AttributeError) == 0)\n"
- << indent << "return nullptr;\n" << outdent
- << "PyErr_Clear();\n";
-
- if (boolCast.has_value()) {
- writeSmartPointerCppSelfDefinition(s, context);
- s << "if (";
- writeNbBoolExpression(s, boolCast.value(), true /* invert */);
- s << ") {\n" << indent
- << R"(PyTypeObject *tp = Py_TYPE(self);
-PyErr_Format(PyExc_AttributeError, "Attempt to retrieve '%s' from null object '%s'.",
- Shiboken::String::toCString(name), tp->tp_name);
-return nullptr;
-)" << outdent << "}\n";
- }
-
- // This generates the code which dispatches access to member functions
- // and fields from the smart pointer to its pointee.
- s << smartPtrComment
- << "if (auto *rawObj = PyObject_CallMethod(self, "
- << SMART_POINTER_GETTER << ", 0)) {\n" << indent
- << "if (auto *attribute = PyObject_GetAttr(rawObj, name))\n"
- << indent << "tmp = attribute;\n" << outdent
- << "Py_DECREF(rawObj);\n" << outdent
- << "}\n"
- << "if (!tmp) {\n" << indent
- << R"(PyTypeObject *tp = Py_TYPE(self);
-PyErr_Format(PyExc_AttributeError,
- "'%.50s' object has no attribute '%.400s'",
- tp->tp_name, Shiboken::String::toCString(name));
-)" << outdent
- << "}\n"
- << "return tmp;\n" << outdent << "}\n\n";
-}
-
void CppGenerator::writeNbBoolExpression(TextStream &s, const BoolCastFunction &f,
bool invert)
{
@@ -6390,7 +6008,7 @@ void CppGenerator::writeNbBoolExpression(TextStream &s, const BoolCastFunction &
void CppGenerator::writeNbBoolFunction(const GeneratorContext &context,
const BoolCastFunction &f,
- TextStream &s) const
+ TextStream &s)
{
s << "static int " << cpythonBaseName(context.metaClass()) << "___nb_bool(PyObject *self)\n"
<< "{\n" << indent;
@@ -6414,28 +6032,57 @@ void CppGenerator::writeNbBoolFunction(const GeneratorContext &context,
// function.
void CppGenerator::writeInitFunc(TextStream &declStr, TextStream &callStr,
const QString &initFunctionName,
- const TypeEntry *enclosingEntry)
+ const TypeEntryCPtr &enclosingEntry,
+ const QString &pythonName, bool lazy)
{
- const bool hasParent =
- enclosingEntry && enclosingEntry->type() != TypeEntry::TypeSystemType;
- declStr << "void init_" << initFunctionName << "(PyObject *"
+ const QString functionName = "init_"_L1 + initFunctionName;
+ const bool hasParent = enclosingEntry && enclosingEntry->type() != TypeEntry::TypeSystemType;
+ declStr << "PyTypeObject *" << functionName << "(PyObject *"
<< (hasParent ? "enclosingClass" : "module") << ");\n";
- callStr << "init_" << initFunctionName;
- if (hasParent) {
- callStr << "(reinterpret_cast<PyTypeObject *>("
- << cpythonTypeNameExt(enclosingEntry) << ")->tp_dict);\n";
+
+ if (!lazy) {
+ const QString enclosing = hasParent
+ ? "reinterpret_cast<PyObject *>("_L1 + cpythonTypeNameExt(enclosingEntry) + u')'
+ : "module"_L1;
+ callStr << functionName << '(' << enclosing << ");\n";
+ } else if (hasParent) {
+ const QString &enclosingName = enclosingEntry->name();
+ const auto parts = QStringView{enclosingName}.split(u"::", Qt::SkipEmptyParts);
+ callStr << "Shiboken::Module::AddTypeCreationFunction("
+ << "module, \"" << pythonName << "\", " << functionName << ", \"";
+ for (qsizetype i = 0; i < parts.size(); ++i) {
+ if (i > 0)
+ callStr << "\", \"";
+ callStr << parts.at(i);
+ }
+ callStr << "\");\n";
} else {
- callStr << "(module);\n";
+ callStr << "Shiboken::Module::AddTypeCreationFunction("
+ << "module, \"" << pythonName << "\", "
+ << "init_" << initFunctionName << ");\n";
}
}
+static void writeSubModuleHandling(TextStream &s, const QString &moduleName,
+ const QString &subModuleOf)
+{
+ s << "{\n" << indent
+ << "Shiboken::AutoDecRef parentModule(Shiboken::Module::import(\""
+ << subModuleOf << "\"));\n"
+ << "if (parentModule.isNull())\n" << indent
+ << "return nullptr;\n" << outdent
+ << "if (PyModule_AddObject(parentModule.object(), \"" << moduleName
+ << "\", module) < 0)\n"
+ << indent << "return nullptr;\n" << outdent << outdent << "}\n";
+}
+
bool CppGenerator::finishGeneration()
{
//Generate CPython wrapper file
StringStream s_classInitDecl(TextStream::Language::Cpp);
StringStream s_classPythonDefines(TextStream::Language::Cpp);
- QSet<Include> includes;
+ std::set<Include> includes;
StringStream s_globalFunctionImpl(TextStream::Language::Cpp);
StringStream s_globalFunctionDef(TextStream::Language::Cpp);
StringStream signatureStream(TextStream::Language::Cpp);
@@ -6444,8 +6091,8 @@ bool CppGenerator::finishGeneration()
for (auto it = functionGroups.cbegin(), end = functionGroups.cend(); it != end; ++it) {
const AbstractMetaFunctionCList &overloads = it.value();
for (const auto &func : overloads) {
- if (func->typeEntry())
- includes << func->typeEntry()->include();
+ if (auto te = func->typeEntry())
+ includes.insert(te->include());
}
if (overloads.isEmpty())
@@ -6461,26 +6108,61 @@ bool CppGenerator::finishGeneration()
}
AbstractMetaClassCList classesWithStaticFields;
- for (auto cls : api().classes()){
- auto *te = cls->typeEntry();
+ for (const auto &cls : api().classes()){
+ auto te = cls->typeEntry();
if (shouldGenerate(te)) {
+ const bool hasConfigCondition = te->hasConfigCondition();
+ if (hasConfigCondition) {
+ s_classInitDecl << te->configCondition() << '\n';
+ s_classPythonDefines << te->configCondition() << '\n';
+ }
writeInitFunc(s_classInitDecl, s_classPythonDefines,
getSimpleClassInitFunctionName(cls),
- te->targetLangEnclosingEntry());
+ targetLangEnclosingEntry(te), cls->name());
if (cls->hasStaticFields()) {
- s_classInitDecl << "void "
- << getSimpleClassStaticFieldsInitFunctionName(cls) << "();\n";
+ s_classInitDecl << "PyTypeObject *"
+ << getSimpleClassStaticFieldsInitFunctionName(cls) << "(PyObject *module);\n";
classesWithStaticFields.append(cls);
}
+ if (hasConfigCondition) {
+ s_classInitDecl << "#endif\n";
+ s_classPythonDefines << "#endif\n";
+ }
}
}
// Initialize smart pointer types.
for (const auto &smp : api().instantiatedSmartPointers()) {
GeneratorContext context = contextForSmartPointer(smp.specialized, smp.type);
+ const auto enclosingClass = context.metaClass()->enclosingClass();
+ auto enclosingTypeEntry = enclosingClass
+ ? enclosingClass->typeEntry()
+ : targetLangEnclosingEntry(smp.type.typeEntry());
+
writeInitFunc(s_classInitDecl, s_classPythonDefines,
getInitFunctionName(context),
- smp.type.typeEntry()->targetLangEnclosingEntry());
+ enclosingTypeEntry, smp.type.name());
+ includes.insert(smp.type.instantiations().constFirst().typeEntry()->include());
+ }
+
+ for (auto &instantiatedContainer : api().instantiatedContainers()) {
+ includes.insert(instantiatedContainer.typeEntry()->include());
+ for (const auto &inst : instantiatedContainer.instantiations())
+ includes.insert(inst.typeEntry()->include());
+ }
+
+ const ExtendedConverterData extendedConverters = getExtendedConverters();
+ for (auto it = extendedConverters.cbegin(), end = extendedConverters.cend(); it != end; ++it) {
+ TypeEntryCPtr te = it.key();
+ includes.insert(te->include());
+ for (const auto &metaClass : it.value())
+ includes.insert(metaClass->typeEntry()->include());
+ }
+
+ const QList<CustomConversionPtr> &typeConversions = getPrimitiveCustomConversions();
+ for (const auto &c : typeConversions) {
+ if (auto te = c->ownerType())
+ includes.insert(te->include());
}
QString moduleFileName(outputDirectory() + u'/' + subDirectoryForPackage(packageName()));
@@ -6512,55 +6194,64 @@ bool CppGenerator::finishGeneration()
}
s << "#include \"" << getModuleHeaderFileName() << '"' << "\n\n";
- for (const Include &include : qAsConst(includes))
+ for (const Include &include : includes)
s << include;
s << '\n';
// Global enums
AbstractMetaEnumList globalEnums = api().globalEnums();
- for (const AbstractMetaClass *nsp : invisibleTopNamespaces())
+ for (const auto &nsp : invisibleTopNamespaces()) {
+ const auto oldSize = globalEnums.size();
nsp->getEnumsToBeGenerated(&globalEnums);
+ if (globalEnums.size() > oldSize)
+ s << nsp->typeEntry()->include();
+ }
TypeDatabase *typeDb = TypeDatabase::instance();
- const TypeSystemTypeEntry *moduleEntry = typeDb->defaultTypeSystemType();
+ TypeSystemTypeEntryCPtr moduleEntry = typeDb->defaultTypeSystemType();
Q_ASSERT(moduleEntry);
s << '\n';
// Extra includes
QList<Include> extraIncludes = moduleEntry->extraIncludes();
- for (const AbstractMetaEnum &cppEnum : qAsConst(globalEnums))
+ for (const AbstractMetaEnum &cppEnum : std::as_const(globalEnums))
extraIncludes.append(cppEnum.typeEntry()->extraIncludes());
if (!extraIncludes.isEmpty()) {
s << "// Extra includes\n";
std::sort(extraIncludes.begin(), extraIncludes.end());
- for (const Include &inc : qAsConst(extraIncludes))
+ for (const Include &inc : std::as_const(extraIncludes))
s << inc;
s << '\n';
}
+ // FIXME PYSIDE-7: Remove backwards compatible structure
s << "// Current module's type array.\n"
- << "PyTypeObject **" << cppApiVariableName() << " = nullptr;\n"
+ << "Shiboken::Module::TypeInitStruct *" << cppApiVariableName() << " = nullptr;\n"
+ << "// Backwards compatible structure with identical indexing.\n"
+ << "PyTypeObject **" << cppApiVariableNameOld() << " = nullptr;\n"
<< "// Current module's PyObject pointer.\n"
<< "PyObject *" << pythonModuleObjectName() << " = nullptr;\n"
<< "// Current module's converter array.\n"
- << "SbkConverter **" << convertersVariableName() << " = nullptr;\n";
+ << "SbkConverter **" << convertersVariableName() << " = nullptr;\n\n";
const CodeSnipList snips = moduleEntry->codeSnips();
// module inject-code native/beginning
- if (!snips.isEmpty())
- writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::NativeCode);
+ writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::NativeCode);
// cleanup staticMetaObject attribute
if (usePySideExtensions()) {
+ QString iType = cppApiVariableName() + "[i].type"_L1;
+ QString iName = cppApiVariableName() + "[i].fullName"_L1;
+
s << "void cleanTypesAttributes() {\n" << indent
<< "static PyObject *attrName = Shiboken::PyName::qtStaticMetaObject();\n"
- << "for (int i = 0, imax = SBK_" << moduleName()
- << "_IDX_COUNT; i < imax; i++) {\n" << indent
- << "PyObject *pyType = reinterpret_cast<PyObject *>(" << cppApiVariableName() << "[i]);\n"
- << "if (pyType && PyObject_HasAttr(pyType, attrName))\n" << indent
+ << "const int imax = SBK_" << moduleName() << "_IDX_COUNT;\n"
+ << "for (int i = 0; i < imax && " << iName << " != nullptr; ++i) {\n" << indent
+ << "auto *pyType = reinterpret_cast<PyObject *>(" << iType << ");\n"
+ << "if (pyType != nullptr && PyObject_HasAttr(pyType, attrName))\n" << indent
<< "PyObject_SetAttr(pyType, attrName, Py_None);\n" << outdent
- << outdent << "}\n" << outdent << "}\n";
+ << outdent << "}\n" << outdent << "}\n\n";
}
s << "// Global functions "
@@ -6568,7 +6259,7 @@ bool CppGenerator::finishGeneration()
<< s_globalFunctionImpl.toString() << '\n'
<< "static PyMethodDef " << moduleName() << "_methods[] = {\n" << indent
<< s_globalFunctionDef.toString()
- << "{0} // Sentinel\n" << outdent << "};\n\n"
+ << METHOD_DEF_SENTINEL << outdent << "};\n\n"
<< "// Classes initialization functions "
<< "------------------------------------------------------------\n"
<< s_classInitDecl.toString() << '\n';
@@ -6578,12 +6269,8 @@ bool CppGenerator::finishGeneration()
s << "// Enum definitions "
<< "------------------------------------------------------------\n";
- for (const AbstractMetaEnum &cppEnum : qAsConst(globalEnums)) {
- if (cppEnum.isAnonymous() || cppEnum.isPrivate())
- continue;
+ for (const AbstractMetaEnum &cppEnum : std::as_const(globalEnums))
writeEnumConverterFunctions(s, cppEnum);
- s << '\n';
- }
if (convImpl.size() > 0) {
s << "// Enum converters "
@@ -6593,7 +6280,6 @@ bool CppGenerator::finishGeneration()
<< "} // namespace Shiboken\n\n";
}
- writeFlagsNumberMethodsDefinitions(s, globalEnums);
s << '\n';
}
@@ -6601,20 +6287,19 @@ bool CppGenerator::finishGeneration()
if (!requiredModules.isEmpty())
s << "// Required modules' type and converter arrays.\n";
for (const QString &requiredModule : requiredModules) {
- s << "PyTypeObject **" << cppApiVariableName(requiredModule) << ";\n"
+ s << "Shiboken::Module::TypeInitStruct *" << cppApiVariableName(requiredModule) << ";\n"
<< "SbkConverter **" << convertersVariableName(requiredModule) << ";\n";
}
s << "\n// Module initialization "
<< "------------------------------------------------------------\n";
- ExtendedConverterData extendedConverters = getExtendedConverters();
if (!extendedConverters.isEmpty()) {
s << '\n' << "// Extended Converters.\n\n";
for (ExtendedConverterData::const_iterator it = extendedConverters.cbegin(), end = extendedConverters.cend(); it != end; ++it) {
- const TypeEntry *externalType = it.key();
+ TypeEntryCPtr externalType = it.key();
s << "// Extended implicit conversions for "
<< externalType->qualifiedTargetLangName() << '.' << '\n';
- for (const AbstractMetaClass *sourceClass : it.value()) {
+ for (const auto &sourceClass : it.value()) {
AbstractMetaType sourceType = AbstractMetaType::fromAbstractMetaClass(sourceClass);
AbstractMetaType targetType = AbstractMetaType::fromTypeEntry(externalType);
writePythonToCppConversionFunctions(s, sourceType, targetType);
@@ -6622,10 +6307,9 @@ bool CppGenerator::finishGeneration()
}
}
- const QList<const CustomConversion *> &typeConversions = getPrimitiveCustomConversions();
if (!typeConversions.isEmpty()) {
s << "\n// Primitive Type converters.\n\n";
- for (const CustomConversion *conversion : typeConversions) {
+ for (const auto &conversion : typeConversions) {
s << "// C++ to Python conversion for primitive type '" << conversion->ownerType()->qualifiedCppName() << "'.\n";
writeCppToPythonFunction(s, conversion);
writeCustomConverterFunctions(s, conversion);
@@ -6635,14 +6319,17 @@ bool CppGenerator::finishGeneration()
QHash<AbstractMetaType, OpaqueContainerData> opaqueContainers;
const auto &containers = api().instantiatedContainers();
+ QSet<AbstractMetaType> valueConverters;
if (!containers.isEmpty()) {
s << "// Container Type converters.\n\n";
for (const AbstractMetaType &container : containers) {
- s << "// C++ to Python conversion for container type '" << container.cppSignature() << "'.\n";
+ s << "// C++ to Python conversion for container type '"
+ << container.cppSignature() << "'.\n";
writeContainerConverterFunctions(s, container);
if (container.generateOpaqueContainer()) {
- opaqueContainers.insert(container,
- writeOpaqueContainerConverterFunctions(s, container));
+ auto data = writeOpaqueContainerConverterFunctions(s, container,
+ &valueConverters);
+ opaqueContainers.insert(container, data);
}
}
s << '\n';
@@ -6674,6 +6361,8 @@ bool CppGenerator::finishGeneration()
// PYSIDE-510: Create a signatures string for the introspection feature.
writeSignatureStrings(s, signatureStream.toString(), moduleName(), "global functions");
+ writeInitInheritance(s);
+
// Write module init function
const QString globalModuleVar = pythonModuleObjectName();
s << "extern \"C\" LIBSHIBOKEN_EXPORT PyObject *PyInit_"
@@ -6683,8 +6372,8 @@ bool CppGenerator::finishGeneration()
<< indent << "return " << globalModuleVar << ";\n" << outdent;
// module inject-code target/beginning
- if (!snips.isEmpty())
- writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning, TypeSystem::TargetLangCode);
+ writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionBeginning,
+ TypeSystem::TargetLangCode);
for (const QString &requiredModule : requiredModules) {
s << "{\n" << indent
@@ -6700,9 +6389,25 @@ bool CppGenerator::finishGeneration()
int maxTypeIndex = getMaxTypeIndex() + api().instantiatedSmartPointers().size();
if (maxTypeIndex) {
- s << "// Create an array of wrapper types for the current module.\n"
- << "static PyTypeObject *cppApi[SBK_" << moduleName() << "_IDX_COUNT];\n"
- << cppApiVariableName() << " = cppApi;\n\n";
+ s << "// Create an array of wrapper types/names for the current module.\n"
+ << "static Shiboken::Module::TypeInitStruct cppApi[] = {\n" << indent;
+
+ // Windows did not like an array of QString.
+ QStringList typeNames;
+ for (int idx = 0; idx < maxTypeIndex; ++idx)
+ typeNames.append("+++ unknown entry #"_L1 + QString::number(idx)
+ + " in "_L1 + moduleName());
+
+ collectFullTypeNamesArray(typeNames);
+
+ for (auto typeName : typeNames)
+ s << "{nullptr, \"" << typeName << "\"},\n";
+
+ s << "{nullptr, nullptr}\n" << outdent << "};\n"
+ << "// The new global structure consisting of (type, name) pairs.\n"
+ << cppApiVariableName() << " = cppApi;\n"
+ << "// The backward compatible alias with upper case indexes.\n"
+ << cppApiVariableNameOld() << " = reinterpret_cast<PyTypeObject **>(cppApi);\n\n";
}
s << "// Create an array of primitive type converters for the current module.\n"
@@ -6712,13 +6417,18 @@ bool CppGenerator::finishGeneration()
<< "PyObject *module = Shiboken::Module::create(\"" << moduleName()
<< "\", &moduledef);\n\n"
<< "// Make module available from global scope\n"
- << globalModuleVar << " = module;\n\n"
- << "// Initialize classes in the type system\n"
+ << globalModuleVar << " = module;\n\n";
+
+ const QString subModuleOf = typeDb->defaultTypeSystemType()->subModuleOf();
+ if (!subModuleOf.isEmpty())
+ writeSubModuleHandling(s, moduleName(), subModuleOf);
+
+ s << "// Initialize classes in the type system\n"
<< s_classPythonDefines.toString();
if (!typeConversions.isEmpty()) {
s << '\n';
- for (const CustomConversion *conversion : typeConversions) {
+ for (const auto &conversion : typeConversions) {
writePrimitiveConverterInitialization(s, conversion);
s << '\n';
}
@@ -6727,7 +6437,7 @@ bool CppGenerator::finishGeneration()
if (!containers.isEmpty()) {
s << '\n';
for (const AbstractMetaType &container : containers) {
- const QString converterObj = writeContainerConverterInitialization(s, container);
+ const QString converterObj = writeContainerConverterInitialization(s, container, api());
const auto it = opaqueContainers.constFind(container);
if (it != opaqueContainers.constEnd()) {
writeSetPythonToCppPointerConversion(s, converterObj,
@@ -6762,24 +6472,17 @@ bool CppGenerator::finishGeneration()
}
}
- writeEnumsInitialization(s, globalEnums, ErrorReturn::Default);
+ writeEnumsInitialization(s, globalEnums);
s << "// Register primitive types converters.\n";
- const PrimitiveTypeEntryList &primitiveTypeList = primitiveTypes();
- for (const PrimitiveTypeEntry *pte : primitiveTypeList) {
- if (!pte->generateCode() || !pte->isCppPrimitive())
+ const PrimitiveTypeEntryCList &primitiveTypeList = primitiveTypes();
+ for (const auto &pte : primitiveTypeList) {
+ if (!pte->generateCode() || !isCppPrimitive(pte))
continue;
if (!pte->referencesType())
continue;
- const TypeEntry *referencedType = pte->basicReferencedTypeEntry();
- QString converter = converterObject(referencedType);
- QStringList cppSignature = pte->qualifiedCppName().split(u"::"_s, Qt::SkipEmptyParts);
- while (!cppSignature.isEmpty()) {
- QString signature = cppSignature.join(u"::"_s);
- s << "Shiboken::Conversions::registerConverterName("
- << converter << ", \"" << signature << "\");\n";
- cppSignature.removeFirst();
- }
+ TypeEntryCPtr referencedType = basicReferencedTypeEntry(pte);
+ registerConverterInScopes(s, pte->qualifiedCppName(), converterObject(referencedType));
}
s << '\n';
@@ -6791,27 +6494,29 @@ bool CppGenerator::finishGeneration()
// of the previously registered types (PYSIDE-1529).
if (!classesWithStaticFields.isEmpty()) {
s << "\n// Static field initialization\n";
- for (auto cls : qAsConst(classesWithStaticFields))
- s << getSimpleClassStaticFieldsInitFunctionName(cls) << "();\n";
+ for (const auto &cls : std::as_const(classesWithStaticFields)) {
+ ConfigurableScope configScope(s, cls->typeEntry());
+ s << getSimpleClassStaticFieldsInitFunctionName(cls) << "(module);\n";
+ }
}
- s << "\nif (PyErr_Occurred()) {\n" << indent
+ s << '\n' << initInheritanceFunction << "();\n"
+ << "\nif (" << shibokenErrorsOccurred << ") {\n" << indent
<< "PyErr_Print();\n"
<< "Py_FatalError(\"can't initialize module " << moduleName() << "\");\n"
<< outdent << "}\n";
// module inject-code target/end
- if (!snips.isEmpty())
- writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::TargetLangCode);
+ writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::TargetLangCode);
// module inject-code native/end
- if (!snips.isEmpty())
- writeCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::NativeCode);
+ writeModuleCodeSnips(s, snips, TypeSystem::CodeSnipPositionEnd, TypeSystem::NativeCode);
if (usePySideExtensions()) {
- for (const AbstractMetaEnum &metaEnum : qAsConst(globalEnums))
+ for (const AbstractMetaEnum &metaEnum : std::as_const(globalEnums))
if (!metaEnum.isAnonymous()) {
- s << "qRegisterMetaType< ::" << metaEnum.typeEntry()->qualifiedCppName()
+ ConfigurableScope configScope(s, metaEnum.typeEntry());
+ s << "qRegisterMetaType< " << getFullTypeName(metaEnum.typeEntry())
<< " >(\"" << metaEnum.name() << "\");\n";
}
@@ -6819,7 +6524,7 @@ bool CppGenerator::finishGeneration()
s << "PySide::registerCleanupFunction(cleanTypesAttributes);\n\n";
}
- // finish the rest of __signature__ initialization.
+ // finish the rest of get_signature() initialization.
s << "FinishSignatureInitialization(module, " << moduleName()
<< "_SignatureStrings);\n"
<< "\nreturn module;\n" << outdent << "}\n";
@@ -6845,17 +6550,17 @@ static bool useParentHeuristics(const ApiExtractorResult &api,
{
if (!ComplexTypeEntry::isParentManagementEnabled()) // FIXME PYSIDE 7: Remove this
return true;
- auto *owner = func->ownerClass();
- if (owner == nullptr)
+ const auto owner = func->ownerClass();
+ if (!owner)
return false;
- auto *ownerEntry = owner->parentManagementEntry();
- if (ownerEntry == nullptr)
+ auto ownerEntry = parentManagementEntry(owner);
+ if (!ownerEntry)
return false;
- auto *argTypeEntry = argType.typeEntry();
+ auto argTypeEntry = argType.typeEntry();
if (!argTypeEntry->isComplex())
return false;
- auto *argClass = AbstractMetaClass::findClass(api.classes(), argTypeEntry);
- return argClass != nullptr && argClass->parentManagementEntry() == ownerEntry;
+ const auto argClass = AbstractMetaClass::findClass(api.classes(), argTypeEntry);
+ return argClass && parentManagementEntry(argClass) == ownerEntry;
}
bool CppGenerator::writeParentChildManagement(TextStream &s, const AbstractMetaFunctionCPtr &func,
@@ -6894,7 +6599,7 @@ bool CppGenerator::writeParentChildManagement(TextStream &s, const AbstractMetaF
if (parentIndex == 0) {
parentVariable = PYTHON_RETURN_VAR;
} else if (parentIndex == -1) {
- parentVariable = u"self"_s;
+ parentVariable = PYTHON_SELF_VAR;
} else {
parentVariable = usePyArgs
? pythonArgsAt(parentIndex - 1) : PYTHON_ARG;
@@ -6904,7 +6609,7 @@ bool CppGenerator::writeParentChildManagement(TextStream &s, const AbstractMetaF
if (childIndex == 0) {
childVariable = PYTHON_RETURN_VAR;
} else if (childIndex == -1) {
- childVariable = u"self"_s;
+ childVariable = PYTHON_SELF_VAR;
} else {
childVariable = usePyArgs
? pythonArgsAt(childIndex - 1) : PYTHON_ARG;
@@ -6961,9 +6666,9 @@ void CppGenerator::writeReturnValueHeuristics(TextStream &s, const AbstractMetaF
}
}
-void CppGenerator::writeHashFunction(TextStream &s, const GeneratorContext &context) const
+void CppGenerator::writeHashFunction(TextStream &s, const GeneratorContext &context)
{
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
const char hashType[] = "Py_hash_t";
s << "static " << hashType << ' ' << cpythonBaseName(metaClass)
<< "_HashFunc(PyObject *self)\n{\n" << indent;
@@ -6987,7 +6692,7 @@ void CppGenerator::writeHashFunction(TextStream &s, const GeneratorContext &cont
void CppGenerator::writeDefaultSequenceMethods(TextStream &s,
const GeneratorContext &context) const
{
- const AbstractMetaClass *metaClass = context.metaClass();
+ const auto metaClass = context.metaClass();
ErrorReturn errorReturn = ErrorReturn::Zero;
// __len__
@@ -7054,14 +6759,20 @@ void CppGenerator::writeIndexError(TextStream &s, const QString &errorMsg,
<< errorReturn << outdent << "}\n";
}
+QString CppGenerator::writeReprFunctionHeader(TextStream &s, const GeneratorContext &context)
+{
+ QString funcName = cpythonBaseName(context.metaClass()) + REPR_FUNCTION;
+ s << "extern \"C\"\n{\n"
+ << "static PyObject *" << funcName << "(PyObject *self)\n{\n" << indent;
+ return funcName;
+}
+
QString CppGenerator::writeReprFunction(TextStream &s,
const GeneratorContext &context,
- uint indirections) const
+ uint indirections)
{
- const AbstractMetaClass *metaClass = context.metaClass();
- QString funcName = cpythonBaseName(metaClass) + reprFunction();
- s << "extern \"C\"\n{\n"
- << "static PyObject *" << funcName << "(PyObject *self)\n{\n" << indent;
+ const auto metaClass = context.metaClass();
+ QString funcName = writeReprFunctionHeader(s, context);
writeCppSelfDefinition(s, context);
s << R"(QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
@@ -7075,25 +6786,22 @@ QByteArray str = buffer.data();
const auto idx = str.indexOf('(');
auto *typeName = Py_TYPE(self)->tp_name;
if (idx >= 0)
-)";
- {
- Indentation indent(s);
- s << "str.replace(0, idx, typeName);\n";
- }
- s << "str = str.trimmed();\n"
- << "PyObject *mod = PyDict_GetItem(Py_TYPE(self)->tp_dict, Shiboken::PyMagicName::module());\n";
+)" << indent << "str.replace(0, idx, typeName);\n" << outdent
+ << "str = str.trimmed();\n"
+ << "Shiboken::AutoDecRef tpDict(PepType_GetDict(Py_TYPE(self)));\n"
+ << "PyObject *mod = PyDict_GetItem(tpDict.object(), Shiboken::PyMagicName::module());\n";
// PYSIDE-595: The introduction of heap types has the side effect that the module name
// is always prepended to the type name. Therefore the strchr check:
- s << "if (mod != nullptr && std::strchr(typeName, '.') == nullptr)\n";
- {
- Indentation indent(s);
- s << "return Shiboken::String::fromFormat(\"<%s.%s at %p>\", Shiboken::String::toCString(mod), str.constData(), self);\n";
- }
- s << "else\n";
- {
- Indentation indent(s);
- s << "return Shiboken::String::fromFormat(\"<%s at %p>\", str.constData(), self);\n";
- }
- s << outdent << "}\n} // extern C\n\n";
+ s << "if (mod != nullptr && std::strchr(typeName, '.') == nullptr)\n" << indent
+ << "return Shiboken::String::fromFormat(\"<%s.%s at %p>\","
+ " Shiboken::String::toCString(mod), str.constData(), self);\n"
+ << outdent
+ << "return Shiboken::String::fromFormat(\"<%s at %p>\", str.constData(), self);\n";
+ writeReprFunctionFooter(s);
return funcName;
}
+
+void CppGenerator::writeReprFunctionFooter(TextStream &s)
+{
+ s << outdent << "}\n} // extern C\n\n";
+}