From 84becad3db953a692eb4c1e49531b1fc5d04812f Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 13 Oct 2020 15:33:34 +0200 Subject: shiboken2: Some Refactorings - AbstractMetaType: Pass TypeEntry to constructor - AbstractMetaType: Return instantiations() by const ref - AbstractMetaClass: Make baseTemplateInstantiations a member instead of a global hash. Simplify the code accordingly. Task-number: PYSIDE-1202 Change-Id: I1a18aa4ff97481af8cb13e8732fe3952c48edb29 Reviewed-by: Cristian Maureira-Fredes --- .../shiboken2/ApiExtractor/abstractmetabuilder.cpp | 12 +++---- .../shiboken2/ApiExtractor/abstractmetalang.cpp | 41 ++++++++-------------- sources/shiboken2/ApiExtractor/abstractmetalang.h | 13 +++---- sources/shiboken2/generator/generator.cpp | 5 ++- .../shiboken2/generator/shiboken2/cppgenerator.cpp | 7 ++-- .../shiboken2/generator/shiboken2/overloaddata.cpp | 12 +++---- .../generator/shiboken2/shibokengenerator.cpp | 9 ++--- 7 files changed, 37 insertions(+), 62 deletions(-) (limited to 'sources') diff --git a/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp b/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp index b06376866..a2e2841df 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp +++ b/sources/shiboken2/ApiExtractor/abstractmetabuilder.cpp @@ -1258,9 +1258,7 @@ void AbstractMetaBuilderPrivate::fixReturnTypeOfConversionOperator(AbstractMetaF if (!retType) return; - auto *metaType = new AbstractMetaType; - metaType->setTypeEntry(retType); - metaFunction->replaceType(metaType); + metaFunction->replaceType(new AbstractMetaType(retType)); } AbstractMetaFunctionList AbstractMetaBuilderPrivate::classFunctionList(const ScopeModelItem &scopeItem, @@ -2010,8 +2008,7 @@ AbstractMetaType *AbstractMetaBuilderPrivate::translateType(const AddedFunction: } // These are only implicit and should not appear in code... - auto *metaType = new AbstractMetaType; - metaType->setTypeEntry(type); + auto *metaType = new AbstractMetaType(type); metaType->setIndirections(typeInfo.indirections); if (typeInfo.isReference) metaType->setReferenceType(LValueReference); @@ -2611,7 +2608,7 @@ bool AbstractMetaBuilderPrivate::inheritTemplate(AbstractMetaClass *subclass, const TypeInfo &info) { QVector targs = info.instantiations(); - QVector templateTypes; + AbstractMetaTypeList templateTypes; QString errorMessage; if (subclass->isTypeDef()) { @@ -2654,8 +2651,7 @@ bool AbstractMetaBuilderPrivate::inheritTemplate(AbstractMetaClass *subclass, } if (t) { - auto *temporaryType = new AbstractMetaType; - temporaryType->setTypeEntry(t); + auto *temporaryType = new AbstractMetaType(t); temporaryType->setConstant(i.isConstant()); temporaryType->setReferenceType(i.referenceType()); temporaryType->setIndirectionsV(i.indirectionsV()); diff --git a/sources/shiboken2/ApiExtractor/abstractmetalang.cpp b/sources/shiboken2/ApiExtractor/abstractmetalang.cpp index 9c54cc0d4..458e54626 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetalang.cpp +++ b/sources/shiboken2/ApiExtractor/abstractmetalang.cpp @@ -186,7 +186,8 @@ void AbstractMetaAttributes::assignMetaAttributes(const AbstractMetaAttributes & * AbstractMetaType */ -AbstractMetaType::AbstractMetaType() : +AbstractMetaType::AbstractMetaType(const TypeEntry *t) : + m_typeEntry(t), m_constant(false), m_volatile(false), m_cppInstantiation(true), @@ -217,7 +218,7 @@ QString AbstractMetaType::fullName() const AbstractMetaType *AbstractMetaType::copy() const { - auto *cpy = new AbstractMetaType; + auto *cpy = new AbstractMetaType(typeEntry()); cpy->setTypeUsagePattern(typeUsagePattern()); cpy->setConstant(isConstant()); @@ -231,8 +232,6 @@ AbstractMetaType *AbstractMetaType::copy() const cpy->setArrayElementType(arrayElementType() ? arrayElementType()->copy() : nullptr); - cpy->setTypeEntry(typeEntry()); - return cpy; } @@ -417,8 +416,7 @@ AbstractMetaType *AbstractMetaType::createVoid() { static const TypeEntry *voidTypeEntry = TypeDatabase::instance()->findType(QLatin1String("void")); Q_ASSERT(voidTypeEntry); - auto *metaType = new AbstractMetaType; - metaType->setTypeEntry(voidTypeEntry); + auto *metaType = new AbstractMetaType(voidTypeEntry); metaType->decideUsagePattern(); return metaType; } @@ -1418,8 +1416,7 @@ AbstractMetaClass::~AbstractMetaClass() qDeleteAll(m_fields); qDeleteAll(m_enums); qDeleteAll(m_propertySpecs); - if (hasTemplateBaseClassInstantiations()) - qDeleteAll(templateBaseClassInstantiations()); + qDeleteAll(m_baseTemplateInstantiations); } /******************************************************************************* @@ -1745,28 +1742,20 @@ QPropertySpec *AbstractMetaClass::propertySpecForReset(const QString &name) cons return nullptr; } -using AbstractMetaClassBaseTemplateInstantiationsMap = QHash; -Q_GLOBAL_STATIC(AbstractMetaClassBaseTemplateInstantiationsMap, metaClassBaseTemplateInstantiations); - bool AbstractMetaClass::hasTemplateBaseClassInstantiations() const { - if (!templateBaseClass()) - return false; - return metaClassBaseTemplateInstantiations()->contains(this); + return m_templateBaseClass != nullptr && !m_baseTemplateInstantiations.isEmpty(); } -AbstractMetaTypeList AbstractMetaClass::templateBaseClassInstantiations() const +const AbstractMetaTypeList &AbstractMetaClass::templateBaseClassInstantiations() const { - if (!templateBaseClass()) - return AbstractMetaTypeList(); - return metaClassBaseTemplateInstantiations()->value(this); + return m_baseTemplateInstantiations; } -void AbstractMetaClass::setTemplateBaseClassInstantiations(AbstractMetaTypeList &instantiations) +void AbstractMetaClass::setTemplateBaseClassInstantiations(const AbstractMetaTypeList &instantiations) { - if (!templateBaseClass()) - return; - metaClassBaseTemplateInstantiations()->insert(this, instantiations); + Q_ASSERT(m_templateBaseClass != nullptr); + m_baseTemplateInstantiations = instantiations; } // Does any of the base classes require deletion in the main thread? @@ -1965,8 +1954,7 @@ void AbstractMetaClass::addDefaultCopyConstructor(bool isPrivate) f->setFunctionType(AbstractMetaFunction::CopyConstructorFunction); f->setDeclaringClass(this); - auto argType = new AbstractMetaType; - argType->setTypeEntry(typeEntry()); + auto argType = new AbstractMetaType(typeEntry()); argType->setReferenceType(LValueReference); argType->setConstant(true); argType->setTypeUsagePattern(AbstractMetaType::ValuePattern); @@ -2195,8 +2183,7 @@ static void addExtraIncludeForType(AbstractMetaClass *metaClass, const AbstractM } if (type->hasInstantiations()) { - const AbstractMetaTypeList &instantiations = type->instantiations(); - for (const AbstractMetaType *instantiation : instantiations) + for (const AbstractMetaType *instantiation : type->instantiations()) addExtraIncludeForType(metaClass, instantiation); } } @@ -2641,7 +2628,7 @@ void AbstractMetaClass::format(QDebug &d) const d << " \"" << b->name() << '"'; } if (auto templateBase = templateBaseClass()) { - const auto instantiatedTypes = templateBaseClassInstantiations(); + const auto &instantiatedTypes = templateBaseClassInstantiations(); d << ", instantiates \"" << templateBase->name(); for (int i = 0, count = instantiatedTypes.size(); i < count; ++i) d << (i ? ',' : '<') << instantiatedTypes.at(i)->name(); diff --git a/sources/shiboken2/ApiExtractor/abstractmetalang.h b/sources/shiboken2/ApiExtractor/abstractmetalang.h index 5d075f1e8..988815dd3 100644 --- a/sources/shiboken2/ApiExtractor/abstractmetalang.h +++ b/sources/shiboken2/ApiExtractor/abstractmetalang.h @@ -303,7 +303,7 @@ public: }; Q_DECLARE_FLAGS(ComparisonFlags, ComparisonFlag); - AbstractMetaType(); + explicit AbstractMetaType(const TypeEntry *t = nullptr); ~AbstractMetaType(); QString package() const; @@ -341,7 +341,7 @@ public: } } - AbstractMetaTypeList instantiations() const + const AbstractMetaTypeList &instantiations() const { return m_instantiations; } @@ -512,7 +512,7 @@ public: AbstractMetaType *getSmartPointerInnerType() const { Q_ASSERT(isSmartPointer()); - AbstractMetaTypeList instantiations = this->instantiations(); + const AbstractMetaTypeList &instantiations = this->instantiations(); Q_ASSERT(!instantiations.isEmpty()); AbstractMetaType *innerType = instantiations.at(0); return innerType; @@ -540,7 +540,7 @@ private: QString formatSignature(bool minimal) const; QString formatPythonSignature() const; - const TypeEntry *m_typeEntry = nullptr; + const TypeEntry *m_typeEntry; AbstractMetaTypeList m_instantiations; QString m_package; mutable QString m_cachedCppSignature; @@ -1640,8 +1640,8 @@ public: } bool hasTemplateBaseClassInstantiations() const; - AbstractMetaTypeList templateBaseClassInstantiations() const; - void setTemplateBaseClassInstantiations(AbstractMetaTypeList& instantiations); + const AbstractMetaTypeList &templateBaseClassInstantiations() const; + void setTemplateBaseClassInstantiations(const AbstractMetaTypeList& instantiations); void setTypeDef(bool typeDef) { m_isTypeDef = typeDef; } bool isTypeDef() const { return m_isTypeDef; } @@ -1712,6 +1712,7 @@ private: const AbstractMetaClass *m_enclosingClass = nullptr; AbstractMetaClassList m_baseClasses; // Real base classes after setting up inheritance + AbstractMetaTypeList m_baseTemplateInstantiations; AbstractMetaClass *m_extendedNamespace = nullptr; const AbstractMetaClass *m_templateBaseClass = nullptr; diff --git a/sources/shiboken2/generator/generator.cpp b/sources/shiboken2/generator/generator.cpp index c0a2af2ee..dd56ab7cd 100644 --- a/sources/shiboken2/generator/generator.cpp +++ b/sources/shiboken2/generator/generator.cpp @@ -229,7 +229,7 @@ QString Generator::getSimplifiedContainerTypeName(const AbstractMetaType *type) // Strip a "const QSharedPtr &" or similar to "QSharedPtr" (PYSIDE-1016/454) const AbstractMetaType *canonicalSmartPtrInstantiation(const AbstractMetaType *type) { - AbstractMetaTypeList instantiations = type->instantiations(); + const AbstractMetaTypeList &instantiations = type->instantiations(); Q_ASSERT(instantiations.size() == 1); const bool needsFix = type->isConstant() || type->referenceType() != NoReference; const bool pointeeNeedsFix = instantiations.constFirst()->isConstant(); @@ -256,8 +256,7 @@ void Generator::addInstantiatedContainersAndSmartPointers(const AbstractMetaType { if (!type) return; - const AbstractMetaTypeList &instantiations = type->instantiations(); - for (const AbstractMetaType *t : instantiations) + for (const auto *t : type->instantiations()) addInstantiatedContainersAndSmartPointers(t, context); const auto typeEntry = type->typeEntry(); const bool isContainer = typeEntry->isContainer(); diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index ea970331e..906e82c93 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -85,10 +85,9 @@ static const char *typeNameOf(const T &t) inline AbstractMetaType *getTypeWithoutContainer(AbstractMetaType *arg) { if (arg && arg->typeEntry()->isContainer()) { - AbstractMetaTypeList lst = arg->instantiations(); // only support containers with 1 type - if (lst.size() == 1) - return lst[0]; + if (arg->instantiations().size() == 1) + return arg->instantiations().constFirst(); } return arg; } @@ -6265,7 +6264,7 @@ void CppGenerator::writeDefaultSequenceMethods(QTextStream &s, const GeneratorCo << CPP_SELF_VAR << "->begin();\n" << INDENT << "std::advance(_item, _i);\n"; - const AbstractMetaTypeList instantiations = metaClass->templateBaseClassInstantiations(); + const AbstractMetaTypeList &instantiations = metaClass->templateBaseClassInstantiations(); if (instantiations.isEmpty()) { qFatal("shiboken: %s: Internal error, no instantiations of \"%s\" were found.", __FUNCTION__, qPrintable(metaClass->qualifiedCppName())); diff --git a/sources/shiboken2/generator/shiboken2/overloaddata.cpp b/sources/shiboken2/generator/shiboken2/overloaddata.cpp index 9f79060da..193384853 100644 --- a/sources/shiboken2/generator/shiboken2/overloaddata.cpp +++ b/sources/shiboken2/generator/shiboken2/overloaddata.cpp @@ -57,8 +57,7 @@ static QString getTypeName(const AbstractMetaType *type) QString typeName = typeEntry->name(); if (typeEntry->isContainer()) { QStringList types; - const AbstractMetaTypeList &instantiations = type->instantiations(); - for (const AbstractMetaType *cType : instantiations) { + for (const auto *cType : type->instantiations()) { const TypeEntry *typeEntry = getReferencedTypeEntry(cType->typeEntry()); types << typeEntry->name(); } @@ -147,8 +146,7 @@ static QString getImplicitConversionTypeName(const AbstractMetaType *containerTy impConv = getTypeName(function->arguments().constFirst()->type()); QStringList types; - const AbstractMetaTypeList &instantiations = containerType->instantiations(); - for (const AbstractMetaType *otherType : instantiations) + for (const auto *otherType : containerType->instantiations()) types << (otherType == instantiation ? impConv : getTypeName(otherType)); return containerType->typeEntry()->qualifiedCppName() + QLatin1Char('<') @@ -258,8 +256,7 @@ void OverloadData::sortNextOverloads() qstringIndex = sortData.lastProcessedItemId(); } - const AbstractMetaTypeList &instantiations = ov->argType()->instantiations(); - for (const AbstractMetaType *instantiation : instantiations) { + for (const auto *instantiation : ov->argType()->instantiations()) { // Add dependencies for type instantiation of container. QString typeName = getTypeName(instantiation); sortData.mapType(typeName); @@ -346,8 +343,7 @@ void OverloadData::sortNextOverloads() } // Process template instantiations - const AbstractMetaTypeList &instantiations = targetType->instantiations(); - for (const AbstractMetaType *instantiation : instantiations) { + for (const auto *instantiation : targetType->instantiations()) { if (sortData.map.contains(getTypeName(instantiation))) { int convertible = sortData.map[getTypeName(instantiation)]; diff --git a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp index 3cd6b8e53..4ff3e8408 100644 --- a/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/shibokengenerator.cpp @@ -2406,8 +2406,7 @@ AbstractMetaType *ShibokenGenerator::buildAbstractMetaTypeFromTypeEntry(const Ty typeName.remove(0, 2); if (m_metaTypeFromStringCache.contains(typeName)) return m_metaTypeFromStringCache.value(typeName); - auto *metaType = new AbstractMetaType; - metaType->setTypeEntry(typeEntry); + auto *metaType = new AbstractMetaType(typeEntry); metaType->clearIndirections(); metaType->setReferenceType(NoReference); metaType->setConstant(false); @@ -2737,8 +2736,7 @@ QString ShibokenGenerator::convertersVariableName(const QString &moduleName) con static QString processInstantiationsVariableName(const AbstractMetaType *type) { QString res = QLatin1Char('_') + _fixedCppTypeName(type->typeEntry()->qualifiedCppName()).toUpper(); - const AbstractMetaTypeList &instantiations = type->instantiations(); - for (const AbstractMetaType *instantiation : instantiations) { + for (const auto *instantiation : type->instantiations()) { res += instantiation->isContainer() ? processInstantiationsVariableName(instantiation) : QLatin1Char('_') + _fixedCppTypeName(instantiation->cppSignature()).toUpper(); @@ -2762,8 +2760,7 @@ QString ShibokenGenerator::getTypeIndexVariableName(const AbstractMetaClass *met return QString(); QString result = QLatin1String("SBK_") + _fixedCppTypeName(templateBaseClass->typeEntry()->qualifiedCppName()).toUpper(); - const AbstractMetaTypeList &templateBaseClassInstantiations = metaClass->templateBaseClassInstantiations(); - for (const AbstractMetaType *instantiation : templateBaseClassInstantiations) + for (const auto *instantiation : metaClass->templateBaseClassInstantiations()) result += processInstantiationsVariableName(instantiation); appendIndexSuffix(&result); return result; -- cgit v1.2.3