diff options
Diffstat (limited to 'sources/shiboken6/generator/shiboken/overloaddata.cpp')
-rw-r--r-- | sources/shiboken6/generator/shiboken/overloaddata.cpp | 134 |
1 files changed, 72 insertions, 62 deletions
diff --git a/sources/shiboken6/generator/shiboken/overloaddata.cpp b/sources/shiboken6/generator/shiboken/overloaddata.cpp index dff5628bf..c28fcdc1a 100644 --- a/sources/shiboken6/generator/shiboken/overloaddata.cpp +++ b/sources/shiboken6/generator/shiboken/overloaddata.cpp @@ -16,7 +16,6 @@ #include "pytypenames.h" #include "textstream.h" #include "exception.h" -#include "messages.h" #include "qtcompat.h" @@ -31,16 +30,16 @@ using namespace Qt::StringLiterals; static QString getTypeName(const AbstractMetaType &type) { - const TypeEntry *typeEntry = type.typeEntry(); + TypeEntryCPtr typeEntry = type.typeEntry(); if (typeEntry->isPrimitive()) - typeEntry = typeEntry->asPrimitive()->basicReferencedTypeEntry(); + typeEntry = basicReferencedTypeEntry(typeEntry); QString typeName = typeEntry->name(); if (typeEntry->isContainer()) { QStringList types; for (const auto &cType : type.instantiations()) { - const TypeEntry *typeEntry = cType.typeEntry(); + TypeEntryCPtr typeEntry = cType.typeEntry(); if (typeEntry->isPrimitive()) - typeEntry = typeEntry->asPrimitive()->basicReferencedTypeEntry(); + typeEntry = basicReferencedTypeEntry(typeEntry); types << typeEntry->name(); } typeName += u'<' + types.join(u',') + u" >"_s; @@ -55,7 +54,7 @@ static bool typesAreEqual(const AbstractMetaType &typeA, const AbstractMetaType if (typeA.instantiations().size() != typeB.instantiations().size()) return false; - for (int i = 0; i < typeA.instantiations().size(); ++i) { + for (qsizetype i = 0; i < typeA.instantiations().size(); ++i) { if (!typesAreEqual(typeA.instantiations().at(i), typeB.instantiations().at(i))) return false; } @@ -126,6 +125,7 @@ using OverloadGraph = Graph<QString>; void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) { QHash<QString, OverloadDataList> typeToOverloads; + using Edge = std::pair<QString, QString>; bool checkPyObject = false; bool checkPySequence = false; @@ -135,13 +135,13 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) // Primitive types that are not int, long, short, // char and their respective unsigned counterparts. - static const QStringList nonIntegerPrimitives{floatT(), doubleT(), boolT()}; + static const QStringList nonIntegerPrimitives{floatT, doubleT, boolT}; // Signed integer primitive types. - static const QStringList signedIntegerPrimitives{intT(), shortT(), longT(), longLongT()}; + static const QStringList signedIntegerPrimitives{intT, shortT, longT, longLongT}; // sort the children overloads - for (const auto &ov : qAsConst(m_children)) + for (const auto &ov : std::as_const(m_children)) ov->sortNextOverloads(api); if (m_children.size() <= 1 || sortByOverloadNumberModification(m_children)) @@ -152,7 +152,7 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) // with graph sorting using integers. OverloadGraph graph; - for (const auto &ov : qAsConst(m_children)) { + for (const auto &ov : std::as_const(m_children)) { const QString typeName = getTypeName(ov->modifiedArgType()); auto it = typeToOverloads.find(typeName); if (it == typeToOverloads.end()) { @@ -162,15 +162,15 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) it.value().append(ov); } - if (!checkPyObject && typeName == cPyObjectT()) + if (!checkPyObject && typeName == cPyObjectT) checkPyObject = true; - else if (!checkPySequence && typeName == cPySequenceT()) + else if (!checkPySequence && typeName == cPySequenceT) checkPySequence = true; - else if (!checkPyBuffer && typeName == cPyBufferT()) + else if (!checkPyBuffer && typeName == cPyBufferT) checkPyBuffer = true; - else if (!checkQVariant && typeName == qVariantT()) + else if (!checkQVariant && typeName == qVariantT) checkQVariant = true; - else if (!checkQString && typeName == qStringT()) + else if (!checkQString && typeName == qStringT) checkQString = true; for (const auto &instantiation : ov->argType().instantiations()) { @@ -183,7 +183,7 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) // as Point must come before the PointF instantiation, or else list<Point> will never // be called. In the case of primitive types, list<double> must come before list<int>. if (instantiation.isPrimitive() && (signedIntegerPrimitives.contains(instantiation.name()))) { - for (const QString &primitive : qAsConst(nonIntegerPrimitives)) + for (const QString &primitive : std::as_const(nonIntegerPrimitives)) graph.addNode(getImplicitConversionTypeName(ov->argType(), instantiation, nullptr, primitive)); } else { const auto &funcs = api.implicitConversions(instantiation); @@ -196,9 +196,9 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) // Create the graph of type dependencies based on implicit conversions. // All C++ primitive types, add any forgotten type AT THE END OF THIS LIST! - static const QStringList primitiveTypes{intT(), unsignedIntT(), longT(), unsignedLongT(), - shortT(), unsignedShortT(), boolT(), unsignedCharT(), charT(), floatT(), - doubleT(), constCharPtrT()}; + static const QStringList primitiveTypes{intT, unsignedIntT, longT, unsignedLongT, + shortT, unsignedShortT, boolT, unsignedCharT, charT, floatT, + doubleT, constCharPtrT}; QStringList foundPrimitiveTypeIds; for (const auto &p : primitiveTypes) { @@ -207,13 +207,13 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) } if (checkPySequence && checkPyObject) - graph.addEdge(cPySequenceT(), cPyObjectT()); + graph.addEdge(cPySequenceT, cPyObjectT); QStringList classesWithIntegerImplicitConversion; AbstractMetaFunctionCList involvedConversions; - for (const auto &ov : qAsConst(m_children)) { + for (const auto &ov : std::as_const(m_children)) { const AbstractMetaType &targetType = ov->argType(); const QString targetTypeEntryName = getTypeName(ov->modifiedArgType()); @@ -226,7 +226,7 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) else convertibleType = getTypeName(function->arguments().constFirst().type()); - if (convertibleType == intT() || convertibleType == unsignedIntT()) + if (convertibleType == intT || convertibleType == unsignedIntT) classesWithIntegerImplicitConversion << targetTypeEntryName; if (!graph.hasNode(convertibleType)) @@ -241,12 +241,12 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) // Process inheritance relationships if (targetType.isValue() || targetType.isObject()) { - auto *te = targetType.typeEntry(); + const auto te = targetType.typeEntry(); auto metaClass = AbstractMetaClass::findClass(api.classes(), te); if (!metaClass) throw Exception(msgArgumentClassNotFound(m_overloads.constFirst(), te)); const auto &ancestors = metaClass->allTypeSystemAncestors(); - for (const AbstractMetaClass *ancestor : ancestors) { + for (const auto &ancestor : ancestors) { QString ancestorTypeName = ancestor->typeEntry()->name(); if (!graph.hasNode(ancestorTypeName)) continue; @@ -263,7 +263,7 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) graph.addEdge(convertible, targetTypeEntryName); if (instantiation.isPrimitive() && (signedIntegerPrimitives.contains(instantiation.name()))) { - for (const QString &primitive : qAsConst(nonIntegerPrimitives)) { + for (const QString &primitive : std::as_const(nonIntegerPrimitives)) { QString convertibleTypeName = getImplicitConversionTypeName(ov->argType(), instantiation, nullptr, primitive); // Avoid cyclic dependency. @@ -288,28 +288,28 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) if ((checkPySequence || checkPyObject || checkPyBuffer) - && !targetTypeEntryName.contains(cPyObjectT()) - && !targetTypeEntryName.contains(cPyBufferT()) - && !targetTypeEntryName.contains(cPySequenceT())) { + && !targetTypeEntryName.contains(cPyObjectT) + && !targetTypeEntryName.contains(cPyBufferT) + && !targetTypeEntryName.contains(cPySequenceT)) { if (checkPySequence) { // PySequence will be checked after all more specific types, but before PyObject. - graph.addEdge(targetTypeEntryName, cPySequenceT()); + graph.addEdge(targetTypeEntryName, cPySequenceT); } else if (checkPyBuffer) { // PySequence will be checked after all more specific types, but before PyObject. - graph.addEdge(targetTypeEntryName, cPyBufferT()); + graph.addEdge(targetTypeEntryName, cPyBufferT); } else { // Add dependency on PyObject, so its check is the last one (too generic). - graph.addEdge(targetTypeEntryName, cPyObjectT()); + graph.addEdge(targetTypeEntryName, cPyObjectT); } - } else if (checkQVariant && targetTypeEntryName != qVariantT()) { - if (!graph.containsEdge(qVariantT(), targetTypeEntryName)) // Avoid cyclic dependency. - graph.addEdge(targetTypeEntryName, qVariantT()); + } else if (checkQVariant && targetTypeEntryName != qVariantT) { + if (!graph.containsEdge(qVariantT, targetTypeEntryName)) // Avoid cyclic dependency. + graph.addEdge(targetTypeEntryName, qVariantT); } else if (checkQString && ov->argType().isPointer() - && targetTypeEntryName != qStringT() - && targetTypeEntryName != qByteArrayT() - && (!checkPyObject || targetTypeEntryName != cPyObjectT())) { - if (!graph.containsEdge(qStringT(), targetTypeEntryName)) // Avoid cyclic dependency. - graph.addEdge(targetTypeEntryName, qStringT()); + && targetTypeEntryName != qStringT + && targetTypeEntryName != qByteArrayT + && (!checkPyObject || targetTypeEntryName != cPyObjectT)) { + if (!graph.containsEdge(qStringT, targetTypeEntryName)) // Avoid cyclic dependency. + graph.addEdge(targetTypeEntryName, qStringT); } if (targetType.isEnum()) { @@ -320,25 +320,36 @@ void OverloadDataRootNode::sortNextOverloads(const ApiExtractorResult &api) } // QByteArray args need to be checked after QString args - if (graph.hasNode(qStringT()) && graph.hasNode(qByteArrayT())) - graph.addEdge(qStringT(), qByteArrayT()); + if (graph.hasNode(qStringT) && graph.hasNode(qByteArrayT)) + graph.addEdge(qStringT, qByteArrayT); + + static const Edge rangeOrder[] = + {{doubleT, floatT}, + {longLongT, longT}, {longLongT, intT}, {intT, shortT}, + {unsignedLongLongT, unsignedLongT}, {unsignedLongLongT, unsignedT}, + {unsignedLongLongT, unsignedIntT}, {unsignedT, unsignedShortT} + }; + for (const auto &r : rangeOrder) { + if (graph.hasNode(r.first) && graph.hasNode(r.second)) + graph.addEdge(r.first, r.second); + } - for (const auto &ov : qAsConst(m_children)) { + for (const auto &ov : std::as_const(m_children)) { const AbstractMetaType &targetType = ov->argType(); if (!targetType.isEnum()) continue; QString targetTypeEntryName = getTypeName(targetType); // Enum values must precede types implicitly convertible from "int" or "unsigned int". - for (const QString &implicitFromInt : qAsConst(classesWithIntegerImplicitConversion)) + for (const QString &implicitFromInt : std::as_const(classesWithIntegerImplicitConversion)) graph.addEdge(targetTypeEntryName, implicitFromInt); } // Special case for double(int i) (not tracked by m_generator->implicitConversions - for (const QString &signedIntegerName : qAsConst(signedIntegerPrimitives)) { + for (const QString &signedIntegerName : std::as_const(signedIntegerPrimitives)) { if (graph.hasNode(signedIntegerName)) { - for (const QString &nonIntegerName : qAsConst(nonIntegerPrimitives)) { + for (const QString &nonIntegerName : std::as_const(nonIntegerPrimitives)) { if (graph.hasNode(nonIntegerName)) graph.addEdge(nonIntegerName, signedIntegerName); } @@ -381,8 +392,7 @@ static std::pair<int, int> getMinMaxArgs(const AbstractMetaFunctionCPtr &func) int defaultValueIndex = -1; const auto &arguments = func->arguments(); int argIndex = 0; - for (qsizetype i = 0, size = arguments.size(); i < size; ++i) { - const auto &arg = arguments.at(i); + for (const auto &arg : arguments) { if (!arg.isModifiedRemoved()) { if (defaultValueIndex < 0 && arg.hasDefaultValueExpression()) defaultValueIndex = argIndex; @@ -463,7 +473,7 @@ OverloadDataNode *OverloadDataRootNode::addOverloadDataNode(const AbstractMetaFu { OverloadDataNodePtr overloadData; if (!func->isOperatorOverload()) { - for (const auto &tmp : qAsConst(m_children)) { + for (const auto &tmp : std::as_const(m_children)) { // TODO: 'const char *', 'char *' and 'char' will have the same TypeEntry? // If an argument have a type replacement, then we should create a new overloaddata @@ -475,13 +485,13 @@ OverloadDataNode *OverloadDataRootNode::addOverloadDataNode(const AbstractMetaFu } } - if (overloadData.isNull()) { + if (!overloadData) { const int argpos = argPos() + 1; overloadData.reset(new OverloadDataNode(func, this, arg, argpos)); m_children.append(overloadData); } - return overloadData.data(); + return overloadData.get(); } bool OverloadData::hasNonVoidReturnType() const @@ -604,7 +614,7 @@ const AbstractMetaArgument *OverloadDataNode::overloadArgument(const AbstractMet bool OverloadDataRootNode::nextArgumentHasDefaultValue() const { for (const auto &overloadData : m_children) { - if (!overloadData->getFunctionWithDefaultValue().isNull()) + if (overloadData->getFunctionWithDefaultValue()) return true; } return false; @@ -612,20 +622,20 @@ bool OverloadDataRootNode::nextArgumentHasDefaultValue() const static const OverloadDataRootNode *_findNextArgWithDefault(const OverloadDataRootNode *overloadData) { - if (!overloadData->getFunctionWithDefaultValue().isNull()) + if (overloadData->getFunctionWithDefaultValue()) return overloadData; const OverloadDataRootNode *result = nullptr; const OverloadDataList &data = overloadData->children(); for (const auto &odata : data) { - const auto *tmp = _findNextArgWithDefault(odata.data()); + const auto *tmp = _findNextArgWithDefault(odata.get()); if (!result || (tmp && result->argPos() > tmp->argPos())) result = tmp; } return result; } -const OverloadDataRootNode *OverloadDataRootNode::findNextArgWithDefault() +const OverloadDataRootNode *OverloadDataRootNode::findNextArgWithDefault() const { return _findNextArgWithDefault(this); } @@ -641,10 +651,10 @@ bool OverloadDataRootNode::isFinalOccurrence(const AbstractMetaFunctionCPtr &fun AbstractMetaFunctionCPtr OverloadDataRootNode::getFunctionWithDefaultValue() const { - const int argpos = argPos(); + const qsizetype argpos = argPos(); for (const auto &func : m_overloads) { - int removedArgs = 0; - for (int i = 0; i <= argpos + removedArgs; i++) { + qsizetype removedArgs = 0; + for (qsizetype i = 0; i <= argpos + removedArgs; i++) { if (func->arguments().at(i).isModifiedRemoved()) removedArgs++; } @@ -661,7 +671,7 @@ QList<int> OverloadData::invalidArgumentLengths() const for (const auto &func : m_overloads) { const AbstractMetaArgumentList args = func->arguments(); int offset = 0; - for (int i = 0; i < args.size(); ++i) { + for (qsizetype i = 0; i < args.size(); ++i) { if (func->arguments().at(i).isModifiedRemoved()) { offset++; } else { @@ -691,8 +701,8 @@ int OverloadData::numberOfRemovedArguments(const AbstractMetaFunctionCPtr &func, { Q_ASSERT(finalArgPos >= 0); int removed = 0; - const int size = func->arguments().size(); - for (int i = 0; i < qMin(size, finalArgPos + removed); ++i) { + const auto size = func->arguments().size(); + for (qsizetype i = 0; i < qMin(size, qsizetype(finalArgPos + removed)); ++i) { if (func->arguments().at(i).isModifiedRemoved()) ++removed; } @@ -935,7 +945,7 @@ void OverloadDataRootNode::formatOverloads(QDebug &d) const if (count < 2) return; d << "=("; - for (int i = 0; i < count; ++i) { + for (qsizetype i = 0; i < count; ++i) { if (i) d << '\n'; d << m_overloads.at(i)->signature(); @@ -949,7 +959,7 @@ void OverloadDataRootNode::formatNextOverloadData(QDebug &d) const d << ", next[" << count << ']'; if (d.verbosity() >= 3) { d << "=("; - for (int i = 0; i < count; ++i) { + for (qsizetype i = 0; i < count; ++i) { if (i) d << '\n'; m_children.at(i)->formatDebug(d); |