aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-09-11 14:34:10 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2023-09-20 12:45:03 +0200
commitc04a2492fe54776a58d72f34282a05e5f9e1158d (patch)
treeaeeaf28de7a322d839e276e872207e9c603ab579
parentb550babdf405dc513199713384a010b66126ebd6 (diff)
shiboken6: Move the finding of bool casts into the class cache
Finding the right function for implementing nb_bool is a lengthy function; move it into the static class cache for access by the header and source generators. Change-Id: I1c7a32ffe115f612b84a6091487e51ba5b7f1247 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io> Reviewed-by: Adrian Herrmann <adrian.herrmann@qt.io> (cherry picked from commit f4d3b539e8938225faebdade31009116d737bd8c) Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp42
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.h10
-rw-r--r--sources/shiboken6/generator/shiboken/shibokengenerator.cpp61
-rw-r--r--sources/shiboken6/generator/shiboken/shibokengenerator.h13
4 files changed, 72 insertions, 54 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 71f8576cd..dac4ab994 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -197,48 +197,6 @@ QString CppGenerator::fileNameForContext(const GeneratorContext &context) const
return fileNameForContextHelper(context, u"_wrapper.cpp"_s);
}
-CppGenerator::BoolCastFunctionOptional
- CppGenerator::boolCast(const AbstractMetaClassCPtr &metaClass) const
-{
- const auto te = metaClass->typeEntry();
- if (te->isSmartPointer()) {
- auto ste = std::static_pointer_cast<const SmartPointerTypeEntry>(te);
-
- auto valueCheckMethod = ste->valueCheckMethod();
- if (!valueCheckMethod.isEmpty()) {
- const auto func = metaClass->findFunction(valueCheckMethod);
- if (!func)
- throw Exception(msgMethodNotFound(metaClass, valueCheckMethod));
- return BoolCastFunction{func, false};
- }
-
- auto nullCheckMethod = ste->nullCheckMethod();
- if (!nullCheckMethod.isEmpty()) {
- const auto func = metaClass->findFunction(nullCheckMethod);
- if (!func)
- throw Exception(msgMethodNotFound(metaClass, nullCheckMethod));
- return BoolCastFunction{func, true};
- }
- }
-
- auto mode = te->operatorBoolMode();
- if (useOperatorBoolAsNbNonZero()
- ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
- const auto func = metaClass->findOperatorBool();
- if (func)
- return BoolCastFunction{func, false};
- }
-
- mode = te->isNullMode();
- if (useIsNullAsNbNonZero()
- ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
- const auto func = metaClass->findQtIsNullMethod();
- if (func)
- return BoolCastFunction{func, true};
- }
- return std::nullopt;
-}
-
std::optional<AbstractMetaType>
CppGenerator::findSmartPointerInstantiation(const SmartPointerTypeEntryCPtr &pointer,
const TypeEntryCPtr &pointee) const
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h
index 002a45e5d..7fee3c0fb 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.h
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.h
@@ -48,13 +48,6 @@ protected:
bool finishGeneration() override;
private:
- struct BoolCastFunction
- {
- AbstractMetaFunctionCPtr function;
- bool invert = false; // Function is isNull() (invert result).
- };
- using BoolCastFunctionOptional = std::optional<BoolCastFunction>;
-
void generateSmartPointerClass(TextStream &s, const GeneratorContext &classContext);
void generateIncludes(TextStream &s, const GeneratorContext &classContext,
const IncludeGroupList &includes = {},
@@ -529,8 +522,7 @@ private:
QString writeReprFunction(TextStream &s, const GeneratorContext &context,
uint indirections) const;
- BoolCastFunctionOptional boolCast(const AbstractMetaClassCPtr &metaClass) const;
- bool hasBoolCast(const AbstractMetaClassCPtr &metaClass) const
+ static bool hasBoolCast(const AbstractMetaClassCPtr &metaClass)
{ return boolCast(metaClass).has_value(); }
std::optional<AbstractMetaType>
diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp
index 2e927c73a..47e24e012 100644
--- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp
@@ -33,6 +33,7 @@
#include <namespacetypeentry.h>
#include <primitivetypeentry.h>
#include <pythontypeentry.h>
+#include <smartpointertypeentry.h>
#include <valuetypeentry.h>
#include <iostream>
@@ -92,6 +93,7 @@ struct GeneratorClassInfoCacheEntry
{
ShibokenGenerator::FunctionGroups functionGroups;
QList<AbstractMetaFunctionCList> numberProtocolOperators;
+ BoolCastFunctionOptional boolCastFunctionO;
bool needsGetattroFunction = false;
};
@@ -1993,9 +1995,11 @@ const GeneratorClassInfoCacheEntry &
auto it = cache->find(scope);
if (it == cache->end()) {
it = cache->insert(scope, {});
- it.value().functionGroups = getFunctionGroupsImpl(scope);
- it.value().needsGetattroFunction = classNeedsGetattroFunctionImpl(scope);
- it.value().numberProtocolOperators = getNumberProtocolOperators(scope);
+ auto &entry = it.value();
+ entry.functionGroups = getFunctionGroupsImpl(scope);
+ entry.needsGetattroFunction = classNeedsGetattroFunctionImpl(scope);
+ entry.numberProtocolOperators = getNumberProtocolOperators(scope);
+ entry.boolCastFunctionO = getBoolCast(scope);
}
return it.value();
}
@@ -2014,6 +2018,12 @@ QList<AbstractMetaFunctionCList>
return getGeneratorClassInfo(scope).numberProtocolOperators;
}
+BoolCastFunctionOptional ShibokenGenerator::boolCast(const AbstractMetaClassCPtr &scope)
+{
+ Q_ASSERT(scope);
+ return getGeneratorClassInfo(scope).boolCastFunctionO;
+}
+
// Use non-const overloads only, for example, "foo()" and "foo()const"
// the second is removed.
static void removeConstOverloads(AbstractMetaFunctionCList *overloads)
@@ -2093,6 +2103,51 @@ QList<AbstractMetaFunctionCList>
return result;
}
+BoolCastFunctionOptional
+ShibokenGenerator::getBoolCast(const AbstractMetaClassCPtr &metaClass)
+{
+ if (metaClass->isNamespace())
+ return std::nullopt;
+
+ const auto te = metaClass->typeEntry();
+ if (te->isSmartPointer()) {
+ auto ste = std::static_pointer_cast<const SmartPointerTypeEntry>(te);
+
+ auto valueCheckMethod = ste->valueCheckMethod();
+ if (!valueCheckMethod.isEmpty()) {
+ const auto func = metaClass->findFunction(valueCheckMethod);
+ if (!func)
+ throw Exception(msgMethodNotFound(metaClass, valueCheckMethod));
+ return BoolCastFunction{func, false};
+ }
+
+ auto nullCheckMethod = ste->nullCheckMethod();
+ if (!nullCheckMethod.isEmpty()) {
+ const auto func = metaClass->findFunction(nullCheckMethod);
+ if (!func)
+ throw Exception(msgMethodNotFound(metaClass, nullCheckMethod));
+ return BoolCastFunction{func, true};
+ }
+ }
+
+ auto mode = te->operatorBoolMode();
+ if (useOperatorBoolAsNbNonZero()
+ ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
+ const auto func = metaClass->findOperatorBool();
+ if (func)
+ return BoolCastFunction{func, false};
+ }
+
+ mode = te->isNullMode();
+ if (useIsNullAsNbNonZero()
+ ? mode != TypeSystem::BoolCast::Disabled : mode == TypeSystem::BoolCast::Enabled) {
+ const auto func = metaClass->findQtIsNullMethod();
+ if (func)
+ return BoolCastFunction{func, true};
+ }
+ return std::nullopt;
+}
+
static bool isInplaceAdd(const AbstractMetaFunctionCPtr &func)
{
return func->name() == u"operator+=";
diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h
index 7febd1c0e..ade6e2d56 100644
--- a/sources/shiboken6/generator/shiboken/shibokengenerator.h
+++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h
@@ -14,6 +14,7 @@
#include <QtCore/QRegularExpression>
#include <array>
+#include <optional>
class EnumTypeEntry;
class FlagsTypeEntry;
@@ -28,6 +29,15 @@ struct ShibokenGeneratorOptions;
QT_FORWARD_DECLARE_CLASS(TextStream)
+// Function to be used for implementing nb_bool
+struct BoolCastFunction
+{
+ AbstractMetaFunctionCPtr function;
+ bool invert = false; // Function is "isNull()", (invert result).
+};
+
+using BoolCastFunctionOptional = std::optional<BoolCastFunction>;
+
/**
* Abstract generator that contains common methods used in CppGenerator and HeaderGenerator.
*/
@@ -98,6 +108,8 @@ protected:
static QList<AbstractMetaFunctionCList>
numberProtocolOperators(const AbstractMetaClassCPtr &scope);
+ static BoolCastFunctionOptional boolCast(const AbstractMetaClassCPtr &scope);
+
/**
* Returns all different inherited overloads of func, and includes func as well.
* The function can be called multiple times without duplication.
@@ -352,6 +364,7 @@ private:
static FunctionGroups getFunctionGroupsImpl(const AbstractMetaClassCPtr &scope);
static QList<AbstractMetaFunctionCList>
getNumberProtocolOperators(const AbstractMetaClassCPtr &metaClass);
+ static BoolCastFunctionOptional getBoolCast(const AbstractMetaClassCPtr &metaClass);
static bool classNeedsGetattroFunctionImpl(const AbstractMetaClassCPtr &metaClass);
QString translateTypeForWrapperMethod(const AbstractMetaType &cType,