diff options
Diffstat (limited to 'sources/shiboken6/generator/shiboken/shibokengenerator.h')
-rw-r--r-- | sources/shiboken6/generator/shiboken/shibokengenerator.h | 234 |
1 files changed, 131 insertions, 103 deletions
diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h index 767af579d..22ee73fa2 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.h +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h @@ -6,20 +6,37 @@ #include <generator.h> -#include "customconversion.h" +#include "customconversion_typedefs.h" +#include "abstractmetalang_enums.h" +#include "typesystem_typedefs.h" +#include "typesystem_enums.h" #include <QtCore/QRegularExpression> #include <array> +#include <optional> +class EnumTypeEntry; +class FlagsTypeEntry; class DocParser; class CodeSnip; class QPropertySpec; class OverloadData; -class TextStream; +class TargetToNativeConversion; struct GeneratorClassInfoCacheEntry; +struct IncludeGroup; +struct ShibokenGeneratorOptions; + +class TextStream; -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. @@ -27,6 +44,8 @@ QT_FORWARD_DECLARE_CLASS(TextStream) class ShibokenGenerator : public Generator { public: + Q_DISABLE_COPY_MOVE(ShibokenGenerator) + /// Besides the actual bindings (see AbstractMetaFunction::generateBinding(), /// some functions need to be generated into the wrapper class /// (virtual method/avoid protected hack expose). @@ -70,15 +89,18 @@ public: const char *name() const override { return "Shiboken"; } + static QList<OptionDescription> options(); + static std::shared_ptr<OptionsParser> createOptionsParser(); + static QString minimalConstructorExpression(const ApiExtractorResult &api, const AbstractMetaType &type); static QString minimalConstructorExpression(const ApiExtractorResult &api, - const TypeEntry *type); + const TypeEntryCPtr &type); protected: bool doSetup() override; - GeneratorContext contextForClass(const AbstractMetaClass *c) const override; + GeneratorContext contextForClass(const AbstractMetaClassCPtr &c) const override; /** * Returns a map with all functions grouped, the function name is used as key. @@ -86,7 +108,12 @@ protected: * \param scope Where to search for functions, null means all global functions. */ FunctionGroups getGlobalFunctionGroups() const; - static FunctionGroups getFunctionGroups(const AbstractMetaClass *scope); + static FunctionGroups getFunctionGroups(const AbstractMetaClassCPtr &scope); + + 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. @@ -118,6 +145,7 @@ protected: /// Replaces variables for the user's custom code at global or class level. void processCodeSnip(QString &code) const; + void processCodeSnip(QString &code, const QString &context) const; void processClassCodeSnip(QString &code, const GeneratorContext &context) const; /** @@ -143,145 +171,166 @@ protected: int arg_count = -1) const; /// Returns the top-most class that has multiple inheritance in the ancestry. - static const AbstractMetaClass *getMultipleInheritingClass(const AbstractMetaClass *metaClass); + static AbstractMetaClassCPtr + getMultipleInheritingClass(const AbstractMetaClassCPtr &metaClass); - static bool useOverrideCaching(const AbstractMetaClass *metaClass); - AttroCheck checkAttroFunctionNeeds(const AbstractMetaClass *metaClass) const; + static bool useOverrideCaching(const AbstractMetaClassCPtr &metaClass); + static AttroCheck checkAttroFunctionNeeds(const AbstractMetaClassCPtr &metaClass); - /// 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. + /// 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. static AbstractMetaFunctionCList - getMethodsWithBothStaticAndNonStaticMethods(const AbstractMetaClass *metaClass); + getMethodsWithBothStaticAndNonStaticMethods(const AbstractMetaClassCPtr &metaClass); static void writeToPythonConversion(TextStream &s, const AbstractMetaType &type, - const AbstractMetaClass *context, + const AbstractMetaClassCPtr &context, const QString &argumentName); static void writeToCppConversion(TextStream &s, const AbstractMetaType &type, - const AbstractMetaClass *context, const QString &inArgName, const QString &outArgName); static void writeToCppConversion(TextStream &s, - const AbstractMetaClass *metaClass, const QString &inArgName, + const AbstractMetaClassCPtr &metaClass, + const QString &inArgName, const QString &outArgName); /// Returns true if the argument is a pointer that rejects nullptr values. static bool shouldRejectNullPointerArgument(const AbstractMetaFunctionCPtr &func, int argIndex); - /// Verifies if the class should have a C++ wrapper generated for it, instead of only a Python wrapper. - bool shouldGenerateCppWrapper(const AbstractMetaClass *metaClass) const; + /// Verifies if the class should have a C++ wrapper generated for it, + /// instead of only a Python wrapper. + static bool shouldGenerateCppWrapper(const AbstractMetaClassCPtr &metaClass); + + static bool shouldGenerateMetaObjectFunctions(const AbstractMetaClassCPtr &metaClass); /// Returns which functions need to be generated into the wrapper class - FunctionGeneration functionGeneration(const AbstractMetaFunctionCPtr &func) const; + static FunctionGeneration functionGeneration(const AbstractMetaFunctionCPtr &func); // Return a list of implicit conversions if generation is enabled. - AbstractMetaFunctionCList implicitConversions(const TypeEntry *t) const; + AbstractMetaFunctionCList implicitConversions(const TypeEntryCPtr &t) const; + + static QString wrapperName(const AbstractMetaClassCPtr &metaClass); - QString wrapperName(const AbstractMetaClass *metaClass) const; + static QString fullPythonClassName(const AbstractMetaClassCPtr &metaClass); + + static QString headerFileNameForContext(const GeneratorContext &context); + IncludeGroup baseWrapperIncludes(const GeneratorContext &classContext) const; - static QString fullPythonClassName(const AbstractMetaClass *metaClass); static QString fullPythonFunctionName(const AbstractMetaFunctionCPtr &func, bool forceFunc); - bool wrapperDiagnostics() const { return m_wrapperDiagnostics; } + static bool wrapperDiagnostics(); static QString protectedEnumSurrogateName(const AbstractMetaEnum &metaEnum); static QString pythonPrimitiveTypeName(const QString &cppTypeName); static QString pythonOperatorFunctionName(const AbstractMetaFunctionCPtr &func); + static QList<AbstractMetaFunctionCList> + filterGroupedOperatorFunctions(const AbstractMetaClassCPtr &metaClass, + OperatorQueryOptions query); - static QString fixedCppTypeName(const CustomConversion::TargetToNativeConversion *toNative); + static QString fixedCppTypeName(const TargetToNativeConversion &toNative); static QString fixedCppTypeName(const AbstractMetaType &type); - static QString fixedCppTypeName(const TypeEntry *type, QString typeName = QString()); + static QString fixedCppTypeName(const TypeEntryCPtr &type, QString typeName = {}); static bool isNumber(const QString &cpythonApiName); - static bool isNumber(const TypeEntry *type); + static bool isNumber(const TypeEntryCPtr &type); static bool isNumber(const AbstractMetaType &type); - static bool isPyInt(const TypeEntry *type); + static bool isPyInt(const TypeEntryCPtr &type); static bool isPyInt(const AbstractMetaType &type); static bool isNullPtr(const QString &value); static QString converterObject(const AbstractMetaType &type) ; - static QString converterObject(const TypeEntry *type); + static QString converterObject(const TypeEntryCPtr &type); - static QString cpythonBaseName(const AbstractMetaClass *metaClass); - static QString cpythonBaseName(const TypeEntry *type); + static QString cpythonBaseName(const AbstractMetaClassCPtr &metaClass); + static QString cpythonBaseName(const TypeEntryCPtr &type); + static QString containerCpythonBaseName(const ContainerTypeEntryCPtr &ctype); static QString cpythonBaseName(const AbstractMetaType &type); - static QString cpythonTypeName(const AbstractMetaClass *metaClass); - static QString cpythonTypeName(const TypeEntry *type); - static QString cpythonTypeNameExt(const TypeEntry *type); - static QString cpythonTypeNameExt(const AbstractMetaType &type) ; - static QString cpythonCheckFunction(const TypeEntry *type); + static QString cpythonTypeName(const AbstractMetaClassCPtr &metaClass); + static QString cpythonTypeName(const TypeEntryCPtr &type); + static QString cpythonTypeNameExtSet(const TypeEntryCPtr &type); + static QString cpythonTypeNameExtSet(const AbstractMetaType &type); + static QString cpythonTypeNameExt(const TypeEntryCPtr &type); + static QString cpythonTypeNameExt(const AbstractMetaType &type); + static QString cpythonCheckFunction(TypeEntryCPtr type); static QString cpythonCheckFunction(AbstractMetaType metaType); - static QString cpythonIsConvertibleFunction(const TypeEntry *type); - static QString cpythonIsConvertibleFunction(AbstractMetaType metaType); + static QString cpythonIsConvertibleFunction(const TypeEntryCPtr &type); + static QString cpythonIsConvertibleFunction(const AbstractMetaType &metaType); static QString cpythonIsConvertibleFunction(const AbstractMetaArgument &metaArg); - static QString cpythonToCppConversionFunction(const AbstractMetaClass *metaClass) ; - static QString cpythonToCppConversionFunction(const AbstractMetaType &type, - const AbstractMetaClass *context = nullptr); - static QString cpythonToPythonConversionFunction(const AbstractMetaType &type, - const AbstractMetaClass *context = nullptr); - static QString cpythonToPythonConversionFunction(const AbstractMetaClass *metaClass); - static QString cpythonToPythonConversionFunction(const TypeEntry *type); + static QString cpythonToCppConversionFunction(const AbstractMetaClassCPtr &metaClass) ; + static QString cpythonToCppConversionFunction(const AbstractMetaType &type); + static QString cpythonToPythonConversionFunction(const AbstractMetaType &type); + static QString cpythonToPythonConversionFunction(const AbstractMetaClassCPtr &metaClass); + static QString cpythonToPythonConversionFunction(const TypeEntryCPtr &type); static QString cpythonFunctionName(const AbstractMetaFunctionCPtr &func) ; static QString cpythonMethodDefinitionName(const AbstractMetaFunctionCPtr &func); - static QString cpythonGettersSettersDefinitionName(const AbstractMetaClass *metaClass); - static QString cpythonGetattroFunctionName(const AbstractMetaClass *metaClass); - static QString cpythonSetattroFunctionName(const AbstractMetaClass *metaClass); + static QString cpythonGettersSettersDefinitionName(const AbstractMetaClassCPtr &metaClass); + static QString cpythonGetattroFunctionName(const AbstractMetaClassCPtr &metaClass); + static QString cpythonSetattroFunctionName(const AbstractMetaClassCPtr &metaClass); static QString cpythonGetterFunctionName(const AbstractMetaField &metaField); static QString cpythonSetterFunctionName(const AbstractMetaField &metaField); static QString cpythonGetterFunctionName(const QPropertySpec &property, - const AbstractMetaClass *metaClass); + const AbstractMetaClassCPtr &metaClass); static QString cpythonSetterFunctionName(const QPropertySpec &property, - const AbstractMetaClass *metaClass); - static QString cpythonWrapperCPtr(const AbstractMetaClass *metaClass, - const QString &argName = QStringLiteral("self")); - static QString cpythonWrapperCPtr(const AbstractMetaType &metaType, + const AbstractMetaClassCPtr &metaClass); + static QString cpythonWrapperCPtr(const AbstractMetaClassCPtr &metaClass, + const QString &argName = QLatin1StringView("self")); + static QString cpythonWrapperCPtr(const AbstractMetaType &metaType, const QString &argName); - static QString cpythonWrapperCPtr(const TypeEntry *type, const QString &argName); + static QString cpythonWrapperCPtr(const TypeEntryCPtr &type, const QString &argName); - static QString cpythonEnumName(const EnumTypeEntry *enumEntry); + static QString cpythonEnumName(const EnumTypeEntryCPtr &enumEntry); static QString cpythonEnumName(const AbstractMetaEnum &metaEnum); - static QString cpythonFlagsName(const FlagsTypeEntry *flagsEntry); + static QString cpythonFlagsName(const FlagsTypeEntryCPtr &flagsEntry); static QString cpythonFlagsName(const AbstractMetaEnum *metaEnum); - /// Returns the special cast function name, the function used to proper cast class with multiple inheritance. - static QString cpythonSpecialCastFunctionName(const AbstractMetaClass *metaClass); + /// Returns the special cast function name, the function used to proper cast + /// class with multiple inheritance. + static QString cpythonSpecialCastFunctionName(const AbstractMetaClassCPtr &metaClass); - /// Returns the file name for the module global header. If no module name is provided the current will be used. + /// Returns the file name for the module global header. If no module name + /// is provided the current will be used. static QString getModuleHeaderFileName(const QString &moduleName = QString()); static QString getPrivateModuleHeaderFileName(const QString &moduleName = QString()); - OptionDescriptions options() const override; - bool handleOption(const QString &key, const QString &value) override; + /// Includes for header (native wrapper class) or binding source + QList<IncludeGroup> classIncludes(const AbstractMetaClassCPtr &metaClass) const; /// Returns true if the user enabled the so called "parent constructor heuristic". - bool useCtorHeuristic() const; + static bool useCtorHeuristic(); /// Returns true if the user enabled the so called "return value heuristic". - bool useReturnValueHeuristic() const; + static bool useReturnValueHeuristic(); /// Returns true if the generator should use the result of isNull()const to compute boolean casts. - bool useIsNullAsNbNonZero() const; + static bool useIsNullAsNbBool(); + /// Whether to generate lean module headers + static bool leanHeaders(); /// Returns true if the generator should use operator bool to compute boolean casts. - bool useOperatorBoolAsNbNonZero() const; + static bool useOperatorBoolAsNbBool(); /// Generate implicit conversions of function arguments - bool generateImplicitConversions() const; + static bool generateImplicitConversions(); + static QString cppApiVariableNameOld(const QString &moduleName = {}); static QString cppApiVariableName(const QString &moduleName = QString()); static QString pythonModuleObjectName(const QString &moduleName = QString()); static QString convertersVariableName(const QString &moduleName = QString()); /// Returns the type index variable name for a given class. - static QString getTypeIndexVariableName(const AbstractMetaClass *metaClass); + static QString getTypeIndexVariableName(const AbstractMetaClassCPtr &metaClass); /// Returns the type index variable name for a given typedef for a template /// class instantiation made of the template class and the instantiation values - static QString getTypeAlternateTemplateIndexVariableName(const AbstractMetaClass *metaClass); - static QString getTypeIndexVariableName(const TypeEntry *type); + static QString getTypeAlternateTemplateIndexVariableName(const AbstractMetaClassCPtr &metaClass); + static QString getTypeIndexVariableName(TypeEntryCPtr type); static QString getTypeIndexVariableName(const AbstractMetaType &type) ; + /// Collect all type names as an array for initializing the type/name struct. + void collectFullTypeNamesArray(QStringList &typeNames); + /// Returns true if the user don't want verbose error messages on the generated bindings. - bool verboseErrorMessagesDisabled() const; + static bool verboseErrorMessagesDisabled(); void collectContainerTypesFromConverterMacros(const QString &code, bool toPythonMacro); @@ -289,16 +338,14 @@ protected: const AbstractMetaFunctionCPtr &metaFunc, Options options = NoOption); - static void writeUnusedVariableCast(TextStream &s, const QString &variableName); - // All data about extended converters: the type entries of the target type, and a // list of AbstractMetaClasses accepted as argument for the conversion. - using ExtendedConverterData = QHash<const TypeEntry *, AbstractMetaClassCList>; + using ExtendedConverterData = QHash<TypeEntryCPtr, AbstractMetaClassCList>; /// Returns all extended conversions for the current module. ExtendedConverterData getExtendedConverters() const; /// Returns a list of converters for the non wrapper types of the current module. - static QList<const CustomConversion *> getPrimitiveCustomConversions() ; + static QList<CustomConversionPtr> getPrimitiveCustomConversions(); /// Returns true if the Python wrapper for the received OverloadData must accept a list of arguments. bool pythonFunctionWrapperUsesListOfArguments(const AbstractMetaFunctionCPtr &func) const; @@ -316,16 +363,20 @@ protected: private: static QString getModuleHeaderFileBaseName(const QString &moduleName = QString()); static QString cpythonGetterFunctionName(const QString &name, - const AbstractMetaClass *enclosingClass); + const AbstractMetaClassCPtr &enclosingClass); static QString cpythonSetterFunctionName(const QString &name, - const AbstractMetaClass *enclosingClass); + const AbstractMetaClassCPtr &enclosingClass); - static const GeneratorClassInfoCacheEntry &getGeneratorClassInfo(const AbstractMetaClass *scope); - static FunctionGroups getFunctionGroupsImpl(const AbstractMetaClass *scope); - static bool classNeedsGetattroFunctionImpl(const AbstractMetaClass *metaClass); + static const GeneratorClassInfoCacheEntry & + getGeneratorClassInfo(const AbstractMetaClassCPtr &scope); + 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, - const AbstractMetaClass *context, + const AbstractMetaClassCPtr &context, Options opt = NoOption) const; /** @@ -334,7 +385,7 @@ private: * \param func the metafunction to be searched in subclasses. * \param seen the function's minimal signatures already seen. */ - static void getInheritedOverloads(const AbstractMetaClass *scope, + static void getInheritedOverloads(const AbstractMetaClassCPtr &scope, AbstractMetaFunctionCList *overloads); @@ -363,7 +414,7 @@ private: QString functionReturnType(const AbstractMetaFunctionCPtr &func, Options options = NoOption) const; /// Utility function for writeCodeSnips. - using ArgumentVarReplacementPair = QPair<AbstractMetaArgument, QString>; + using ArgumentVarReplacementPair = std::pair<AbstractMetaArgument, QString>; using ArgumentVarReplacementList = QList<ArgumentVarReplacementPair>; static ArgumentVarReplacementList getArgumentReplacement(const AbstractMetaFunctionCPtr &func, @@ -425,14 +476,7 @@ private: void replaceTemplateVariables(QString &code, const AbstractMetaFunctionCPtr &func) const; - bool m_useCtorHeuristic = false; - bool m_userReturnValueHeuristic = false; - bool m_verboseErrorMessagesDisabled = false; - bool m_useIsNullAsNbNonZero = false; - bool m_useOperatorBoolAsNbNonZero = false; - // FIXME PYSIDE 7 Flip generateImplicitConversions default or remove? - bool m_generateImplicitConversions = true; - bool m_wrapperDiagnostics = false; + static ShibokenGeneratorOptions m_options; /// Type system converter variable replacement names and regular expressions. static const QHash<int, QString> &typeSystemConvName(); @@ -444,20 +488,4 @@ private: Q_DECLARE_OPERATORS_FOR_FLAGS(ShibokenGenerator::FunctionGeneration); Q_DECLARE_OPERATORS_FOR_FLAGS(ShibokenGenerator::AttroCheck); -extern const QString CPP_ARG; -extern const QString CPP_ARG_REMOVED; -extern const QString CPP_RETURN_VAR; -extern const QString CPP_SELF_VAR; -extern const QString NULL_PTR; -extern const QString PYTHON_ARG; -extern const QString PYTHON_ARGS; -extern const QString PYTHON_OVERRIDE_VAR; -extern const QString PYTHON_RETURN_VAR; -extern const QString PYTHON_TO_CPP_VAR; -extern const QString SMART_POINTER_GETTER; - -extern const QString CONV_RULE_OUT_VAR_SUFFIX; -extern const QString BEGIN_ALLOW_THREADS; -extern const QString END_ALLOW_THREADS; - #endif // SHIBOKENGENERATOR_H |