diff options
Diffstat (limited to 'sources/shiboken2/generator/shiboken2/shibokengenerator.h')
-rw-r--r-- | sources/shiboken2/generator/shiboken2/shibokengenerator.h | 113 |
1 files changed, 74 insertions, 39 deletions
diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.h b/sources/shiboken2/generator/shiboken2/shibokengenerator.h index 7970ceb94..cbe796313 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.h +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.h @@ -53,6 +53,7 @@ extern const char *END_ALLOW_THREADS; class DocParser; class CodeSnip; +class QPropertySpec; class OverloadData; struct GeneratorClassInfoCacheEntry; @@ -64,6 +65,21 @@ QT_FORWARD_DECLARE_CLASS(QTextStream) class ShibokenGenerator : public Generator { public: + enum class AttroCheckFlag + { + None = 0x0, + GetattroOverloads = 0x01, + GetattroSmartPointer = 0x02, + GetattroUser = 0x04, // Injected code + GetattroMask = 0x0F, + SetattroQObject = 0x10, + SetattroSmartPointer = 0x20, + SetattroMethodOverride = 0x40, + SetattroUser = 0x80, // Injected code + SetattroMask = 0xF0, + }; + Q_DECLARE_FLAGS(AttroCheck, AttroCheckFlag); + using FunctionGroups = QMap<QString, AbstractMetaFunctionList>; // Sorted ShibokenGenerator(); @@ -74,6 +90,9 @@ public: /// Returns a list of all ancestor classes for the given class. AbstractMetaClassList getAllAncestors(const AbstractMetaClass *metaClass) const; + /// Returns true if the user enabled PySide extensions. + bool usePySideExtensions() const; + protected: bool doSetup() override; @@ -92,6 +111,8 @@ protected: const AbstractMetaFunction *func, Options options = NoOption) const override; + GeneratorContext contextForClass(const AbstractMetaClass *c) const override; + /** * Returns a map with all functions grouped, the function name is used as key. * Example of return value: { "foo" -> ["foo(int)", "foo(int, long)], "bar" -> "bar(double)"} @@ -109,11 +130,15 @@ protected: AbstractMetaFunctionList getFunctionAndInheritedOverloads(const AbstractMetaFunction *func, QSet<QString> *seen); /// Write user's custom code snippets at class or module level. + void writeClassCodeSnips(QTextStream &s, + const QVector<CodeSnip> & codeSnips, + TypeSystem::CodeSnipPosition position, + TypeSystem::Language language, + const GeneratorContext &context); void writeCodeSnips(QTextStream &s, const QVector<CodeSnip> & codeSnips, TypeSystem::CodeSnipPosition position, - TypeSystem::Language language, - const AbstractMetaClass *context = nullptr); + TypeSystem::Language language); /// Write user's custom code snippets at function level. void writeCodeSnips(QTextStream &s, const QVector<CodeSnip> & codeSnips, @@ -123,7 +148,8 @@ protected: const AbstractMetaArgument *lastArg = nullptr); /// Replaces variables for the user's custom code at global or class level. - void processCodeSnip(QString &code, const AbstractMetaClass *context = nullptr); + void processCodeSnip(QString &code); + void processClassCodeSnip(QString &code, const GeneratorContext &context); /** * Verifies if any of the function's code injections of the "native" @@ -140,7 +166,8 @@ protected: * \param func the function to check * \return true if the function's code snippets call the wrapped C++ function */ - bool injectedCodeCallsCppFunction(const AbstractMetaFunction *func); + bool injectedCodeCallsCppFunction(const GeneratorContext &context, + const AbstractMetaFunction *func); /** * Verifies if any of the function's code injections of the "native" class makes a @@ -181,11 +208,8 @@ protected: /// Returns the top-most class that has multiple inheritance in the ancestry. static const AbstractMetaClass *getMultipleInheritingClass(const AbstractMetaClass *metaClass); - /// Returns true if the class needs to have a getattro function. - bool classNeedsGetattroFunction(const AbstractMetaClass *metaClass); - - /// Returns true if the class needs to have a setattro function. - bool classNeedsSetattroFunction(const AbstractMetaClass *metaClass); + static bool useOverrideCaching(const AbstractMetaClass *metaClass); + AttroCheck checkAttroFunctionNeeds(const AbstractMetaClass *metaClass) const; /// Returns a list of methods of the given class where each one is part of a different overload with both static and non-static method. AbstractMetaFunctionList getMethodsWithBothStaticAndNonStaticMethods(const AbstractMetaClass *metaClass); @@ -204,16 +228,15 @@ protected: /// Verifies if the class should have a C++ wrapper generated for it, instead of only a Python wrapper. bool shouldGenerateCppWrapper(const AbstractMetaClass *metaClass) const; - /// Adds enums eligible for generation from classes/namespaces marked not to be generated. - static void lookForEnumsInClassesNotToBeGenerated(AbstractMetaEnumList &enumList, const AbstractMetaClass *metaClass); - /// Returns the enclosing class for an enum, or nullptr if it should be global. - const AbstractMetaClass *getProperEnclosingClassForEnum(const AbstractMetaEnum *metaEnum); + /// Condition to call WriteVirtualMethodNative. Was extracted because also used to count these calls. + bool shouldWriteVirtualMethodNative(const AbstractMetaFunction *func); QString wrapperName(const AbstractMetaClass *metaClass) const; - QString wrapperName(const AbstractMetaType *metaType) const; QString fullPythonClassName(const AbstractMetaClass *metaClass); - QString fullPythonFunctionName(const AbstractMetaFunction *func); + QString fullPythonFunctionName(const AbstractMetaFunction *func, bool forceFunc); + + bool wrapperDiagnostics() const { return m_wrapperDiagnostics; } static QString protectedEnumSurrogateName(const AbstractMetaEnum *metaEnum); static QString protectedFieldGetterName(const AbstractMetaField *field); @@ -279,16 +302,18 @@ protected: static bool visibilityModifiedToPrivate(const AbstractMetaFunction *func); + static bool isNullPtr(const QString &value); + QString converterObject(const AbstractMetaType *type); QString converterObject(const TypeEntry *type); - QString cpythonBaseName(const AbstractMetaClass *metaClass); - QString cpythonBaseName(const TypeEntry *type); - QString cpythonBaseName(const AbstractMetaType *type); - QString cpythonTypeName(const AbstractMetaClass *metaClass); - QString cpythonTypeName(const TypeEntry *type); - QString cpythonTypeNameExt(const TypeEntry *type); - QString cpythonTypeNameExt(const AbstractMetaType *type); + static QString cpythonBaseName(const AbstractMetaClass *metaClass); + static QString cpythonBaseName(const TypeEntry *type); + static QString cpythonBaseName(const AbstractMetaType *type); + static QString cpythonTypeName(const AbstractMetaClass *metaClass); + static QString cpythonTypeName(const TypeEntry *type); + QString cpythonTypeNameExt(const TypeEntry *type) const; + QString cpythonTypeNameExt(const AbstractMetaType *type) const; QString cpythonCheckFunction(const TypeEntry *type, bool genericNumberType = false); QString cpythonCheckFunction(const AbstractMetaType *metaType, bool genericNumberType = false); /** @@ -315,14 +340,18 @@ protected: QString cpythonFunctionName(const AbstractMetaFunction *func); QString cpythonMethodDefinitionName(const AbstractMetaFunction *func); QString cpythonGettersSettersDefinitionName(const AbstractMetaClass *metaClass); - QString cpythonGetattroFunctionName(const AbstractMetaClass *metaClass); - QString cpythonSetattroFunctionName(const AbstractMetaClass *metaClass); - QString cpythonGetterFunctionName(const AbstractMetaField *metaField); - QString cpythonSetterFunctionName(const AbstractMetaField *metaField); + static QString cpythonGetattroFunctionName(const AbstractMetaClass *metaClass); + static QString cpythonSetattroFunctionName(const AbstractMetaClass *metaClass); + static QString cpythonGetterFunctionName(const AbstractMetaField *metaField); + static QString cpythonSetterFunctionName(const AbstractMetaField *metaField); + static QString cpythonGetterFunctionName(const QPropertySpec *property, + const AbstractMetaClass *metaClass); + static QString cpythonSetterFunctionName(const QPropertySpec *property, + const AbstractMetaClass *metaClass); QString cpythonWrapperCPtr(const AbstractMetaClass *metaClass, - const QString &argName = QLatin1String("self")); - QString cpythonWrapperCPtr(const AbstractMetaType *metaType, const QString &argName); - QString cpythonWrapperCPtr(const TypeEntry *type, const QString &argName); + const QString &argName = QLatin1String("self")) const; + QString cpythonWrapperCPtr(const AbstractMetaType *metaType, const QString &argName) const; + QString cpythonWrapperCPtr(const TypeEntry *type, const QString &argName) const; /// Guesses the scope to where belongs an argument's default value. QString guessScopeForDefaultValue(const AbstractMetaFunction *func, @@ -331,13 +360,13 @@ protected: const AbstractMetaArgument *arg, const QString &value) const; - QString cpythonEnumName(const EnumTypeEntry *enumEntry); - QString cpythonEnumName(const AbstractMetaEnum *metaEnum); + static QString cpythonEnumName(const EnumTypeEntry *enumEntry); + static QString cpythonEnumName(const AbstractMetaEnum *metaEnum); - QString cpythonFlagsName(const FlagsTypeEntry *flagsEntry); - QString cpythonFlagsName(const AbstractMetaEnum *metaEnum); + static QString cpythonFlagsName(const FlagsTypeEntry *flagsEntry); + static QString cpythonFlagsName(const AbstractMetaEnum *metaEnum); /// Returns the special cast function name, the function used to proper cast class with multiple inheritance. - QString cpythonSpecialCastFunctionName(const AbstractMetaClass *metaClass); + static QString cpythonSpecialCastFunctionName(const AbstractMetaClass *metaClass); QString getFormatUnitString(const AbstractMetaFunction *func, bool incRef = false) const; @@ -351,8 +380,6 @@ protected: bool useCtorHeuristic() const; /// Returns true if the user enabled the so called "return value heuristic". bool useReturnValueHeuristic() const; - /// Returns true if the user enabled PySide extensions. - bool usePySideExtensions() const; /// Returns true if the generator should use the result of isNull()const to compute boolean casts. bool useIsNullAsNbNonZero() const; /// Returns true if the generated code should use the "#define protected public" hack. @@ -366,9 +393,9 @@ protected: * made of the template class and the instantiation values, or an empty string if the class isn't * derived from a template class at all. */ - QString getTypeIndexVariableName(const AbstractMetaClass *metaClass, bool alternativeTemplateName = false); - QString getTypeIndexVariableName(const TypeEntry *type); - QString getTypeIndexVariableName(const AbstractMetaType *type); + QString getTypeIndexVariableName(const AbstractMetaClass *metaClass, bool alternativeTemplateName = false) const; + QString getTypeIndexVariableName(const TypeEntry *type) const; + QString getTypeIndexVariableName(const AbstractMetaType *type) const; /// Returns true if the user don't want verbose error messages on the generated bindings. bool verboseErrorMessagesDisabled() const; @@ -437,6 +464,11 @@ protected: static QStringList m_knownPythonTypes; private: + static QString cpythonGetterFunctionName(const QString &name, + const AbstractMetaClass *enclosingClass); + static QString cpythonSetterFunctionName(const QString &name, + const AbstractMetaClass *enclosingClass); + static const GeneratorClassInfoCacheEntry &getGeneratorClassInfo(const AbstractMetaClass *scope); static FunctionGroups getFunctionGroupsImpl(const AbstractMetaClass *scope); static bool classNeedsGetattroFunctionImpl(const AbstractMetaClass *metaClass); @@ -536,6 +568,7 @@ private: bool m_verboseErrorMessagesDisabled = false; bool m_useIsNullAsNbNonZero = false; bool m_avoidProtectedHack = false; + bool m_wrapperDiagnostics = false; using AbstractMetaTypeCache = QHash<QString, AbstractMetaType *>; AbstractMetaTypeCache m_metaTypeFromStringCache; @@ -545,4 +578,6 @@ private: QRegularExpression m_typeSystemConvRegEx[TypeSystemConverterVariables]; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(ShibokenGenerator::AttroCheck); + #endif // SHIBOKENGENERATOR_H |