Skip to content

Commit b0db304

Browse files
ddobrevtritao
authored andcommitted
Fixed overloading of operators with parameters mapped to the same type.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent f4673f5 commit b0db304

File tree

4 files changed

+66
-11
lines changed

4 files changed

+66
-11
lines changed

src/Generator/Passes/CheckAmbiguousFunctions.cs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using CppSharp.AST;
45
using CppSharp.AST.Extensions;
56
using CppSharp.Types;
7+
using CppSharp.Generators;
68

79
namespace CppSharp.Passes
810
{
@@ -68,13 +70,16 @@ public override bool VisitFunctionDecl(AST.Function function)
6870

6971
private bool CheckDefaultParametersForAmbiguity(Function function, Function overload)
7072
{
71-
var commonParameters = Math.Min(function.Parameters.Count, overload.Parameters.Count);
73+
List<Parameter> functionParams = RemoveOperatorParams(function);
74+
List<Parameter> overloadParams = RemoveOperatorParams(overload);
75+
76+
var commonParameters = Math.Min(functionParams.Count, overloadParams.Count);
7277

7378
var i = 0;
7479
for (; i < commonParameters; ++i)
7580
{
76-
AST.Type funcType = GetFinalType(function.Parameters[i]);
77-
AST.Type overloadType = GetFinalType(overload.Parameters[i]);
81+
AST.Type funcType = GetFinalType(functionParams[i]);
82+
AST.Type overloadType = GetFinalType(overloadParams[i]);
7883

7984
AST.Type funcPointee = funcType.GetFinalPointee() ?? funcType;
8085
AST.Type overloadPointee = overloadType.GetFinalPointee() ?? overloadType;
@@ -85,28 +90,53 @@ private bool CheckDefaultParametersForAmbiguity(Function function, Function over
8590
return false;
8691
}
8792

88-
for (; i < function.Parameters.Count; ++i)
93+
for (; i < functionParams.Count; ++i)
8994
{
90-
var funcParam = function.Parameters[i];
95+
var funcParam = functionParams[i];
9196
if (!funcParam.HasDefaultValue)
9297
return false;
9398
}
9499

95-
for (; i < overload.Parameters.Count; ++i)
100+
for (; i < overloadParams.Count; ++i)
96101
{
97-
var overloadParam = overload.Parameters[i];
102+
var overloadParam = overloadParams[i];
98103
if (!overloadParam.HasDefaultValue)
99104
return false;
100105
}
101106

102-
if (function.Parameters.Count > overload.Parameters.Count)
107+
if (functionParams.Count > overloadParams.Count)
103108
overload.ExplicitlyIgnore();
104109
else
105110
function.ExplicitlyIgnore();
106111

107112
return true;
108113
}
109114

115+
private List<Parameter> RemoveOperatorParams(Function function)
116+
{
117+
var functionParams = new List<Parameter>(function.Parameters);
118+
119+
if (!function.IsOperator ||
120+
(Context.Options.GeneratorKind != GeneratorKind.CLI &&
121+
Context.Options.GeneratorKind != GeneratorKind.CSharp))
122+
return functionParams;
123+
124+
// C++ operators in a class have no class param unlike C#
125+
// but we need to be able to compare them to free operators.
126+
Parameter param = functionParams.Find(p => p.Kind == ParameterKind.Regular);
127+
if (param != null)
128+
{
129+
AST.Type type = param.Type.Desugar();
130+
type = (type.GetFinalPointee() ?? type).Desugar();
131+
Class @class;
132+
if (type.TryGetClass(out @class) &&
133+
function.Namespace == @class)
134+
functionParams.Remove(param);
135+
}
136+
137+
return functionParams;
138+
}
139+
110140
private AST.Type GetFinalType(Parameter parameter)
111141
{
112142
TypeMap typeMap;

tests/Common/Common.Tests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,13 @@ public void TestEqualityOperator()
632632
var bar = new Bar { A = 5, B = 5.5f };
633633
Assert.IsTrue(bar == bar);
634634
Assert.IsFalse(new Bar { A = 5, B = 5.6f } == bar);
635+
#if !__MonoCS__
635636
using (var differentConstOverloads = new DifferentConstOverloads())
636637
{
637-
Assert.IsTrue(differentConstOverloads != null);
638+
DifferentConstOverloads other = null;
639+
Assert.IsTrue(differentConstOverloads != other);
638640
}
641+
#endif
639642

640643
#pragma warning restore 1718
641644
}
@@ -654,7 +657,10 @@ public void TestOperatorOverloads()
654657
{
655658
var differentConstOverloads = new DifferentConstOverloads();
656659
Assert.IsTrue(differentConstOverloads == new DifferentConstOverloads());
657-
Assert.IsFalse(differentConstOverloads == 5);
660+
Assert.IsTrue(differentConstOverloads == 5);
661+
Assert.IsFalse(differentConstOverloads == 4);
662+
Assert.IsTrue(differentConstOverloads == "abcde");
663+
Assert.IsFalse(differentConstOverloads == "abcd");
658664
}
659665

660666
[Test]

tests/Common/Common.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ DifferentConstOverloads::DifferentConstOverloads() : i(5)
652652
{
653653
}
654654

655+
int DifferentConstOverloads::getI() const
656+
{
657+
return i;
658+
}
659+
655660
bool DifferentConstOverloads::operator ==(const DifferentConstOverloads& other)
656661
{
657662
return i == other.i;
@@ -664,7 +669,17 @@ bool DifferentConstOverloads::operator !=(const DifferentConstOverloads& other)
664669

665670
bool DifferentConstOverloads::operator ==(int number) const
666671
{
667-
return false;
672+
return i == number;
673+
}
674+
675+
bool DifferentConstOverloads::operator ==(std::string s) const
676+
{
677+
return i == s.length();
678+
}
679+
680+
bool operator ==(const DifferentConstOverloads& d, const char* s)
681+
{
682+
return d.getI() == strlen(s);
668683
}
669684

670685
int HasVirtualProperty::getProperty()

tests/Common/Common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,13 +900,17 @@ class DLL_API DifferentConstOverloads
900900
{
901901
public:
902902
DifferentConstOverloads();
903+
int getI() const;
903904
bool operator ==(const DifferentConstOverloads& other);
904905
bool operator !=(const DifferentConstOverloads& other);
905906
bool operator ==(int number) const;
907+
bool operator ==(std::string s) const;
906908
private:
907909
int i;
908910
};
909911

912+
DLL_API bool operator ==(const DifferentConstOverloads& d, const char* s);
913+
910914
class TestNamingAnonymousTypesInUnion
911915
{
912916
public:

0 commit comments

Comments
 (0)