Browse Source

Fixed the generated C# for const/non-const overloads with > 1 param.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1170/head
Dimitar Dobrev 7 years ago
parent
commit
b966bd6798
  1. 80
      src/Generator/Passes/CheckAmbiguousFunctions.cs
  2. 1
      tests/Common/Common.Tests.cs
  3. 8
      tests/Common/Common.cpp
  4. 2
      tests/Common/Common.h

80
src/Generator/Passes/CheckAmbiguousFunctions.cs

@ -54,7 +54,7 @@ namespace CppSharp.Passes
if (CheckConstnessForAmbiguity(function, overload) || if (CheckConstnessForAmbiguity(function, overload) ||
CheckDefaultParametersForAmbiguity(function, overload) || CheckDefaultParametersForAmbiguity(function, overload) ||
CheckSingleParameterPointerConstnessForAmbiguity(function, overload)) CheckParametersPointerConstnessForAmbiguity(function, overload))
{ {
function.IsAmbiguous = true; function.IsAmbiguous = true;
overload.IsAmbiguous = true; overload.IsAmbiguous = true;
@ -186,67 +186,57 @@ namespace CppSharp.Passes
return false; return false;
} }
private static bool CheckSingleParameterPointerConstnessForAmbiguity( private static bool CheckParametersPointerConstnessForAmbiguity(
Function function, Function overload) Function function, Function overload)
{ {
var functionParams = function.Parameters.Where( var functionParams = function.Parameters.Where(
p => p.Kind == ParameterKind.Regular).ToList(); p => p.Kind == ParameterKind.Regular).ToList();
// It's difficult to handle this case for more than one parameter.
// For example, if we have:
//
// void f(float&, const int&);
// void f(const float&, int&);
//
// What should we do? Generate both? Generate the first one encountered?
// Generate the one with the least amount of "complex" parameters?
// So let's just start with the simplest case for the time being.
if (functionParams.Count != 1)
return false;
var overloadParams = overload.Parameters.Where( var overloadParams = overload.Parameters.Where(
p => p.Kind == ParameterKind.Regular).ToList(); p => p.Kind == ParameterKind.Regular).ToList();
if (overloadParams.Count != 1) if (functionParams.Count != overloadParams.Count)
return false; return false;
var parameterFunction = functionParams[0]; for (int i = 0; i < functionParams.Count; i++)
var parameterOverload = overloadParams[0]; {
var parameterFunction = functionParams[i];
var parameterOverload = overloadParams[i];
var pointerParamFunction = parameterFunction.Type.Desugar() as PointerType; var pointerParamFunction = parameterFunction.Type.Desugar() as PointerType;
var pointerParamOverload = parameterOverload.Type.Desugar() as PointerType; var pointerParamOverload = parameterOverload.Type.Desugar() as PointerType;
if (pointerParamFunction == null || pointerParamOverload == null) if (pointerParamFunction == null || pointerParamOverload == null)
return false; continue;
if (!pointerParamFunction.GetPointee().Equals(pointerParamOverload.GetPointee())) if (!pointerParamFunction.GetPointee().Equals(pointerParamOverload.GetPointee()))
return false; continue;
if (parameterFunction.IsConst && !parameterOverload.IsConst) if (parameterFunction.IsConst && !parameterOverload.IsConst)
{ {
function.ExplicitlyIgnore(); function.ExplicitlyIgnore();
return true; return true;
} }
if (parameterOverload.IsConst && !parameterFunction.IsConst) if (parameterOverload.IsConst && !parameterFunction.IsConst)
{ {
overload.ExplicitlyIgnore(); overload.ExplicitlyIgnore();
return true; return true;
} }
if (pointerParamFunction.Modifier == PointerType.TypeModifier.RVReference && if (pointerParamFunction.Modifier == PointerType.TypeModifier.RVReference &&
pointerParamOverload.Modifier != PointerType.TypeModifier.RVReference) pointerParamOverload.Modifier != PointerType.TypeModifier.RVReference)
{ {
function.ExplicitlyIgnore(); function.ExplicitlyIgnore();
return true; return true;
} }
if (pointerParamFunction.Modifier != PointerType.TypeModifier.RVReference && if (pointerParamFunction.Modifier != PointerType.TypeModifier.RVReference &&
pointerParamOverload.Modifier == PointerType.TypeModifier.RVReference) pointerParamOverload.Modifier == PointerType.TypeModifier.RVReference)
{ {
overload.ExplicitlyIgnore(); overload.ExplicitlyIgnore();
return true; return true;
}
} }
return false; return false;

1
tests/Common/Common.Tests.cs

@ -59,6 +59,7 @@ public class CommonTests : GeneratorTestFixture
Common.IntegerOverload(0); Common.IntegerOverload(0);
Common.IntegerOverload((uint) 0); Common.IntegerOverload((uint) 0);
Common.TakeVoidStarStar(null); Common.TakeVoidStarStar(null);
Common.OverloadPointer(IntPtr.Zero, 1);
using (new DerivedFromSecondaryBaseWithIgnoredVirtualMethod()) { } using (new DerivedFromSecondaryBaseWithIgnoredVirtualMethod()) { }
#pragma warning restore 0168 #pragma warning restore 0168

8
tests/Common/Common.cpp

@ -1014,3 +1014,11 @@ void takeReferenceToVoidStar(const void*& p)
void takeVoidStarStar(void** p) void takeVoidStarStar(void** p)
{ {
} }
void overloadPointer(void* p, int i)
{
}
void overloadPointer(const void* p, int i)
{
}

2
tests/Common/Common.h

@ -1482,3 +1482,5 @@ DLL_API void integerOverload(long i);
DLL_API void integerOverload(unsigned long i); DLL_API void integerOverload(unsigned long i);
DLL_API void takeReferenceToVoidStar(const void*& p); DLL_API void takeReferenceToVoidStar(const void*& p);
DLL_API void takeVoidStarStar(void** p); DLL_API void takeVoidStarStar(void** p);
DLL_API void overloadPointer(void* p, int i = 0);
DLL_API void overloadPointer(const void* p, int i = 0);

Loading…
Cancel
Save