diff options
Diffstat (limited to 'sources')
9 files changed, 96 insertions, 55 deletions
diff --git a/sources/shiboken6/ApiExtractor/pymethoddefentry.cpp b/sources/shiboken6/ApiExtractor/pymethoddefentry.cpp index 75ac227b8..64d44378b 100644 --- a/sources/shiboken6/ApiExtractor/pymethoddefentry.cpp +++ b/sources/shiboken6/ApiExtractor/pymethoddefentry.cpp @@ -6,10 +6,15 @@ #include <QtCore/QDebug> +TextStream &operator<<(TextStream &str, const castToPyCFunction &c) +{ + str << "reinterpret_cast<PyCFunction>(" << c.m_function << ')'; + return str; +} + TextStream &operator<<(TextStream &s, const PyMethodDefEntry &e) { - s << "{\"" << e.name << "\", reinterpret_cast<PyCFunction>(" - << e.function << "), "; + s << "{\"" << e.name << "\", " << castToPyCFunction(e.function) <<", "; if (e.methFlags.isEmpty()) { s << '0'; } else { diff --git a/sources/shiboken6/ApiExtractor/pymethoddefentry.h b/sources/shiboken6/ApiExtractor/pymethoddefentry.h index 54eaa4ce1..4a716ba59 100644 --- a/sources/shiboken6/ApiExtractor/pymethoddefentry.h +++ b/sources/shiboken6/ApiExtractor/pymethoddefentry.h @@ -11,6 +11,14 @@ QT_FORWARD_DECLARE_CLASS(QDebug) class TextStream; +struct castToPyCFunction +{ + explicit castToPyCFunction(QStringView function) noexcept : + m_function(function) {} + + QStringView m_function; +}; + struct PyMethodDefEntry { QString name; @@ -21,6 +29,7 @@ struct PyMethodDefEntry using PyMethodDefEntries = QList<PyMethodDefEntry>; +TextStream &operator<<(TextStream &str, const castToPyCFunction &e); TextStream &operator<<(TextStream &s, const PyMethodDefEntry &e); TextStream &operator<<(TextStream &s, const PyMethodDefEntries &e); diff --git a/sources/shiboken6/generator/CMakeLists.txt b/sources/shiboken6/generator/CMakeLists.txt index b84754ba7..25ab0eaad 100644 --- a/sources/shiboken6/generator/CMakeLists.txt +++ b/sources/shiboken6/generator/CMakeLists.txt @@ -20,7 +20,7 @@ shiboken/configurablescope.h shiboken/cppgenerator.cpp shiboken/cppgenerator.h shiboken/cppgenerator_container.cpp shiboken/ctypenames.h -shiboken/generatorargument.cpp shiboken/generatorargument.h +shiboken/generatorargument.cpp shiboken/generatorargument.h shiboken/generatorstrings.h shiboken/headergenerator.cpp shiboken/headergenerator.h shiboken/overloaddata.cpp shiboken/overloaddata.h shiboken/pytypenames.h diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 567cb6f1d..10c12f52a 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -4,6 +4,7 @@ #include "cppgenerator.h" #include "configurablescope.h" #include "generatorargument.h" +#include "generatorstrings.h" #include "defaultvalue.h" #include "generatorcontext.h" #include "codesnip.h" @@ -75,12 +76,6 @@ TextStream &operator<<(TextStream &str, const sbkUnusedVariableCast &c) return str; } -static const QString CPP_ARG0 = u"cppArg0"_s; -static const char methodDefSentinel[] = "{nullptr, nullptr, 0, nullptr} // Sentinel\n"; -const char *CppGenerator::PYTHON_TO_CPPCONVERSION_STRUCT = "Shiboken::Conversions::PythonToCppConversion"; - -static inline QString reprFunction() { return QStringLiteral("__repr__"); } - TextStream &operator<<(TextStream &s, CppGenerator::ErrorReturn r) { s << "return"; @@ -215,7 +210,7 @@ void CppGenerator::clearTpFuncs() // 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, {} }}; @@ -462,14 +457,6 @@ void CppGenerator::generateIncludes(TextStream &s, const GeneratorContext &class 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) @@ -480,7 +467,7 @@ static void writePyMethodDefs(TextStream &s, const QString &className, s << "{\"__copy__\", reinterpret_cast<PyCFunction>(" << className << "___copy__)" << ", METH_NOARGS, nullptr},\n"; } - s << methodDefSentinel << outdent + s << METHOD_DEF_SENTINEL << outdent << "};\n\n"; } @@ -3233,8 +3220,7 @@ void CppGenerator::writeSingleFunctionCall(TextStream &s, 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" << sbkUnusedVariableCast(cppArgRemoved); @@ -3256,7 +3242,7 @@ void CppGenerator::writeSingleFunctionCall(TextStream &s, continue; auto argType = getArgumentType(func, argIdx); int argPos = argIdx - removedArgs; - QString argName = CPP_ARG + QString::number(argPos); + QString argName = CPP_ARG(argPos); QString pyArgName = usePyArgs ? pythonArgsAt(argPos) : PYTHON_ARG; indirections[argIdx] = writeArgumentConversion(s, argType, argName, pyArgName, errorReturn, @@ -3823,7 +3809,7 @@ 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); @@ -3831,7 +3817,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr const int idx = arg.argumentIndex() - removedArgs; const auto deRef = argumentIndirections.at(i); QString argName = AbstractMetaType::dereferencePrefix(deRef) - + CPP_ARG + QString::number(idx); + + CPP_ARG(idx); userArgs.append(argName); } } @@ -3862,7 +3848,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; @@ -4569,9 +4555,9 @@ void CppGenerator::writeClassDefinition(TextStream &s, else if ( it = m_nbFuncs.find(func->name()); it != m_nbFuncs.end() ) it.value() = cpythonFunctionName(func); } - if (m_tpFuncs.value(reprFunction()).isEmpty() + if (m_tpFuncs.value(REPR_FUNCTION).isEmpty() && metaClass->hasToStringCapability()) { - m_tpFuncs[reprFunction()] = writeReprFunction(s, + m_tpFuncs[REPR_FUNCTION] = writeReprFunction(s, classContext, metaClass->toStringCapabilityIndirections()); } @@ -4603,7 +4589,7 @@ void CppGenerator::writeClassDefinition(TextStream &s, << "}\n\nstatic PyType_Slot " << className << "_slots[] = {\n" << indent << "{Py_tp_base, nullptr}, // inserted by introduceWrapperType\n" << pyTypeSlotEntry(u"Py_tp_dealloc", tp_dealloc) - << pyTypeSlotEntry(u"Py_tp_repr", m_tpFuncs.value(reprFunction())) + << pyTypeSlotEntry(u"Py_tp_repr", m_tpFuncs.value(REPR_FUNCTION)) << pyTypeSlotEntry(u"Py_tp_hash", tp_hash) << pyTypeSlotEntry(u"Py_tp_call", tp_call) << pyTypeSlotEntry(u"Py_tp_str", m_tpFuncs.value(u"__str__"_s)) @@ -5064,9 +5050,6 @@ void CppGenerator::writeRichCompareFunctionHeader(TextStream &s, << 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 { @@ -6451,7 +6434,7 @@ bool CppGenerator::finishGeneration() << s_globalFunctionImpl.toString() << '\n' << "static PyMethodDef " << moduleName() << "_methods[] = {\n" << indent << s_globalFunctionDef.toString() - << methodDefSentinel << outdent << "};\n\n" + << METHOD_DEF_SENTINEL << outdent << "};\n\n" << "// Classes initialization functions " << "------------------------------------------------------------\n" << s_classInitDecl.toString() << '\n'; @@ -6941,7 +6924,7 @@ QString CppGenerator::writeReprFunction(TextStream &s, uint indirections) { const auto metaClass = context.metaClass(); - QString funcName = cpythonBaseName(metaClass) + reprFunction(); + QString funcName = cpythonBaseName(metaClass) + REPR_FUNCTION; s << "extern \"C\"\n{\n" << "static PyObject *" << funcName << "(PyObject *self)\n{\n" << indent; writeCppSelfDefinition(s, context); diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index 110326f5c..563bc2b7a 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -517,8 +517,6 @@ private: QHash<QString, QString> m_tpFuncs; QHash<QString, QString> m_nbFuncs; - - static const char *PYTHON_TO_CPPCONVERSION_STRUCT; }; Q_DECLARE_OPERATORS_FOR_FLAGS(CppGenerator::CppSelfDefinitionFlags) diff --git a/sources/shiboken6/generator/shiboken/cppgenerator_container.cpp b/sources/shiboken6/generator/shiboken/cppgenerator_container.cpp index 9632fd940..7e46cdd02 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator_container.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator_container.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "cppgenerator.h" +#include "generatorstrings.h" #include <abstractmetalang.h> #include "apiextractorresult.h" #include "ctypenames.h" diff --git a/sources/shiboken6/generator/shiboken/generatorstrings.h b/sources/shiboken6/generator/shiboken/generatorstrings.h new file mode 100644 index 000000000..6751d5216 --- /dev/null +++ b/sources/shiboken6/generator/shiboken/generatorstrings.h @@ -0,0 +1,34 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#ifndef GENERATORSTRINGS_H +#define GENERATORSTRINGS_H + +#include <QtCore/QString> + +QString CPP_ARG(int i); +QString CPP_ARG_REMOVED(int i); + +extern const QString CPP_RETURN_VAR; +extern const QString CPP_SELF_VAR; +extern const QString NULL_PTR; +extern const QString PYTHON_ARG; +extern const QString PYTHON_ARGS; +extern const QString PYTHON_OVERRIDE_VAR; +extern const QString PYTHON_RETURN_VAR; +extern const QString PYTHON_TO_CPP_VAR; + +extern const QString CONV_RULE_OUT_VAR_SUFFIX; +extern const QString BEGIN_ALLOW_THREADS; +extern const QString END_ALLOW_THREADS; + +extern const QString REPR_FUNCTION; + +extern const QString CPP_ARG0; +extern const char *const METHOD_DEF_SENTINEL; +extern const char *const PYTHON_TO_CPPCONVERSION_STRUCT; +extern const char *const openTargetExternC; +extern const char *const closeExternC; +extern const char *const richCompareComment; + +#endif // GENERATORSTRINGS_H diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index 052ac4a61..c556b0961 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "shibokengenerator.h" +#include "generatorstrings.h" #include "generatorargument.h" #include "defaultvalue.h" #include "generatorcontext.h" @@ -62,8 +63,18 @@ static const char WRAPPER_DIAGNOSTICS[] = "wrapper-diagnostics"; static const char NO_IMPLICIT_CONVERSIONS[] = "no-implicit-conversions"; static const char LEAN_HEADERS[] = "lean-headers"; -const QString CPP_ARG = u"cppArg"_s; -const QString CPP_ARG_REMOVED = u"removed_cppArg"_s; +QString CPP_ARG(int i) +{ + return u"cppArg"_s + QString::number(i); +} + +static const QString CPP_ARG_REMOVED_PREFIX = u"removed_cppArg"_s; + +QString CPP_ARG_REMOVED(int i) +{ + return CPP_ARG_REMOVED_PREFIX + QString::number(i); +} + const QString CPP_RETURN_VAR = u"cppResult"_s; const QString CPP_SELF_VAR = u"cppSelf"_s; const QString NULL_PTR = u"nullptr"_s; @@ -78,6 +89,21 @@ const QString BEGIN_ALLOW_THREADS = u"PyThreadState *_save = PyEval_SaveThread(); // Py_BEGIN_ALLOW_THREADS"_s; const QString END_ALLOW_THREADS = u"PyEval_RestoreThread(_save); // Py_END_ALLOW_THREADS"_s; +const QString REPR_FUNCTION = u"__repr__"_s; + +const QString CPP_ARG0 = u"cppArg0"_s; +const char *const METHOD_DEF_SENTINEL = "{nullptr, nullptr, 0, nullptr} // Sentinel\n"; +const char *const PYTHON_TO_CPPCONVERSION_STRUCT = "Shiboken::Conversions::PythonToCppConversion"; + +const char *const openTargetExternC = R"( +// Target --------------------------------------------------------- + +extern "C" { +)"; +const char *const closeExternC = "} // extern \"C\"\n\n"; +const char *const richCompareComment = + "// PYSIDE-74: By default, we redirect to object's tp_richcompare (which is `==`, `!=`).\n"; + struct ShibokenGeneratorOptions { bool useCtorHeuristic = false; @@ -1337,7 +1363,7 @@ ShibokenGenerator::ArgumentVarReplacementList if (argRemoved && hasConversionRule) argValue = arg.name() + CONV_RULE_OUT_VAR_SUFFIX; else if (argRemoved || (lastArg && arg.argumentIndex() > lastArg->argumentIndex())) - argValue = CPP_ARG_REMOVED + QString::number(i); + argValue = CPP_ARG_REMOVED(i); if (!argRemoved && argValue.isEmpty()) { int argPos = i - removed; AbstractMetaType type = arg.modifiedType(); @@ -1347,7 +1373,7 @@ ShibokenGenerator::ArgumentVarReplacementList } else { argValue = hasConversionRule ? arg.name() + CONV_RULE_OUT_VAR_SUFFIX - : CPP_ARG + QString::number(argPos); + : CPP_ARG(argPos); const auto generatorArg = GeneratorArgument::fromMetaType(type); AbstractMetaType::applyDereference(&argValue, generatorArg.indirections); } @@ -1556,7 +1582,7 @@ void ShibokenGenerator::writeCodeSnips(TextStream &s, QStringList args; for (const ArgumentVarReplacementPair &pair : argReplacements) { - if (pair.second.startsWith(CPP_ARG_REMOVED)) + if (pair.second.startsWith(CPP_ARG_REMOVED_PREFIX)) continue; args << pair.second; } diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h index dc3d9426f..82b1f42a4 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.h +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h @@ -477,19 +477,4 @@ private: Q_DECLARE_OPERATORS_FOR_FLAGS(ShibokenGenerator::FunctionGeneration); Q_DECLARE_OPERATORS_FOR_FLAGS(ShibokenGenerator::AttroCheck); -extern const QString CPP_ARG; -extern const QString CPP_ARG_REMOVED; -extern const QString CPP_RETURN_VAR; -extern const QString CPP_SELF_VAR; -extern const QString NULL_PTR; -extern const QString PYTHON_ARG; -extern const QString PYTHON_ARGS; -extern const QString PYTHON_OVERRIDE_VAR; -extern const QString PYTHON_RETURN_VAR; -extern const QString PYTHON_TO_CPP_VAR; - -extern const QString CONV_RULE_OUT_VAR_SUFFIX; -extern const QString BEGIN_ALLOW_THREADS; -extern const QString END_ALLOW_THREADS; - #endif // SHIBOKENGENERATOR_H |
