Skip to content

Commit aef1acb

Browse files
committed
Fixed the generated C# for default arguments of type pointer to a function.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent a1af3d3 commit aef1acb

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

src/Generator/Passes/HandleDefaultParamValuesPass.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public override bool VisitFunctionDecl(Function function)
6969
if (!desugared.IsPrimitiveTypeConvertibleToRef() &&
7070
(expression.String == "0" || expression.String == "nullptr"))
7171
{
72-
result = $"default({desugared})";
72+
result = desugared.GetPointee()?.Desugar() is FunctionType ?
73+
"null" : $"default({desugared})";
7374
return true;
7475
}
7576

@@ -122,13 +123,6 @@ private bool CheckForDefaultPointer(Type desugared, ref string result)
122123
if (!desugared.IsPointer())
123124
return false;
124125

125-
// IntPtr.Zero is not a constant
126-
if (desugared.IsPointerToPrimitiveType(PrimitiveType.Void))
127-
{
128-
result = "new global::System.IntPtr()";
129-
return true;
130-
}
131-
132126
if (desugared.IsPrimitiveTypeConvertibleToRef())
133127
return false;
134128

tests/CSharp/CSharp.Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ public void TestDefaultArguments()
215215
{
216216
methodsWithDefaultValues.DefaultPointer();
217217
methodsWithDefaultValues.DefaultVoidStar();
218+
methodsWithDefaultValues.DefaultFunctionPointer();
218219
methodsWithDefaultValues.DefaultValueType();
219220
methodsWithDefaultValues.DefaultChar();
220221
methodsWithDefaultValues.DefaultEmptyChar();

tests/CSharp/CSharp.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@ void MethodsWithDefaultValues::defaultVoidStar(void* ptr)
556556
{
557557
}
558558

559+
void MethodsWithDefaultValues::defaultFunctionPointer(void(*functionPtr)(int p))
560+
{
561+
}
562+
559563
void MethodsWithDefaultValues::defaultValueType(QGenericArgument valueType)
560564
{
561565
}

tests/CSharp/CSharp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ class DLL_API MethodsWithDefaultValues : public Quux
393393
~MethodsWithDefaultValues();
394394
void defaultPointer(Foo* ptr1 = 0, Foo* ptr2 = nullptr);
395395
void defaultVoidStar(void* ptr = 0);
396+
void defaultFunctionPointer(void(*functionPtr)(int p) = nullptr);
396397
void defaultValueType(QGenericArgument valueType = QGenericArgument());
397398
void defaultChar(char c = 'a');
398399
void defaultEmptyChar(char c = 0);

0 commit comments

Comments
 (0)