From c04a2492fe54776a58d72f34282a05e5f9e1158d Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 11 Sep 2023 14:34:10 +0200 Subject: 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 Reviewed-by: Adrian Herrmann (cherry picked from commit f4d3b539e8938225faebdade31009116d737bd8c) Reviewed-by: Qt CI Bot --- .../shiboken6/generator/shiboken/cppgenerator.cpp | 42 --------------- .../shiboken6/generator/shiboken/cppgenerator.h | 10 +--- .../generator/shiboken/shibokengenerator.cpp | 61 ++++++++++++++++++++-- .../generator/shiboken/shibokengenerator.h | 13 +++++ 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(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 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; - 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 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 #include #include +#include #include #include @@ -92,6 +93,7 @@ struct GeneratorClassInfoCacheEntry { ShibokenGenerator::FunctionGroups functionGroups; QList 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 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 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(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 #include +#include 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; + /** * Abstract generator that contains common methods used in CppGenerator and HeaderGenerator. */ @@ -98,6 +108,8 @@ protected: static QList 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 getNumberProtocolOperators(const AbstractMetaClassCPtr &metaClass); + static BoolCastFunctionOptional getBoolCast(const AbstractMetaClassCPtr &metaClass); static bool classNeedsGetattroFunctionImpl(const AbstractMetaClassCPtr &metaClass); QString translateTypeForWrapperMethod(const AbstractMetaType &cType, -- cgit v1.2.3