diff options
Diffstat (limited to 'sources/shiboken2/ApiExtractor/typesystem.h')
-rw-r--r-- | sources/shiboken2/ApiExtractor/typesystem.h | 370 |
1 files changed, 163 insertions, 207 deletions
diff --git a/sources/shiboken2/ApiExtractor/typesystem.h b/sources/shiboken2/ApiExtractor/typesystem.h index 48733053d..6635b501e 100644 --- a/sources/shiboken2/ApiExtractor/typesystem.h +++ b/sources/shiboken2/ApiExtractor/typesystem.h @@ -32,6 +32,7 @@ #include "typesystem_enums.h" #include "typesystem_typedefs.h" #include "include.h" +#include "sourcelocation.h" #include <QtCore/QHash> #include <QtCore/qobjectdefs.h> @@ -115,7 +116,7 @@ class CodeSnipAbstract public: QString code() const; - void addCode(const QString &code) { codeList.append(CodeSnipFragment(code)); } + void addCode(const QString &code); void addCode(const QStringRef &code) { addCode(code.toString()); } void addTemplateInstance(TemplateInstance *ti) @@ -124,6 +125,10 @@ public: } QVector<CodeSnipFragment> codeList; + + static QString fixSpaces(QString code); + static QString dedent(const QString &code); + static void prependCode(QString *code, QString firstLine); }; class CustomFunction : public CodeSnipAbstract @@ -228,7 +233,7 @@ struct ArgumentModification struct Modification { - enum Modifiers { + enum Modifiers : uint { InvalidModifier = 0x0000, Private = 0x0001, Protected = 0x0002, @@ -351,6 +356,9 @@ struct FunctionModification: public Modification TypeSystem::ExceptionHandling exceptionHandling() const { return m_exceptionHandling; } void setExceptionHandling(TypeSystem::ExceptionHandling e) { m_exceptionHandling = e; } + int overloadNumber() const { return m_overloadNumber; } + void setOverloadNumber(int overloadNumber) { m_overloadNumber = overloadNumber; } + QString toString() const; #ifndef QT_NO_DEBUG_STREAM @@ -366,6 +374,7 @@ private: QString m_signature; QString m_originalSignature; QRegularExpression m_signaturePattern; + int m_overloadNumber = TypeSystem::OverloadNumberUnset; bool m_thread = false; AllowThread m_allowThread = AllowThread::Unspecified; TypeSystem::ExceptionHandling m_exceptionHandling = TypeSystem::ExceptionHandling::Unspecified; @@ -499,7 +508,6 @@ QDebug operator<<(QDebug d, const AddedFunction::Argument &a); QDebug operator<<(QDebug d, const AddedFunction &af); #endif -class InterfaceTypeEntry; class ObjectTypeEntry; class DocModification @@ -511,8 +519,8 @@ public: explicit DocModification(TypeSystem::DocModificationMode mode, const QString& signature) : m_signature(signature), m_mode(mode) {} - void setCode(const QString& code) { m_code = code; } - void setCode(const QStringRef& code) { m_code = code.toString(); } + void setCode(const QString& code); + void setCode(const QStringRef& code) { setCode(code.toString()); } QString code() const { @@ -543,6 +551,21 @@ private: }; class CustomConversion; +class TypeSystemTypeEntry; + +struct TypeSystemProperty +{ + bool isValid() const { return !name.isEmpty() && !read.isEmpty() && !type.isEmpty(); } + + QString type; + QString name; + QString read; + QString write; + QString reset; + QString designable; + // Indicates whether actual code is generated instead of relying on libpyside. + bool generateGetSetDef = false; +}; class TypeEntry { @@ -559,21 +582,15 @@ public: FlagsType, EnumType, EnumValue, + ConstantValueType, TemplateArgumentType, - ThreadType, BasicValueType, - StringType, ContainerType, - InterfaceType, ObjectType, NamespaceType, - VariantType, - JObjectWrapperType, - CharType, ArrayType, TypeSystemType, CustomType, - TargetLangType, FunctionType, SmartPointerType, TypedefType @@ -581,17 +598,15 @@ public: Q_ENUM(Type) enum CodeGeneration { - GenerateTargetLang = 0x0001, - GenerateCpp = 0x0002, - GenerateForSubclass = 0x0004, - - GenerateNothing = 0, - GenerateAll = 0xffff, - GenerateCode = GenerateTargetLang | GenerateCpp + GenerateNothing, // Rejection, private type, ConstantValueTypeEntry or similar + GenerationDisabled, // generate='no' in type system + GenerateCode, // Generate code + GenerateForSubclass, // Inherited from a loaded dependent type system. }; Q_ENUM(CodeGeneration) - explicit TypeEntry(const QString &name, Type t, const QVersionNumber &vr); + explicit TypeEntry(const QString &entryName, Type t, const QVersionNumber &vr, + const TypeEntry *parent); virtual ~TypeEntry(); @@ -599,6 +614,14 @@ public: { return m_type; } + + const TypeEntry *parent() const { return m_parent; } + void setParent(const TypeEntry *p) { m_parent = p; } + bool isChildOf(const TypeEntry *p) const; + const TypeSystemTypeEntry *typeSystemTypeEntry() const; + // cf AbstractMetaClass::targetLangEnclosingClass() + const TypeEntry *targetLangEnclosingEntry() const; + bool isPrimitive() const { return m_type == PrimitiveType; @@ -611,22 +634,10 @@ public: { return m_type == FlagsType; } - bool isInterface() const - { - return m_type == InterfaceType; - } bool isObject() const { return m_type == ObjectType; } - bool isString() const - { - return m_type == StringType; - } - bool isChar() const - { - return m_type == CharType; - } bool isNamespace() const { return m_type == NamespaceType; @@ -639,14 +650,6 @@ public: { return m_type == SmartPointerType; } - bool isVariant() const - { - return m_type == VariantType; - } - bool isJObjectWrapper() const - { - return m_type == JObjectWrapperType; - } bool isArray() const { return m_type == ArrayType; @@ -663,18 +666,10 @@ public: { return m_type == VarargsType; } - bool isThread() const - { - return m_type == ThreadType; - } bool isCustom() const { return m_type == CustomType; } - bool isBasicValue() const - { - return m_type == BasicValueType; - } bool isTypeSystem() const { return m_type == TypeSystemType; @@ -699,16 +694,17 @@ public: } // The type's name in C++, fully qualified - QString name() const - { - return m_name; - } + QString name() const { return m_name; } + // C++ excluding inline namespaces + QString shortName() const; + // Name as specified in XML + QString entryName() const { return m_entryName; } - uint codeGeneration() const + CodeGeneration codeGeneration() const { return m_codeGeneration; } - void setCodeGeneration(uint cg) + void setCodeGeneration(CodeGeneration cg) { m_codeGeneration = cg; } @@ -720,8 +716,7 @@ public: // on 'load-typesystem' tag inline bool generateCode() const { - return m_codeGeneration != TypeEntry::GenerateForSubclass - && m_codeGeneration != TypeEntry::GenerateNothing; + return m_codeGeneration == GenerateCode; } int revision() const { return m_revision; } @@ -749,33 +744,15 @@ public: } // The type's name in TargetLang - virtual QString targetLangName() const - { - return m_name; - } - - // The type to lookup when converting to TargetLang - virtual QString lookupName() const - { - return targetLangName(); - } + QString targetLangName() const; // "Foo.Bar" + void setTargetLangName(const QString &n) { m_cachedTargetLangName = n; } + QString targetLangEntryName() const; // "Bar" // The package QString targetLangPackage() const { return m_targetLangPackage; } void setTargetLangPackage(const QString &p) { m_targetLangPackage = p; } - virtual QString qualifiedTargetLangName() const - { - QString pkg = targetLangPackage(); - if (pkg.isEmpty()) - return targetLangName(); - return pkg + QLatin1Char('.') + targetLangName(); - } - - virtual InterfaceTypeEntry *designatedInterface() const - { - return nullptr; - } + QString qualifiedTargetLangName() const; void setCustomConstructor(const CustomFunction &func) { @@ -804,11 +781,6 @@ public: return false; } - virtual bool isNativeIdBased() const - { - return false; - } - CodeSnipList codeSnips() const; void setCodeSnips(const CodeSnipList &codeSnips) { @@ -828,7 +800,7 @@ public: return m_docModifications; } - IncludeList extraIncludes() const + const IncludeList &extraIncludes() const { return m_extraIncludes; } @@ -836,13 +808,7 @@ public: { m_extraIncludes = includes; } - void addExtraInclude(const Include &include) - { - if (!m_includesUsed.value(include.name(), false)) { - m_extraIncludes << include; - m_includesUsed[include.name()] = true; - } - } + void addExtraInclude(const Include &newInclude); Include include() const { @@ -897,6 +863,9 @@ public: void useAsTypedef(const TypeEntry *source); + SourceLocation sourceLocation() const; + void setSourceLocation(const SourceLocation &sourceLocation); + #ifndef QT_NO_DEBUG_STREAM virtual void formatDebug(QDebug &d) const; #endif @@ -904,30 +873,38 @@ public: protected: TypeEntry(const TypeEntry &); + virtual QString buildTargetLangName() const; + private: - QString m_name; + const TypeEntry *m_parent; + QString m_name; // C++ fully qualified + mutable QString m_cachedShortName; // C++ excluding inline namespaces + QString m_entryName; QString m_targetLangPackage; - Type m_type; - uint m_codeGeneration = GenerateAll; + mutable QString m_cachedTargetLangName; // "Foo.Bar" + mutable QString m_cachedTargetLangEntryName; // "Bar" CustomFunction m_customConstructor; CustomFunction m_customDestructor; CodeSnipList m_codeSnips; DocModificationList m_docModifications; IncludeList m_extraIncludes; Include m_include; - QHash<QString, bool> m_includesUsed; QString m_conversionRule; - bool m_stream = false; QVersionNumber m_version; CustomConversion *m_customConversion = nullptr; + SourceLocation m_sourceLocation; // XML file + CodeGeneration m_codeGeneration = GenerateCode; int m_revision = 0; int m_sbkIndex = 0; + Type m_type; + bool m_stream = false; }; class TypeSystemTypeEntry : public TypeEntry { public: - explicit TypeSystemTypeEntry(const QString &name, const QVersionNumber &vr); + explicit TypeSystemTypeEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); TypeEntry *clone() const override; @@ -960,7 +937,8 @@ protected: class TemplateArgumentEntry : public TypeEntry { public: - explicit TemplateArgumentEntry(const QString &name, const QVersionNumber &vr); + explicit TemplateArgumentEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); int ordinal() const { @@ -983,7 +961,8 @@ private: class ArrayTypeEntry : public TypeEntry { public: - explicit ArrayTypeEntry(const TypeEntry *nested_type, const QVersionNumber &vr); + explicit ArrayTypeEntry(const TypeEntry *nested_type, const QVersionNumber &vr, + const TypeEntry *parent); void setNestedTypeEntry(TypeEntry *nested) { @@ -994,7 +973,6 @@ public: return m_nestedType; } - QString targetLangName() const override; QString targetLangApiName() const override; TypeEntry *clone() const override; @@ -1002,6 +980,8 @@ public: protected: ArrayTypeEntry(const ArrayTypeEntry &); + QString buildTargetLangName() const override; + private: const TypeEntry *m_nestedType; }; @@ -1010,13 +990,8 @@ private: class PrimitiveTypeEntry : public TypeEntry { public: - explicit PrimitiveTypeEntry(const QString &name, const QVersionNumber &vr); - - QString targetLangName() const override; - void setTargetLangName(const QString &targetLangName) - { - m_targetLangName = targetLangName; - } + explicit PrimitiveTypeEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); QString targetLangApiName() const override; void setTargetLangApiName(const QString &targetLangApiName) @@ -1077,7 +1052,6 @@ protected: PrimitiveTypeEntry(const PrimitiveTypeEntry &); private: - QString m_targetLangName; QString m_targetLangApiName; QString m_defaultConstructor; uint m_preferredTargetLangType : 1; @@ -1089,23 +1063,15 @@ class EnumValueTypeEntry; class EnumTypeEntry : public TypeEntry { public: - explicit EnumTypeEntry(const QString &nspace, const QString &enumName, - const QVersionNumber &vr); + explicit EnumTypeEntry(const QString &entryName, + const QVersionNumber &vr, + const TypeEntry *parent); - QString targetLangName() const override; QString targetLangQualifier() const; - QString qualifiedTargetLangName() const override; QString targetLangApiName() const override; - QString qualifier() const - { - return m_qualifier; - } - void setQualifier(const QString &q) - { - m_qualifier = q; - } + QString qualifier() const; const EnumValueTypeEntry *nullValue() const { return m_nullValue; } void setNullValue(const EnumValueTypeEntry *n) { m_nullValue = n; } @@ -1140,9 +1106,6 @@ protected: EnumTypeEntry(const EnumTypeEntry &); private: - QString m_packageName; - QString m_qualifier; - QString m_targetLangName; const EnumValueTypeEntry *m_nullValue = nullptr; QStringList m_rejectedEnums; @@ -1156,7 +1119,9 @@ private: class EnumValueTypeEntry : public TypeEntry { public: - explicit EnumValueTypeEntry(const QString& name, const QString& value, const EnumTypeEntry* enclosingEnum, const QVersionNumber &vr); + explicit EnumValueTypeEntry(const QString& name, const QString& value, + const EnumTypeEntry* enclosingEnum, + bool isScopedEnum, const QVersionNumber &vr); QString value() const { return m_value; } const EnumTypeEntry* enclosingEnum() const { return m_enclosingEnum; } @@ -1174,10 +1139,9 @@ private: class FlagsTypeEntry : public TypeEntry { public: - explicit FlagsTypeEntry(const QString &name, const QVersionNumber &vr); + explicit FlagsTypeEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); - QString qualifiedTargetLangName() const override; - QString targetLangName() const override; QString targetLangApiName() const override; QString originalName() const @@ -1191,11 +1155,11 @@ public: QString flagsName() const { - return m_targetLangName; + return m_flagsName; } void setFlagsName(const QString &name) { - m_targetLangName = name; + m_flagsName = name; } EnumTypeEntry *originator() const @@ -1212,12 +1176,27 @@ public: protected: FlagsTypeEntry(const FlagsTypeEntry &); + QString buildTargetLangName() const override; + private: QString m_originalName; - QString m_targetLangName; + QString m_flagsName; EnumTypeEntry *m_enum = nullptr; }; +// For primitive values, typically to provide a dummy type for +// example the '2' in non-type template 'Array<2>'. +class ConstantValueTypeEntry : public TypeEntry +{ +public: + explicit ConstantValueTypeEntry(const QString& name, + const TypeEntry *parent); + + TypeEntry *clone() const override; + +protected: + ConstantValueTypeEntry(const ConstantValueTypeEntry &); +}; class ComplexTypeEntry : public TypeEntry { @@ -1233,17 +1212,11 @@ public: Unknown }; - explicit ComplexTypeEntry(const QString &name, Type t, const QVersionNumber &vr); + explicit ComplexTypeEntry(const QString &entryName, Type t, const QVersionNumber &vr, + const TypeEntry *parent); bool isComplex() const override; - void setLookupName(const QString &name) - { - m_lookupName = name; - } - - QString lookupName() const override; - QString targetLangApiName() const override; void setTypeFlags(TypeFlags flags) @@ -1293,6 +1266,9 @@ public: return m_fieldMods; } + const QList<TypeSystemProperty> &properties() const { return m_properties; } + void addProperty(const TypeSystemProperty &p) { m_properties.append(p); } + QString defaultSuperclass() const { return m_defaultSuperclass; @@ -1335,12 +1311,6 @@ public: m_targetType = code; } - QString targetLangName() const override; - void setTargetLangName(const QString &name) - { - m_targetLangName = name; - } - bool isGenericClass() const { return m_genericClass; @@ -1405,17 +1375,16 @@ private: AddedFunctionList m_addedFunctions; FunctionModificationList m_functionMods; FieldModificationList m_fieldMods; + QList<TypeSystemProperty> m_properties; QString m_defaultConstructor; QString m_defaultSuperclass; QString m_qualifiedCppName; - QString m_targetLangName; uint m_polymorphicBase : 1; uint m_genericClass : 1; uint m_deleteInMainThread : 1; QString m_polymorphicIdValue; - QString m_lookupName; QString m_targetType; TypeFlags m_typeFlags; CopyableFlag m_copyableFlag = Unknown; @@ -1432,9 +1401,10 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(ComplexTypeEntry::TypeFlags) class TypedefEntry : public ComplexTypeEntry { public: - explicit TypedefEntry(const QString &name, + explicit TypedefEntry(const QString &entryName, const QString &sourceType, - const QVersionNumber &vr); + const QVersionNumber &vr, + const TypeEntry *parent); QString sourceType() const { return m_sourceType; } void setSourceType(const QString &s) { m_sourceType =s; } @@ -1463,7 +1433,7 @@ class ContainerTypeEntry : public ComplexTypeEntry { Q_GADGET public: - enum Type { + enum ContainerKind { NoContainer, ListContainer, StringListContainer, @@ -1478,17 +1448,17 @@ public: MultiHashContainer, PairContainer, }; - Q_ENUM(Type) + Q_ENUM(ContainerKind) - explicit ContainerTypeEntry(const QString &name, Type type, const QVersionNumber &vr); + explicit ContainerTypeEntry(const QString &entryName, ContainerKind containerKind, + const QVersionNumber &vr, const TypeEntry *parent); - Type type() const + ContainerKind containerKind() const { - return m_type; + return m_containerKind; } QString typeName() const; - QString targetLangName() const override; QString qualifiedCppName() const override; TypeEntry *clone() const override; @@ -1500,17 +1470,20 @@ protected: ContainerTypeEntry(const ContainerTypeEntry &); private: - Type m_type; + ContainerKind m_containerKind; }; class SmartPointerTypeEntry : public ComplexTypeEntry { public: - explicit SmartPointerTypeEntry(const QString &name, + using Instantiations = QVector<const TypeEntry *>; + + explicit SmartPointerTypeEntry(const QString &entryName, const QString &getterName, const QString &smartPointerType, const QString &refCountMethodName, - const QVersionNumber &vr); + const QVersionNumber &vr, + const TypeEntry *parent); QString getter() const { @@ -1524,6 +1497,13 @@ public: TypeEntry *clone() const override; + Instantiations instantiations() const { return m_instantiations; } + void setInstantiations(const Instantiations &i) { m_instantiations = i; } + bool matchesInstantiation(const TypeEntry *e) const; + +#ifndef QT_NO_DEBUG_STREAM + void formatDebug(QDebug &d) const override; +#endif protected: SmartPointerTypeEntry(const SmartPointerTypeEntry &); @@ -1531,12 +1511,14 @@ private: QString m_getterName; QString m_smartPointerType; QString m_refCountMethodName; + Instantiations m_instantiations; }; class NamespaceTypeEntry : public ComplexTypeEntry { public: - explicit NamespaceTypeEntry(const QString &name, const QVersionNumber &vr); + explicit NamespaceTypeEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); TypeEntry *clone() const override; @@ -1550,72 +1532,56 @@ public: bool matchesFile(const QString &needle) const; + bool isVisible() const; + void setVisibility(TypeSystem::Visibility v) { m_visibility = v; } + + // C++ 11 inline namespace, from code model + bool isInlineNamespace() const { return m_inlineNamespace; } + void setInlineNamespace(bool i) { m_inlineNamespace = i; } + + static bool isVisibleScope(const TypeEntry *e); + #ifndef QT_NO_DEBUG_STREAM void formatDebug(QDebug &d) const override; #endif + bool generateUsing() const { return m_generateUsing; } + void setGenerateUsing(bool generateUsing) { m_generateUsing = generateUsing; } + protected: NamespaceTypeEntry(const NamespaceTypeEntry &); private: QRegularExpression m_filePattern; const NamespaceTypeEntry *m_extends = nullptr; + TypeSystem::Visibility m_visibility = TypeSystem::Visibility::Auto; bool m_hasPattern = false; + bool m_inlineNamespace = false; + bool m_generateUsing = true; // Whether to generate "using namespace" into wrapper }; class ValueTypeEntry : public ComplexTypeEntry { public: - explicit ValueTypeEntry(const QString &name, const QVersionNumber &vr); + explicit ValueTypeEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); bool isValue() const override; - bool isNativeIdBased() const override; - TypeEntry *clone() const override; protected: - explicit ValueTypeEntry(const QString &name, Type t, const QVersionNumber &vr); + explicit ValueTypeEntry(const QString &entryName, Type t, const QVersionNumber &vr, + const TypeEntry *parent); ValueTypeEntry(const ValueTypeEntry &); }; -class InterfaceTypeEntry : public ComplexTypeEntry -{ -public: - explicit InterfaceTypeEntry(const QString &name, const QVersionNumber &vr); - - static QString interfaceName(const QString &name) - { - return name + QLatin1String("Interface"); - } - - ObjectTypeEntry *origin() const - { - return m_origin; - } - void setOrigin(ObjectTypeEntry *origin) - { - m_origin = origin; - } - - bool isNativeIdBased() const override; - QString qualifiedCppName() const override; - - TypeEntry *clone() const override; - -protected: - InterfaceTypeEntry(const InterfaceTypeEntry &); - -private: - ObjectTypeEntry *m_origin = nullptr; -}; - - class FunctionTypeEntry : public TypeEntry { public: explicit FunctionTypeEntry(const QString& name, const QString& signature, - const QVersionNumber &vr); + const QVersionNumber &vr, + const TypeEntry *parent); void addSignature(const QString& signature) { m_signatures << signature; @@ -1643,23 +1609,13 @@ private: class ObjectTypeEntry : public ComplexTypeEntry { public: - explicit ObjectTypeEntry(const QString &name, const QVersionNumber &vr); - - InterfaceTypeEntry *designatedInterface() const override; - void setDesignatedInterface(InterfaceTypeEntry *entry) - { - m_interface = entry; - } - - bool isNativeIdBased() const override; + explicit ObjectTypeEntry(const QString &entryName, const QVersionNumber &vr, + const TypeEntry *parent); TypeEntry *clone() const override; protected: ObjectTypeEntry(const ObjectTypeEntry &); - -private: - InterfaceTypeEntry *m_interface = nullptr; }; struct TypeRejection |