diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-05-20 09:32:50 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-05-20 15:12:14 +0000 |
commit | 147d5a7e06c5827e5ccedf03b55d9e2edb7d3e67 (patch) | |
tree | 02dde60a4ab754daf71d61459382c83bcb5b3997 | |
parent | 141592626aa52b149205ef1efc803b699de8f9ec (diff) |
shiboken6: Refactor argument ownership
In ArgumentModification, replace the
QHash<TypeSystem::Language, TypeSystem::Ownership>
by 2 getters and setters for native and target language.
Rename InvalidOwnership to UnspecifiedOwnership.
Change-Id: I4f1a6bd67f50a7c2064b7543df197f580ffb73ca
Reviewed-by: Christian Tismer <tismer@stackless.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit 5b9b83f58bc990136697b8255a4f9230653b8d74)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
8 files changed, 55 insertions, 27 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp b/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp index 2232c811e..1b21b94b8 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp @@ -701,16 +701,16 @@ bool AbstractMetaFunction::allowThread() const return result; } -TypeSystem::Ownership AbstractMetaFunction::ownership(const AbstractMetaClass *cls, TypeSystem::Language language, int key) const +TypeSystem::Ownership AbstractMetaFunction::argumentTargetOwnership(const AbstractMetaClass *cls, int idx) const { for (const auto &modification : modifications(cls)) { for (const ArgumentModification &argumentModification : modification.argument_mods()) { - if (argumentModification.index() == key) - return argumentModification.ownerships().value(language, TypeSystem::InvalidOwnership); + if (argumentModification.index() == idx) + return argumentModification.targetOwnerShip(); } } - return TypeSystem::InvalidOwnership; + return TypeSystem::UnspecifiedOwnership; } QString AbstractMetaFunction::typeReplaced(int key) const diff --git a/sources/shiboken6/ApiExtractor/abstractmetafunction.h b/sources/shiboken6/ApiExtractor/abstractmetafunction.h index 9ea22503e..15192e6de 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetafunction.h +++ b/sources/shiboken6/ApiExtractor/abstractmetafunction.h @@ -289,8 +289,8 @@ public: QList<ReferenceCount> referenceCounts(const AbstractMetaClass *cls, int idx = -2) const; ArgumentOwner argumentOwner(const AbstractMetaClass *cls, int idx) const; - // Returns the ownership rules for the given argument in the given context - TypeSystem::Ownership ownership(const AbstractMetaClass *cls, TypeSystem::Language language, int idx) const; + // Returns the ownership rules for the given argument (target lang). + TypeSystem::Ownership argumentTargetOwnership(const AbstractMetaClass *cls, int idx) const; QString typeReplaced(int argument_index) const; bool isModifiedToArray(int argumentIndex) const; diff --git a/sources/shiboken6/ApiExtractor/modifications.cpp b/sources/shiboken6/ApiExtractor/modifications.cpp index e7d0aa8a1..645934e3b 100644 --- a/sources/shiboken6/ApiExtractor/modifications.cpp +++ b/sources/shiboken6/ApiExtractor/modifications.cpp @@ -507,7 +507,8 @@ public: QList<ReferenceCount> referenceCounts; QString modified_type; QString replacedDefaultExpression; - QHash<TypeSystem::Language, TypeSystem::Ownership> ownerships; + TypeSystem::Ownership m_targetOwnerShip = TypeSystem::UnspecifiedOwnership; + TypeSystem::Ownership m_nativeOwnerShip = TypeSystem::UnspecifiedOwnership; CodeSnipList conversion_rules; ArgumentOwner owner; QString renamed_to; @@ -555,14 +556,26 @@ void ArgumentModification::setReplacedDefaultExpression(const QString &value) d->replacedDefaultExpression = value; } -const QHash<TypeSystem::Language, TypeSystem::Ownership> &ArgumentModification::ownerships() const +TypeSystem::Ownership ArgumentModification::targetOwnerShip() const { - return d->ownerships; + return d->m_targetOwnerShip; } -void ArgumentModification::insertOwnership(TypeSystem::Language l, TypeSystem::Ownership o) +void ArgumentModification::setTargetOwnerShip(TypeSystem::Ownership o) { - d->ownerships.insert(l, o); + if (o != d->m_targetOwnerShip) + d->m_targetOwnerShip = o; +} + +TypeSystem::Ownership ArgumentModification::nativeOwnership() const +{ + return d->m_nativeOwnerShip; +} + +void ArgumentModification::setNativeOwnership(TypeSystem::Ownership o) +{ + if (o != d->m_nativeOwnerShip) + d->m_nativeOwnerShip = o; } const CodeSnipList &ArgumentModification::conversionRules() const @@ -929,8 +942,10 @@ QDebug operator<<(QDebug d, const ArgumentModification &a) d << ", modified_type=\"" << a.modifiedType() << '"'; if (!a.replacedDefaultExpression().isEmpty()) d << ", replacedDefaultExpression=\"" << a.replacedDefaultExpression() << '"'; - if (!a.ownerships().isEmpty()) - d << ", ownerships=" << a.ownerships(); + if (a.targetOwnerShip() != TypeSystem::UnspecifiedOwnership) + d << ", target ownership=" << a.targetOwnerShip(); + if (a.nativeOwnership() != TypeSystem::UnspecifiedOwnership) + d << ", native ownership=" << a.nativeOwnership(); if (!a.renamedToName().isEmpty()) d << ", renamed_to=\"" << a.renamedToName() << '"'; d << ", owner=" << a.owner() << ')'; diff --git a/sources/shiboken6/ApiExtractor/modifications.h b/sources/shiboken6/ApiExtractor/modifications.h index 465f7805f..09527d4ad 100644 --- a/sources/shiboken6/ApiExtractor/modifications.h +++ b/sources/shiboken6/ApiExtractor/modifications.h @@ -198,8 +198,11 @@ public: void setReplacedDefaultExpression(const QString &value); // The new definition of ownership for a specific argument - const QHash<TypeSystem::Language, TypeSystem::Ownership> &ownerships() const; - void insertOwnership(TypeSystem::Language l, TypeSystem::Ownership o); + + TypeSystem::Ownership targetOwnerShip() const; + void setTargetOwnerShip(TypeSystem::Ownership o); + TypeSystem::Ownership nativeOwnership() const; + void setNativeOwnership(TypeSystem::Ownership o); // Different conversion rules const CodeSnipList &conversionRules() const; diff --git a/sources/shiboken6/ApiExtractor/tests/testmodifyfunction.cpp b/sources/shiboken6/ApiExtractor/tests/testmodifyfunction.cpp index 906b8a4b4..0d95d5abb 100644 --- a/sources/shiboken6/ApiExtractor/tests/testmodifyfunction.cpp +++ b/sources/shiboken6/ApiExtractor/tests/testmodifyfunction.cpp @@ -98,7 +98,8 @@ void TestModifyFunction::testOwnershipTransfer() const auto func = classB->findFunction(QLatin1String("method")); QVERIFY(!func.isNull()); - QCOMPARE(func->ownership(func->ownerClass(), TypeSystem::TargetLangCode, 0), TypeSystem::CppOwnership); + QCOMPARE(func->argumentTargetOwnership(func->ownerClass(), 0), + TypeSystem::CppOwnership); } @@ -219,10 +220,12 @@ void TestModifyFunction::testWithApiVersion() AbstractMetaClass* classB = AbstractMetaClass::findClass(classes, QLatin1String("B")); auto func = classB->findFunction(QLatin1String("method")); - QCOMPARE(func->ownership(func->ownerClass(), TypeSystem::TargetLangCode, 0), TypeSystem::CppOwnership); + auto returnOwnership = func->argumentTargetOwnership(func->ownerClass(), 0); + QCOMPARE(returnOwnership, TypeSystem::CppOwnership); func = classB->findFunction(QLatin1String("methodB")); - QVERIFY(func->ownership(func->ownerClass(), TypeSystem::TargetLangCode, 0) != TypeSystem::CppOwnership); + returnOwnership = func->argumentTargetOwnership(func->ownerClass(), 0); + QVERIFY(returnOwnership != TypeSystem::CppOwnership); } // Modifications on class/typesystem level are tested below diff --git a/sources/shiboken6/ApiExtractor/typesystem_enums.h b/sources/shiboken6/ApiExtractor/typesystem_enums.h index 0495a7591..1953fef6a 100644 --- a/sources/shiboken6/ApiExtractor/typesystem_enums.h +++ b/sources/shiboken6/ApiExtractor/typesystem_enums.h @@ -50,7 +50,7 @@ enum class AllowThread { }; enum Ownership { - InvalidOwnership, + UnspecifiedOwnership, DefaultOwnership, TargetLangOwnership, CppOwnership diff --git a/sources/shiboken6/ApiExtractor/typesystemparser.cpp b/sources/shiboken6/ApiExtractor/typesystemparser.cpp index f962258df..64bb7a8a4 100644 --- a/sources/shiboken6/ApiExtractor/typesystemparser.cpp +++ b/sources/shiboken6/ApiExtractor/typesystemparser.cpp @@ -2112,7 +2112,16 @@ bool TypeSystemParser::parseDefineOwnership(const QXmlStreamReader &, return false; } auto &lastArgMod = m_contextStack.top()->functionMods.last().argument_mods().last(); - lastArgMod.insertOwnership(lang, ownershipOpt.value()); + switch (lang) { + case TypeSystem::TargetLangCode: + lastArgMod.setTargetOwnerShip(ownershipOpt.value()); + break; + case TypeSystem::NativeCode: + lastArgMod.setNativeOwnership(ownershipOpt.value()); + break; + default: + break; + } return true; } diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index a5d38b87b..7989b008d 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -1102,7 +1102,7 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s, << " = PyTuple_GET_ITEM(" << PYTHON_ARGS << ", " << index - 1 << ")->ob_refcnt == 1;\n"; } else if (index == 0 && - argMod.ownerships().value(TypeSystem::TargetLangCode) == TypeSystem::CppOwnership) { + argMod.targetOwnerShip() == TypeSystem::CppOwnership) { invalidateReturn = true; } } @@ -1197,9 +1197,8 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s, for (const FunctionModification &funcMod : functionModifications) { for (const ArgumentModification &argMod : funcMod.argument_mods()) { - if (argMod.ownerships().contains(TypeSystem::NativeCode) - && argMod.index() == 0 - && argMod.ownerships().value(TypeSystem::NativeCode) == TypeSystem::CppOwnership) { + if (argMod.index() == 0 + && argMod.nativeOwnership() == TypeSystem::CppOwnership) { s << "if (Shiboken::Object::checkType(" << PYTHON_RETURN_VAR << "))\n"; Indentation indent(s); s << "Shiboken::Object::releaseOwnership(" << PYTHON_RETURN_VAR << ");\n"; @@ -3724,7 +3723,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr QList<ArgumentModification> refcount_mods; for (const auto &func_mod : func->modifications()) { for (const ArgumentModification &arg_mod : func_mod.argument_mods()) { - if (arg_mod.ownerships().contains(TypeSystem::TargetLangCode)) + if (arg_mod.targetOwnerShip() != TypeSystem::UnspecifiedOwnership) ownership_mods.append(arg_mod); else if (!arg_mod.referenceCounts().isEmpty()) refcount_mods.append(arg_mod); @@ -3760,8 +3759,7 @@ void CppGenerator::writeMethodCall(TextStream &s, const AbstractMetaFunctionCPtr // The default ownership does nothing. This is useful to avoid automatic heuristically // based generation of code defining parenting. - const auto ownership = - arg_mod.ownerships().value(TypeSystem::TargetLangCode, TypeSystem::DefaultOwnership); + const auto ownership = arg_mod.targetOwnerShip(); if (ownership == TypeSystem::DefaultOwnership) continue; |