Skip to content

Commit b966bd6

Browse files
committed
Fixed the generated C# for const/non-const overloads with > 1 param.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 980f367 commit b966bd6

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

src/Generator/Passes/CheckAmbiguousFunctions.cs

+35-45
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override bool VisitFunctionDecl(AST.Function function)
5454

5555
if (CheckConstnessForAmbiguity(function, overload) ||
5656
CheckDefaultParametersForAmbiguity(function, overload) ||
57-
CheckSingleParameterPointerConstnessForAmbiguity(function, overload))
57+
CheckParametersPointerConstnessForAmbiguity(function, overload))
5858
{
5959
function.IsAmbiguous = true;
6060
overload.IsAmbiguous = true;
@@ -186,67 +186,57 @@ private static bool CheckConstnessForAmbiguity(Function function, Function overl
186186
return false;
187187
}
188188

189-
private static bool CheckSingleParameterPointerConstnessForAmbiguity(
189+
private static bool CheckParametersPointerConstnessForAmbiguity(
190190
Function function, Function overload)
191191
{
192192
var functionParams = function.Parameters.Where(
193193
p => p.Kind == ParameterKind.Regular).ToList();
194194

195-
// It's difficult to handle this case for more than one parameter.
196-
// For example, if we have:
197-
//
198-
// void f(float&, const int&);
199-
// void f(const float&, int&);
200-
//
201-
// What should we do? Generate both? Generate the first one encountered?
202-
// Generate the one with the least amount of "complex" parameters?
203-
// So let's just start with the simplest case for the time being.
204-
205-
if (functionParams.Count != 1)
206-
return false;
207-
208195
var overloadParams = overload.Parameters.Where(
209196
p => p.Kind == ParameterKind.Regular).ToList();
210197

211-
if (overloadParams.Count != 1)
198+
if (functionParams.Count != overloadParams.Count)
212199
return false;
213200

214-
var parameterFunction = functionParams[0];
215-
var parameterOverload = overloadParams[0];
201+
for (int i = 0; i < functionParams.Count; i++)
202+
{
203+
var parameterFunction = functionParams[i];
204+
var parameterOverload = overloadParams[i];
216205

217-
var pointerParamFunction = parameterFunction.Type.Desugar() as PointerType;
218-
var pointerParamOverload = parameterOverload.Type.Desugar() as PointerType;
206+
var pointerParamFunction = parameterFunction.Type.Desugar() as PointerType;
207+
var pointerParamOverload = parameterOverload.Type.Desugar() as PointerType;
219208

220-
if (pointerParamFunction == null || pointerParamOverload == null)
221-
return false;
209+
if (pointerParamFunction == null || pointerParamOverload == null)
210+
continue;
222211

223-
if (!pointerParamFunction.GetPointee().Equals(pointerParamOverload.GetPointee()))
224-
return false;
212+
if (!pointerParamFunction.GetPointee().Equals(pointerParamOverload.GetPointee()))
213+
continue;
225214

226-
if (parameterFunction.IsConst && !parameterOverload.IsConst)
227-
{
228-
function.ExplicitlyIgnore();
229-
return true;
230-
}
215+
if (parameterFunction.IsConst && !parameterOverload.IsConst)
216+
{
217+
function.ExplicitlyIgnore();
218+
return true;
219+
}
231220

232-
if (parameterOverload.IsConst && !parameterFunction.IsConst)
233-
{
234-
overload.ExplicitlyIgnore();
235-
return true;
236-
}
221+
if (parameterOverload.IsConst && !parameterFunction.IsConst)
222+
{
223+
overload.ExplicitlyIgnore();
224+
return true;
225+
}
237226

238-
if (pointerParamFunction.Modifier == PointerType.TypeModifier.RVReference &&
239-
pointerParamOverload.Modifier != PointerType.TypeModifier.RVReference)
240-
{
241-
function.ExplicitlyIgnore();
242-
return true;
243-
}
227+
if (pointerParamFunction.Modifier == PointerType.TypeModifier.RVReference &&
228+
pointerParamOverload.Modifier != PointerType.TypeModifier.RVReference)
229+
{
230+
function.ExplicitlyIgnore();
231+
return true;
232+
}
244233

245-
if (pointerParamFunction.Modifier != PointerType.TypeModifier.RVReference &&
246-
pointerParamOverload.Modifier == PointerType.TypeModifier.RVReference)
247-
{
248-
overload.ExplicitlyIgnore();
249-
return true;
234+
if (pointerParamFunction.Modifier != PointerType.TypeModifier.RVReference &&
235+
pointerParamOverload.Modifier == PointerType.TypeModifier.RVReference)
236+
{
237+
overload.ExplicitlyIgnore();
238+
return true;
239+
}
250240
}
251241

252242
return false;

tests/Common/Common.Tests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public unsafe void TestCodeGeneration()
5959
Common.IntegerOverload(0);
6060
Common.IntegerOverload((uint) 0);
6161
Common.TakeVoidStarStar(null);
62+
Common.OverloadPointer(IntPtr.Zero, 1);
6263
using (new DerivedFromSecondaryBaseWithIgnoredVirtualMethod()) { }
6364

6465
#pragma warning restore 0168

tests/Common/Common.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,11 @@ void takeReferenceToVoidStar(const void*& p)
10141014
void takeVoidStarStar(void** p)
10151015
{
10161016
}
1017+
1018+
void overloadPointer(void* p, int i)
1019+
{
1020+
}
1021+
1022+
void overloadPointer(const void* p, int i)
1023+
{
1024+
}

tests/Common/Common.h

+2
Original file line numberDiff line numberDiff line change
@@ -1482,3 +1482,5 @@ DLL_API void integerOverload(long i);
14821482
DLL_API void integerOverload(unsigned long i);
14831483
DLL_API void takeReferenceToVoidStar(const void*& p);
14841484
DLL_API void takeVoidStarStar(void** p);
1485+
DLL_API void overloadPointer(void* p, int i = 0);
1486+
DLL_API void overloadPointer(const void* p, int i = 0);

0 commit comments

Comments
 (0)