diff options
3 files changed, 37 insertions, 22 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 9eb1f2c13..0bae97122 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -717,6 +717,14 @@ void CppGenerator::generateClass(TextStream &s, s << openTargetExternC; + const auto &constructors = getConstructors(metaClass); + if (!constructors.isEmpty()) { + OverloadData overloadData(constructors, api()); + writeConstructorWrapper(s, overloadData, classContext); + // On constructors, we also generate the property initializers. + writeSignatureInfo(signatureStream, overloadData, true); + } + const auto &functionGroups = getFunctionGroups(metaClass); for (auto it = functionGroups.cbegin(), end = functionGroups.cend(); it != end; ++it) { if (contains(sequenceProtocols(), it.key()) || contains(mappingProtocols(), it.key())) @@ -728,13 +736,8 @@ void CppGenerator::generateClass(TextStream &s, const auto &rfunc = overloads.constFirst(); OverloadData overloadData(overloads, api()); - if (rfunc->isConstructor()) { - writeConstructorWrapper(s, overloadData, classContext); - // On constructors, we also generate the property initializers. - writeSignatureInfo(signatureStream, overloadData, true); - } // call operators - else if (rfunc->name() == u"operator()") { + if (rfunc->name() == u"operator()") { writeMethodWrapper(s, overloadData, classContext); writeSignatureInfo(signatureStream, overloadData); } @@ -3696,9 +3699,11 @@ QString CppGenerator::argumentNameFromIndex(const ApiExtractorResult &api, case 0: return PYTHON_RETURN_VAR; case 1: { // Single argument? - OverloadData data(getFunctionGroups(func->implementingClass()).value(func->name()), api); - if (!data.pythonFunctionWrapperUsesListOfArguments()) - return PYTHON_ARG; + if (!func->isConstructor()) { + OverloadData data(getFunctionGroups(func->implementingClass()).value(func->name()), api); + if (!data.pythonFunctionWrapperUsesListOfArguments()) + return PYTHON_ARG; + } break; } } @@ -4493,14 +4498,6 @@ void CppGenerator::writeClassDefinition(TextStream &s, QString tp_hash; QString tp_call; const QString className = chopType(cpythonTypeName(metaClass)); - AbstractMetaFunctionCList ctors; - const auto &allCtors = metaClass->queryFunctions(FunctionQueryOption::AnyConstructor); - for (const auto &f : allCtors) { - if (!f->isPrivate() && !f->isModifiedRemoved() - && f->functionType() != AbstractMetaFunction::MoveConstructorFunction) { - ctors.append(f); - } - } bool onlyPrivCtor = !metaClass->hasNonPrivateConstructor(); @@ -4518,7 +4515,7 @@ void CppGenerator::writeClassDefinition(TextStream &s, } else { tp_dealloc = isQApp ? u"&SbkDeallocQAppWrapper"_s : u"&SbkDeallocWrapper"_s; - if (!onlyPrivCtor && !ctors.isEmpty()) + if (!onlyPrivCtor && !getConstructors(metaClass).isEmpty()) tp_init = cpythonConstructorName(metaClass); } diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index 68ee81800..3c80752b2 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -105,6 +105,7 @@ struct ShibokenGeneratorOptions struct GeneratorClassInfoCacheEntry { ShibokenGenerator::FunctionGroups functionGroups; + AbstractMetaFunctionCList constructors; QList<AbstractMetaFunctionCList> numberProtocolOperators; BoolCastFunctionOptional boolCastFunctionO; ShibokenGenerator::AttroCheck attroCheck; @@ -1937,7 +1938,7 @@ AbstractMetaFunctionCList if (func->isAssignmentOperator() || func->isConversionOperator() || func->isModifiedRemoved() || func->isPrivate() || func->ownerClass() != func->implementingClass() - || func->isConstructor() || func->isOperatorOverload()) + || func->isOperatorOverload()) continue; overloads.append(func); } @@ -2060,7 +2061,7 @@ const GeneratorClassInfoCacheEntry & if (it == cache->end()) { it = cache->insert(scope, {}); auto &entry = it.value(); - entry.functionGroups = getFunctionGroupsImpl(scope); + entry.functionGroups = getFunctionGroupsImpl(scope, &entry.constructors); entry.attroCheck = checkAttroFunctionNeedsImpl(scope, entry.functionGroups); entry.numberProtocolOperators = getNumberProtocolOperators(scope); entry.boolCastFunctionO = getBoolCast(scope); @@ -2075,6 +2076,13 @@ ShibokenGenerator::FunctionGroups return getGeneratorClassInfo(scope).functionGroups; } +AbstractMetaFunctionCList + ShibokenGenerator::getConstructors(const AbstractMetaClassCPtr &scope) +{ + Q_ASSERT(scope); + return getGeneratorClassInfo(scope).constructors; +} + QList<AbstractMetaFunctionCList> ShibokenGenerator::numberProtocolOperators(const AbstractMetaClassCPtr &scope) { @@ -2113,7 +2121,8 @@ static void removeConstOverloads(AbstractMetaFunctionCList *overloads) } ShibokenGenerator::FunctionGroups - ShibokenGenerator::getFunctionGroupsImpl(const AbstractMetaClassCPtr &scope) + ShibokenGenerator::getFunctionGroupsImpl(const AbstractMetaClassCPtr &scope, + AbstractMetaFunctionCList *constructors) { AbstractMetaFunctionCList lst = scope->functions(); scope->getFunctionsFromInvisibleNamespacesToBeGenerated(&lst); @@ -2123,6 +2132,11 @@ ShibokenGenerator::FunctionGroups if (isGroupable(func) && func->ownerClass() == func->implementingClass() && func->generateBinding()) { + if (func->isConstructor()) { + if (func->functionType() != AbstractMetaFunction::MoveConstructorFunction) + constructors->append(func); + continue; + } auto it = results.find(func->name()); if (it == results.end()) { it = results.insert(func->name(), AbstractMetaFunctionCList(1, func)); diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h index bd6f7afa9..e3782e0be 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.h +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h @@ -109,6 +109,9 @@ protected: */ FunctionGroups getGlobalFunctionGroups() const; static FunctionGroups getFunctionGroups(const AbstractMetaClassCPtr &scope); + /// Returns the constructors for which bindings should be generated. + /// \param scope Where to search for functions + static AbstractMetaFunctionCList getConstructors(const AbstractMetaClassCPtr &scope); static QList<AbstractMetaFunctionCList> numberProtocolOperators(const AbstractMetaClassCPtr &scope); @@ -370,7 +373,8 @@ private: static const GeneratorClassInfoCacheEntry & getGeneratorClassInfo(const AbstractMetaClassCPtr &scope); - static FunctionGroups getFunctionGroupsImpl(const AbstractMetaClassCPtr &scope); + static FunctionGroups getFunctionGroupsImpl(const AbstractMetaClassCPtr &scope, + AbstractMetaFunctionCList *constructors); static QList<AbstractMetaFunctionCList> getNumberProtocolOperators(const AbstractMetaClassCPtr &metaClass); static BoolCastFunctionOptional getBoolCast(const AbstractMetaClassCPtr &metaClass); |
