diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-09-17 07:56:30 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-09-17 08:22:55 +0200 |
commit | 4c5e405e691f707f2765b5f920c28213a36d22e5 (patch) | |
tree | 48000f220fc2c4365b276146e3d08a05c3a90312 /sources/shiboken2/generator/shiboken2 | |
parent | 53181fb95d3884cc067d005f37accd92d128bccc (diff) | |
parent | 38814354ff6a30258b79947304fd3a6be4dc7089 (diff) |
Merge remote-tracking branch 'origin/5.15' into dev
Change-Id: I8aa48d07067c45c888c73af87314f6a88c2a6e14
Diffstat (limited to 'sources/shiboken2/generator/shiboken2')
-rw-r--r-- | sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 66 | ||||
-rw-r--r-- | sources/shiboken2/generator/shiboken2/headergenerator.cpp | 6 |
2 files changed, 71 insertions, 1 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index 5e6ad9b23..7217595ee 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -288,6 +288,34 @@ static inline bool canGenerateFieldSetter(const AbstractMetaField *field) return !type->isConstant() || isPointerToConst(type); } +static bool isStdSetterName(QString setterName, QString propertyName) +{ + return setterName.size() == propertyName.size() + 3 + && setterName.startsWith(QLatin1String("set")) + && setterName.endsWith(QStringView{propertyName}.right(propertyName.size() - 1)) + && setterName.at(3) == propertyName.at(0).toUpper(); +} + +static QString buildPropertyString(QPropertySpec *spec) +{ + QString text; + text += QLatin1Char('"'); + text += spec->name(); + text += QLatin1Char(':'); + + if (spec->read() != spec->name()) + text += spec->read(); + + if (!spec->write().isEmpty()) { + text += QLatin1Char(':'); + if (!isStdSetterName(spec->write(), spec->name())) + text += spec->write(); + } + + text += QLatin1Char('"'); + return text; +} + /*! Function used to write the class generated binding code on the buffer \param s the output buffer @@ -556,6 +584,22 @@ void CppGenerator::generateClass(QTextStream &s, const GeneratorContext &classCo // Write single method definitions s << singleMethodDefinitions; + if (usePySideExtensions()) { + // PYSIDE-1019: Write a compressed list of all properties `name:getter[:setter]`. + // Default values are suppressed. + QStringList sorter; + for (const auto spec : metaClass->propertySpecs()) + sorter.append(buildPropertyString(spec)); + sorter.sort(); + + s << '\n'; + s << "static const char *" << className << "_properties[] = {\n"; + for (const auto &entry : qAsConst(sorter)) + s << INDENT << entry << ",\n"; + s << INDENT << NULL_PTR << " // Sentinel\n"; + s << "};\n\n"; + } + // Write methods definition s << "static PyMethodDef " << className << "_methods[] = {\n"; s << methodsDefinitions << Qt::endl; @@ -947,8 +991,22 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, s << INDENT << returnStatement << '\n'; } + //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) + QLatin1Char(':'); + s << INDENT << "static PyObject *nameCache[2] = {};\n"; - s << INDENT << "static const char *funcName = \"" << funcName << "\";\n"; + if (propFlag) + s << INDENT << "// This method belongs to a property.\n"; + s << INDENT << "static const char *funcName = \"" << propStr << funcName << "\";\n"; s << INDENT << "Shiboken::AutoDecRef " << PYTHON_OVERRIDE_VAR << "(Shiboken::BindingManager::instance().getOverride(this, nameCache, funcName));\n"; s << INDENT << "if (" << PYTHON_OVERRIDE_VAR << ".isNull()) {\n"; @@ -5127,6 +5185,12 @@ void CppGenerator::writeClassRegister(QTextStream &s, s << INDENT << ");\n"; s << INDENT << Qt::endl; + if (usePySideExtensions()) { + QString className = metaClass->qualifiedCppName(); + s << INDENT << "SbkObjectType_SetPropertyStrings(reinterpret_cast<PyTypeObject *>(" << typePtr << "), " + << chopType(pyTypeName) << "_properties);\n"; + } + if (!classContext.forSmartPointer()) s << INDENT << cpythonTypeNameExt(classTypeEntry) << Qt::endl; else diff --git a/sources/shiboken2/generator/shiboken2/headergenerator.cpp b/sources/shiboken2/generator/shiboken2/headergenerator.cpp index a06efc882..7826e350a 100644 --- a/sources/shiboken2/generator/shiboken2/headergenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/headergenerator.cpp @@ -391,6 +391,12 @@ bool HeaderGenerator::finishGeneration() QString protectedEnumSurrogates; QTextStream protEnumsSurrogates(&protectedEnumSurrogates); + const auto snips = TypeDatabase::instance()->defaultTypeSystemType()->codeSnips(); + if (!snips.isEmpty()) { + writeCodeSnips(macrosStream, snips, TypeSystem::CodeSnipPositionDeclaration, + TypeSystem::TargetLangCode); + } + Indentation indent(INDENT); macrosStream << "// Type indices\nenum : int {\n"; |