diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-07 10:28:58 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-09-07 14:38:37 +0200 |
commit | 5013f3376cf7bf57eb6003fc4453baefe9060dc5 (patch) | |
tree | 81507fb14025f7dd72a69f7cf70d77308a7cb666 | |
parent | 6541627d63428f8e6315d992180ce8855c246668 (diff) |
shiboken6: Remove OverloadData::overloadsWithoutRepetition()
Mpve the functionality to remove the const-overloads into
ShibokenGenerator::getFunctionGroups(), so that it is not called
repeatedly. Amends 5e4a1287c1742f96c5ba3ce0aca75791ce806157.
Task-number: PYSIDE-1653
Change-Id: I4ed8dfac9d4101a9c1abb76aed884b194f7bf793
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
6 files changed, 45 insertions, 20 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp b/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp index 2199d7fe2..8aae6c19b 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp @@ -384,6 +384,30 @@ AbstractMetaFunction::CompareResult AbstractMetaFunction::compareTo(const Abstra return result; } +// Is this the const overload of another function of equivalent return type? +bool AbstractMetaFunction::isConstOverloadOf(const AbstractMetaFunction *other) const +{ + const auto argumentCount = d->m_arguments.size(); + if (!isConstant() || other->isConstant() || name() != other->name() + || argumentCount != other->arguments().size()) { + return false; + } + + // Match "const Foo &getFoo() const" / "Foo &getFoo()" / "Foo getFoo() const" + const auto otherType = other->type(); + if (d->m_type.name() != otherType.name() + || d->m_type.indirectionsV() != otherType.indirectionsV()) { + return false; + } + + const auto &otherArguments = other->arguments(); + for (qsizetype a = 0; a < argumentCount; ++a) { + if (d->m_arguments.at(a).type() != otherArguments.at(a).type()) + return false; + } + return true; +} + AbstractMetaFunction *AbstractMetaFunction::copy() const { auto *cpy = new AbstractMetaFunction; diff --git a/sources/shiboken6/ApiExtractor/abstractmetafunction.h b/sources/shiboken6/ApiExtractor/abstractmetafunction.h index 20e2e6d90..415fc4ae2 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetafunction.h +++ b/sources/shiboken6/ApiExtractor/abstractmetafunction.h @@ -295,6 +295,7 @@ public: bool isUserDeclared() const; CompareResult compareTo(const AbstractMetaFunction *other) const; + bool isConstOverloadOf(const AbstractMetaFunction *other) const; bool operator <(const AbstractMetaFunction &a) const; diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 345cfc4b6..472e57de8 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -2739,7 +2739,7 @@ void CppGenerator::writeOverloadedFunctionDecisor(TextStream &s, const OverloadD { s << "// Overloaded function decisor\n"; const auto rfunc = overloadData.referenceFunction(); - const AbstractMetaFunctionCList &functionOverloads = overloadData.overloadsWithoutRepetition(); + const AbstractMetaFunctionCList &functionOverloads = overloadData.overloads(); for (int i = 0; i < functionOverloads.count(); i++) { const auto func = functionOverloads.at(i); s << "// " << i << ": "; @@ -2938,7 +2938,7 @@ void CppGenerator::writeOverloadedFunctionDecisorEngine(TextStream &s, void CppGenerator::writeFunctionCalls(TextStream &s, const OverloadData &overloadData, const GeneratorContext &context) const { - const AbstractMetaFunctionCList &overloads = overloadData.overloadsWithoutRepetition(); + const AbstractMetaFunctionCList &overloads = overloadData.overloads(); s << "// Call function/method\n" << (overloads.count() > 1 ? "switch (overloadId) " : "") << "{\n"; { diff --git a/sources/shiboken6/generator/shiboken/overloaddata.cpp b/sources/shiboken6/generator/shiboken/overloaddata.cpp index 6f0efabe4..3be14dc9a 100644 --- a/sources/shiboken6/generator/shiboken/overloaddata.cpp +++ b/sources/shiboken6/generator/shiboken/overloaddata.cpp @@ -719,22 +719,6 @@ bool OverloadData::isFinalOccurrence(const AbstractMetaFunctionCPtr &func) const return true; } -AbstractMetaFunctionCList OverloadData::overloadsWithoutRepetition() const -{ - AbstractMetaFunctionCList overloads = m_overloads; - for (const auto &func : m_overloads) { - if (func->minimalSignature().endsWith(QLatin1String("const"))) - continue; - for (const auto &f : qAsConst(overloads)) { - if ((func->minimalSignature() + QLatin1String("const")) == f->minimalSignature()) { - overloads.removeOne(f); - break; - } - } - } - return overloads; -} - AbstractMetaFunctionCPtr OverloadData::getFunctionWithDefaultValue() const { for (const auto &func : m_overloads) { diff --git a/sources/shiboken6/generator/shiboken/overloaddata.h b/sources/shiboken6/generator/shiboken/overloaddata.h index 88fbdc867..c4d7e6b51 100644 --- a/sources/shiboken6/generator/shiboken/overloaddata.h +++ b/sources/shiboken6/generator/shiboken/overloaddata.h @@ -105,8 +105,6 @@ public: OverloadData *findNextArgWithDefault(); bool isFinalOccurrence(const AbstractMetaFunctionCPtr &func) const; - /// Returns the list of overloads removing repeated constant functions (ex.: "foo()" and "foo()const", the second is removed). - AbstractMetaFunctionCList overloadsWithoutRepetition() const; const AbstractMetaFunctionCList &overloads() const { return m_overloads; } OverloadDataList nextOverloadData() const { return m_nextOverloadData; } OverloadData *previousOverloadData() const { return m_previousOverloadData; } diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index 86891a508..4b0692c61 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -2278,6 +2278,23 @@ ShibokenGenerator::FunctionGroups ShibokenGenerator::getFunctionGroups(const Abs return getGeneratorClassInfo(scope).functionGroups; } +// Use non-const overloads only, for example, "foo()" and "foo()const" +// the second is removed. +static void removeConstOverloads(AbstractMetaFunctionCList *overloads) +{ + for (qsizetype i = overloads->size() - 1; i >= 0; --i) { + const auto &f = overloads->at(i); + if (f->isConstant()) { + for (qsizetype c = 0; c < i; ++c) { + if (f->isConstOverloadOf(overloads->at(c).data())) { + overloads->removeAt(i); + break; + } + } + } + } +} + ShibokenGenerator::FunctionGroups ShibokenGenerator::getFunctionGroupsImpl(const AbstractMetaClass *scope) { AbstractMetaFunctionCList lst = scope->functions(); @@ -2303,6 +2320,7 @@ ShibokenGenerator::FunctionGroups ShibokenGenerator::getFunctionGroupsImpl(const it.value().append(func); } getInheritedOverloads(scope, &it.value()); + removeConstOverloads(&it.value()); } } return results; |