diff options
Diffstat (limited to 'sources/shiboken2/generator/shiboken2/headergenerator.cpp')
-rw-r--r-- | sources/shiboken2/generator/shiboken2/headergenerator.cpp | 106 |
1 files changed, 49 insertions, 57 deletions
diff --git a/sources/shiboken2/generator/shiboken2/headergenerator.cpp b/sources/shiboken2/generator/shiboken2/headergenerator.cpp index 8fde3cd31..d240cacbf 100644 --- a/sources/shiboken2/generator/shiboken2/headergenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/headergenerator.cpp @@ -49,11 +49,10 @@ QString HeaderGenerator::fileNameForContext(GeneratorContext &context) const QString fileNameBase = metaClass->qualifiedCppName().toLower(); fileNameBase.replace(QLatin1String("::"), QLatin1String("_")); return fileNameBase + fileNameSuffix(); - } else { - const AbstractMetaType *smartPointerType = context.preciseType(); - QString fileNameBase = getFileNameBaseForSmartPointer(smartPointerType, metaClass); - return fileNameBase + fileNameSuffix(); } + const AbstractMetaType *smartPointerType = context.preciseType(); + QString fileNameBase = getFileNameBaseForSmartPointer(smartPointerType, metaClass); + return fileNameBase + fileNameSuffix(); } void HeaderGenerator::writeCopyCtor(QTextStream& s, const AbstractMetaClass* metaClass) const @@ -116,8 +115,6 @@ void HeaderGenerator::generateClass(QTextStream &s, GeneratorContext &classConte if (!avoidProtectedHack()) s << "#define protected public" << endl << endl; - s << "#include <shiboken.h>" << endl << endl; - //Includes s << metaClass->typeEntry()->include() << endl; @@ -173,7 +170,7 @@ void HeaderGenerator::generateClass(QTextStream &s, GeneratorContext &classConte s << INDENT << "void* qt_metacast(const char* _clname) override;" << endl; } - if (m_inheritedOverloads.size()) { + if (!m_inheritedOverloads.isEmpty()) { s << INDENT << "// Inherited overloads, because the using keyword sux" << endl; writeInheritedOverloads(s); m_inheritedOverloads.clear(); @@ -230,7 +227,7 @@ void HeaderGenerator::writeFunction(QTextStream& s, const AbstractMetaFunction* QString argName = arg->name(); const TypeEntry* enumTypeEntry = 0; if (arg->type()->isFlags()) - enumTypeEntry = reinterpret_cast<const FlagsTypeEntry*>(arg->type()->typeEntry())->originator(); + enumTypeEntry = static_cast<const FlagsTypeEntry*>(arg->type()->typeEntry())->originator(); else if (arg->type()->isEnum()) enumTypeEntry = arg->type()->typeEntry(); if (enumTypeEntry) @@ -284,46 +281,56 @@ void HeaderGenerator::writeFunction(QTextStream& s, const AbstractMetaFunction* } } -static void _writeTypeIndexDefineLine(QTextStream& s, const QString& variableName, int typeIndex) +static void _writeTypeIndexValue(QTextStream& s, const QString& variableName, + int typeIndex) { - s << "#define "; - s.setFieldWidth(60); + s << " "; + s.setFieldWidth(56); s << variableName; s.setFieldWidth(0); - s << ' ' << typeIndex << endl; + s << " = " << typeIndex; } -void HeaderGenerator::writeTypeIndexDefineLine(QTextStream& s, const TypeEntry* typeEntry) + +static inline void _writeTypeIndexValueLine(QTextStream& s, + const QString& variableName, + int typeIndex) +{ + _writeTypeIndexValue(s, variableName, typeIndex); + s << ",\n"; +} + +void HeaderGenerator::writeTypeIndexValueLine(QTextStream& s, const TypeEntry* typeEntry) { if (!typeEntry || !typeEntry->generateCode()) return; s.setFieldAlignment(QTextStream::AlignLeft); - int typeIndex = getTypeIndex(typeEntry); - _writeTypeIndexDefineLine(s, getTypeIndexVariableName(typeEntry), typeIndex); + const int typeIndex = typeEntry->sbkIndex(); + _writeTypeIndexValueLine(s, getTypeIndexVariableName(typeEntry), typeIndex); if (typeEntry->isComplex()) { - const ComplexTypeEntry* cType = reinterpret_cast<const ComplexTypeEntry*>(typeEntry); + const ComplexTypeEntry* cType = static_cast<const ComplexTypeEntry*>(typeEntry); if (cType->baseContainerType()) { const AbstractMetaClass *metaClass = AbstractMetaClass::findClass(classes(), cType); if (metaClass->templateBaseClass()) - _writeTypeIndexDefineLine(s, getTypeIndexVariableName(metaClass, true), typeIndex); + _writeTypeIndexValueLine(s, getTypeIndexVariableName(metaClass, true), typeIndex); } } if (typeEntry->isEnum()) { - const EnumTypeEntry* ete = reinterpret_cast<const EnumTypeEntry*>(typeEntry); + const EnumTypeEntry* ete = static_cast<const EnumTypeEntry*>(typeEntry); if (ete->flags()) - writeTypeIndexDefineLine(s, ete->flags()); + writeTypeIndexValueLine(s, ete->flags()); } } -void HeaderGenerator::writeTypeIndexDefine(QTextStream& s, const AbstractMetaClass* metaClass) +void HeaderGenerator::writeTypeIndexValueLines(QTextStream& s, const AbstractMetaClass* metaClass) { if (!metaClass->typeEntry()->generateCode()) return; - writeTypeIndexDefineLine(s, metaClass->typeEntry()); + writeTypeIndexValueLine(s, metaClass->typeEntry()); const AbstractMetaEnumList &enums = metaClass->enums(); for (const AbstractMetaEnum *metaEnum : enums) { if (metaEnum->isPrivate()) continue; - writeTypeIndexDefineLine(s, metaEnum->typeEntry()); + writeTypeIndexValueLine(s, metaEnum->typeEntry()); } } @@ -342,47 +349,44 @@ bool HeaderGenerator::finishGeneration() Indentation indent(INDENT); - macrosStream << "// Type indices" << endl; + macrosStream << "// Type indices\nenum : int {\n"; AbstractMetaEnumList globalEnums = this->globalEnums(); const AbstractMetaClassList &classList = classes(); for (const AbstractMetaClass *metaClass : classList) { - writeTypeIndexDefine(macrosStream, metaClass); + writeTypeIndexValueLines(macrosStream, metaClass); lookForEnumsInClassesNotToBeGenerated(globalEnums, metaClass); } for (const AbstractMetaEnum *metaEnum : qAsConst(globalEnums)) - writeTypeIndexDefineLine(macrosStream, metaEnum->typeEntry()); + writeTypeIndexValueLine(macrosStream, metaEnum->typeEntry()); // Write the smart pointer define indexes. int smartPointerCountIndex = getMaxTypeIndex(); int smartPointerCount = 0; const QVector<const AbstractMetaType *> &instantiatedSmartPtrs = instantiatedSmartPointers(); for (const AbstractMetaType *metaType : instantiatedSmartPtrs) { - QString variableName = getTypeIndexVariableName(metaType); - macrosStream << "#define "; - macrosStream.setFieldWidth(60); - macrosStream << variableName; - macrosStream.setFieldWidth(0); - macrosStream << ' ' << smartPointerCountIndex << " // " << metaType->cppSignature() - << endl; + _writeTypeIndexValue(macrosStream, getTypeIndexVariableName(metaType), + smartPointerCountIndex); + macrosStream << ", // " << metaType->cppSignature() << endl; ++smartPointerCountIndex; ++smartPointerCount; } + _writeTypeIndexValue(macrosStream, + QLatin1String("SBK_") + moduleName() + QLatin1String("_IDX_COUNT"), + getMaxTypeIndex() + smartPointerCount); + macrosStream << "\n};\n"; - macrosStream << "#define "; - macrosStream.setFieldWidth(60); - macrosStream << QLatin1String("SBK_") + moduleName() + QLatin1String("_IDX_COUNT"); - macrosStream.setFieldWidth(0); - macrosStream << ' ' << getMaxTypeIndex() + smartPointerCount << endl << endl; macrosStream << "// This variable stores all Python types exported by this module." << endl; macrosStream << "extern PyTypeObject** " << cppApiVariableName() << ';' << endl << endl; + macrosStream << "// This variable stores the Python module object exported by this module." << endl; + macrosStream << "extern PyObject* " << pythonModuleObjectName() << ';' << endl << endl; macrosStream << "// This variable stores all type converters exported by this module." << endl; macrosStream << "extern SbkConverter** " << convertersVariableName() << ';' << endl << endl; // TODO-CONVERTER ------------------------------------------------------------------------------ // Using a counter would not do, a fix must be made to APIExtractor's getTypeIndex(). - macrosStream << "// Converter indices" << endl; + macrosStream << "// Converter indices\nenum : int {\n"; const PrimitiveTypeEntryList &primitives = primitiveTypes(); int pCount = 0; for (const PrimitiveTypeEntry *ptype : primitives) { @@ -393,28 +397,22 @@ bool HeaderGenerator::finishGeneration() if (!ptype->generateCode() || !ptype->customConversion()) continue; - _writeTypeIndexDefineLine(macrosStream, getTypeIndexVariableName(ptype), pCount++); + _writeTypeIndexValueLine(macrosStream, getTypeIndexVariableName(ptype), pCount++); } const QVector<const AbstractMetaType *> &containers = instantiatedContainers(); for (const AbstractMetaType *container : containers) { - //_writeTypeIndexDefineLine(macrosStream, getTypeIndexVariableName(container), pCount); - // DEBUG - QString variableName = getTypeIndexVariableName(container); - macrosStream << "#define "; - macrosStream.setFieldWidth(60); - macrosStream << variableName; - macrosStream.setFieldWidth(0); - macrosStream << ' ' << pCount << " // " << container->cppSignature() << endl; - // DEBUG + _writeTypeIndexValue(macrosStream, getTypeIndexVariableName(container), pCount); + macrosStream << ", // " << container->cppSignature() << endl; pCount++; } // Because on win32 the compiler will not accept a zero length array. if (pCount == 0) pCount++; - _writeTypeIndexDefineLine(macrosStream, QStringLiteral("SBK_%1_CONVERTERS_IDX_COUNT").arg(moduleName()), pCount); - macrosStream << endl; + _writeTypeIndexValue(macrosStream, QStringLiteral("SBK_%1_CONVERTERS_IDX_COUNT") + .arg(moduleName()), pCount); + macrosStream << "\n};\n"; // TODO-CONVERTER ------------------------------------------------------------------------------ macrosStream << "// Macros for type check" << endl; @@ -474,12 +472,6 @@ bool HeaderGenerator::finishGeneration() s << "#include <sbkpython.h>" << endl; s << "#include <sbkconverter.h>" << endl; - s << "#include <sbkenum.h>" << endl; - s << "#include <basewrapper.h>" << endl; - s << "#include <bindingmanager.h>" << endl; - s << "#include <memory>" << endl << endl; - if (usePySideExtensions()) - s << "#include <pysidesignal.h>" << endl; QStringList requiredTargetImports = TypeDatabase::instance()->requiredTargetImports(); if (!requiredTargetImports.isEmpty()) { @@ -580,7 +572,7 @@ void HeaderGenerator::writeInheritedOverloads(QTextStream& s) QString argName = arg->name(); const TypeEntry* enumTypeEntry = 0; if (arg->type()->isFlags()) - enumTypeEntry = reinterpret_cast<const FlagsTypeEntry*>(arg->type()->typeEntry())->originator(); + enumTypeEntry = static_cast<const FlagsTypeEntry*>(arg->type()->typeEntry())->originator(); else if (arg->type()->isEnum()) enumTypeEntry = arg->type()->typeEntry(); if (enumTypeEntry) |