Skip to content

Commit 2d72bc5

Browse files
usiemsmrbean-bremen
authored andcommitted
Filter out methods that do the same (at least in Python)
1 parent 8769ef4 commit 2d72bc5

6 files changed

+66
-1
lines changed

generator/abstractmetabuilder.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,48 @@ void AbstractMetaBuilder::traverseFunctions(ScopeModelItem scope_item, AbstractM
11661166
}
11671167
}
11681168
}
1169+
removeEquivalentFunctions(meta_class);
1170+
}
1171+
1172+
void AbstractMetaBuilder::removeEquivalentFunctions(AbstractMetaClass* parent)
1173+
{
1174+
AbstractMetaFunctionList functions = parent->functions();
1175+
for (AbstractMetaFunction* fun : functions)
1176+
{
1177+
AbstractMetaArgumentList args = fun->arguments();
1178+
bool candidateToRemove = false;
1179+
for (AbstractMetaArgument* arg : args) {
1180+
const TypeEntry* argType = arg->type()->typeEntry();
1181+
if (argType && argType->equivalentType()) {
1182+
candidateToRemove = true;
1183+
break;
1184+
}
1185+
}
1186+
if (!candidateToRemove) {
1187+
continue;
1188+
}
1189+
// check if there are other functions with the same name and equivalent parameters
1190+
AbstractMetaFunctionList overloadedFunctions = parent->queryFunctionsByName(fun->name());
1191+
for (AbstractMetaFunction* overload : overloadedFunctions) {
1192+
if (overload != fun) {
1193+
AbstractMetaArgumentList overloadArgs = overload->arguments();
1194+
if (overloadArgs.size() == args.size()) {
1195+
bool equivalentArgs = true;
1196+
for (int i = 0; i < args.size() && equivalentArgs; i++) {
1197+
const TypeEntry* argType = args[i]->type()->typeEntry();
1198+
const TypeEntry* overloadArgType = overloadArgs[i]->type()->typeEntry();
1199+
// This could have some more equivalency checks, but currently this seems to be sufficient
1200+
equivalentArgs = (argType && overloadArgType &&
1201+
(argType == overloadArgType || argType->equivalentType() == overloadArgType));
1202+
}
1203+
if (equivalentArgs) {
1204+
parent->removeFunction(fun);
1205+
break;
1206+
}
1207+
}
1208+
}
1209+
}
1210+
}
11691211
}
11701212

11711213
bool AbstractMetaBuilder::setupInheritance(AbstractMetaClass *meta_class)

generator/abstractmetabuilder.h

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class AbstractMetaBuilder
9696
void traverseStreamOperator(FunctionModelItem function_item);
9797
void traverseCompareOperator(FunctionModelItem item);
9898
void traverseBinaryArithmeticOperator(FunctionModelItem item);
99+
100+
//! remove functions/methods that are overloads with equivalent parameter types
101+
//! when called from Python
102+
void removeEquivalentFunctions(AbstractMetaClass* parent);
99103

100104
AbstractMetaFunction *traverseFunction(FunctionModelItem function);
101105
AbstractMetaField *traverseField(VariableModelItem field, const AbstractMetaClass *cls);

generator/abstractmetalang.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,11 @@ void AbstractMetaClass::addFunction(AbstractMetaFunction *function)
11031103
m_has_nonpublic |= !function->isPublic();
11041104
}
11051105

1106+
void AbstractMetaClass::removeFunction(AbstractMetaFunction* function)
1107+
{
1108+
m_functions.removeOne(function);
1109+
}
1110+
11061111
bool AbstractMetaClass::hasSignal(const AbstractMetaFunction *other) const
11071112
{
11081113
if (!other->isSignal())

generator/abstractmetalang.h

+1
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ class AbstractMetaClass : public AbstractMetaAttributes
688688
AbstractMetaFunctionList functions() const { return m_functions; }
689689
void setFunctions(const AbstractMetaFunctionList &functions);
690690
void addFunction(AbstractMetaFunction *function);
691+
void removeFunction(AbstractMetaFunction* function);
691692
bool hasFunction(const AbstractMetaFunction *f) const;
692693
bool hasFunction(const QString &str) const;
693694
bool hasSignal(const AbstractMetaFunction *f) const;

generator/typesystem.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1509,22 +1509,27 @@ TypeDatabase *TypeDatabase::instance()
15091509

15101510
TypeDatabase::TypeDatabase() : m_suppressWarnings(true)
15111511
{
1512-
addType(new StringTypeEntry("QString"));
1512+
StringTypeEntry* mainStringType = new StringTypeEntry("QString");
1513+
addType(mainStringType);
15131514

15141515
StringTypeEntry *e = new StringTypeEntry("QLatin1String");
15151516
e->setPreferredConversion(false);
1517+
e->setEquivalentType(mainStringType);
15161518
addType(e);
15171519

15181520
e = new StringTypeEntry("QStringRef");
15191521
e->setPreferredConversion(false);
1522+
e->setEquivalentType(mainStringType);
15201523
addType(e);
15211524

15221525
e = new StringTypeEntry("QStringView");
15231526
e->setPreferredConversion(false);
1527+
e->setEquivalentType(mainStringType);
15241528
addType(e);
15251529

15261530
e = new StringTypeEntry("QAnyStringView");
15271531
e->setPreferredConversion(false);
1532+
e->setEquivalentType(mainStringType);
15281533
addType(e);
15291534

15301535
e = new StringTypeEntry("QXmlStreamStringRef");

generator/typesystem.h

+8
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ class TypeEntry
549549

550550
virtual bool isNativeIdBased() const { return false; }
551551

552+
virtual TypeEntry* equivalentType() const { return nullptr; }
553+
552554
private:
553555
QString m_name;
554556
Type m_type;
@@ -1013,8 +1015,14 @@ class ValueTypeEntry : public ComplexTypeEntry
10131015

10141016
virtual bool isNativeIdBased() const { return true; }
10151017

1018+
virtual TypeEntry* equivalentType() const { return _equivalentType; }
1019+
void setEquivalentType(TypeEntry* typeEntry) { _equivalentType = typeEntry; }
1020+
10161021
protected:
10171022
ValueTypeEntry(const QString &name, Type t) : ComplexTypeEntry(name, t) { }
1023+
1024+
private:
1025+
TypeEntry* _equivalentType{};
10181026
};
10191027

10201028

0 commit comments

Comments
 (0)