diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-09-29 10:07:04 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-09-30 13:09:36 +0200 |
commit | 1e4c98eb237d174ed01ed3df2a0f2467fb5f09e0 (patch) | |
tree | aa64939c418e1cfc5c9d626838cd445eea104d23 | |
parent | faf7c506a451cef1dc8229a3779eb62796c93e5f (diff) |
shiboken2: Generate functions from invisible top level namespaces as global functions
Mainly add those in ShibokenGenerator::getGlobalFunctionGroups() with
some adaptions.
Task-number: PYSIDE-1075
Change-Id: I6dabac72c204904e76162542b5aa3ea1ac3b56ec
Reviewed-by: Christian Tismer <tismer@stackless.com>
7 files changed, 34 insertions, 6 deletions
diff --git a/sources/shiboken2/ApiExtractor/abstractmetalang.cpp b/sources/shiboken2/ApiExtractor/abstractmetalang.cpp index 29c2f153d..38fb5f152 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetalang.cpp +++ b/sources/shiboken2/ApiExtractor/abstractmetalang.cpp @@ -803,6 +803,12 @@ bool AbstractMetaFunction::argumentRemoved(int key) const return false; } +const AbstractMetaClass *AbstractMetaFunction::targetLangOwner() const +{ + return m_class && m_class->isInvisibleNamespace() + ? m_class->targetLangEnclosingClass() : m_class; +} + bool AbstractMetaFunction::isDeprecated() const { const FunctionModificationList &modifications = this->modifications(declaringClass()); diff --git a/sources/shiboken2/ApiExtractor/abstractmetalang.h b/sources/shiboken2/ApiExtractor/abstractmetalang.h index 693ecc4d4..91a6138d1 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetalang.h +++ b/sources/shiboken2/ApiExtractor/abstractmetalang.h @@ -907,6 +907,9 @@ public: m_class = cls; } + // Owner excluding invisible namespaces + const AbstractMetaClass *targetLangOwner() const; + // The first class in a hierarchy that declares the function const AbstractMetaClass *declaringClass() const { diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index a366dfa1c..8de293654 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -1739,7 +1739,7 @@ void CppGenerator::writeMethodWrapperPreamble(QTextStream &s, OverloadData &over const GeneratorContext &context) { const AbstractMetaFunction *rfunc = overloadData.referenceFunction(); - const AbstractMetaClass *ownerClass = rfunc->ownerClass(); + const AbstractMetaClass *ownerClass = rfunc->targetLangOwner(); Q_ASSERT(ownerClass == context.metaClass()); int minArgs = overloadData.minArgs(); int maxArgs = overloadData.maxArgs(); @@ -4779,8 +4779,14 @@ void CppGenerator::writeMethodDefinitionEntry(QTextStream &s, const AbstractMeta if (overloadData.hasArgumentWithDefaultValue()) s << "|METH_KEYWORDS"; } - if (func->ownerClass() && overloadData.hasStaticFunction()) + // METH_STATIC causes a crash when used for global functions (also from + // invisible namespaces). + auto ownerClass = func->ownerClass(); + if (ownerClass + && !invisibleTopNamespaces().contains(const_cast<AbstractMetaClass *>(ownerClass)) + && overloadData.hasStaticFunction()) { s << "|METH_STATIC"; + } } void CppGenerator::writeMethodDefinition(QTextStream &s, const AbstractMetaFunctionList &overloads) diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp index 3606f0af0..3fa1eb646 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp @@ -2457,14 +2457,21 @@ static bool isGroupable(const AbstractMetaFunction *func) return true; } -ShibokenGenerator::FunctionGroups ShibokenGenerator::getGlobalFunctionGroups() const +static void insertIntoFunctionGroups(const AbstractMetaFunctionList &lst, + ShibokenGenerator::FunctionGroups *results) { - const AbstractMetaFunctionList &lst = globalFunctions(); - FunctionGroups results; for (AbstractMetaFunction *func : lst) { if (isGroupable(func)) - results[func->name()].append(func); + (*results)[func->name()].append(func); } +} + +ShibokenGenerator::FunctionGroups ShibokenGenerator::getGlobalFunctionGroups() const +{ + FunctionGroups results; + insertIntoFunctionGroups(globalFunctions(), &results); + for (auto nsp : invisibleTopNamespaces()) + insertIntoFunctionGroups(nsp->functions(), &results); return results; } diff --git a/sources/shiboken2/tests/libsample/removednamespaces.h b/sources/shiboken2/tests/libsample/removednamespaces.h index 9ad798bf7..e46bd2c15 100644 --- a/sources/shiboken2/tests/libsample/removednamespaces.h +++ b/sources/shiboken2/tests/libsample/removednamespaces.h @@ -39,6 +39,8 @@ enum RemovedNamespace1_Enum { RemovedNamespace1_Enum_Value0 = 0, enum { RemovedNamespace1_AnonymousEnum_Value0 }; +inline int mathSum(int x, int y) { return x + y; } + struct ObjectOnInvisibleNamespace { bool exists() const { return true; } diff --git a/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py b/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py index 168a609dc..45d4156c4 100644 --- a/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py +++ b/sources/shiboken2/tests/samplebinding/enumfromremovednamespace_test.py @@ -65,6 +65,9 @@ class TestEnumFromRemovedNamespace(unittest.TestCase): self.assertEqual(objectFullname(signature.parameters['other'].annotation), "sample.ObjectOnInvisibleNamespace") + def testGlobalFunctionFromRemovedNamespace(self): + self.assertEqual(sample.mathSum(1, 2), 3) + def testEnumPromotedToUpperNamespace(self): sample.UnremovedNamespace sample.UnremovedNamespace.RemovedNamespace3_Enum diff --git a/sources/shiboken2/tests/samplebinding/typesystem_sample.xml b/sources/shiboken2/tests/samplebinding/typesystem_sample.xml index 3b74cb13b..7931c5a8d 100644 --- a/sources/shiboken2/tests/samplebinding/typesystem_sample.xml +++ b/sources/shiboken2/tests/samplebinding/typesystem_sample.xml @@ -620,6 +620,7 @@ <namespace-type name="RemovedNamespace1" visible='false'> <enum-type name="RemovedNamespace1_Enum" /> + <function signature="mathSum(int,int)"/> <value-type name="ObjectOnInvisibleNamespace" /> <namespace-type name="RemovedNamespace2" visible='false'> <enum-type name="RemovedNamespace2_Enum" /> |