Skip to content

Commit 637018f

Browse files
committed
Fixed the generated C# for setters with default parameters.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 1420bd9 commit 637018f

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

+9
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,15 @@ private string OverloadParamNameWithDefValue(Parameter p, ref int index)
24502450

24512451
private void GenerateOverloadCall(Function function)
24522452
{
2453+
if (function.OriginalFunction.GenerationKind == GenerationKind.Internal)
2454+
{
2455+
var property = ((Class) function.Namespace).Properties.First(
2456+
p => p.SetMethod == function.OriginalFunction);
2457+
WriteLine($@"{property.Name} = {ExpressionPrinter.VisitParameter(
2458+
function.Parameters.First(p => p.Kind == ParameterKind.Regular))};");
2459+
return;
2460+
}
2461+
24532462
for (int i = 0, j = 0; i < function.Parameters.Count; i++)
24542463
{
24552464
var parameter = function.Parameters[i];

src/Generator/Passes/HandleDefaultParamValuesPass.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ private bool CheckForDefaultPointer(Type desugared, ref string result)
140140
return true;
141141
}
142142

143-
result = "null";
144-
return true;
143+
return false;
145144
}
146145

147146
private bool? CheckForDefaultConstruct(Type desugared, Expression expression,

tests/CSharp/CSharp.Tests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public void TestUncompilableCode()
6363
{
6464
new Bar(qux).Dispose();
6565
}
66+
using (var quux = new Quux())
67+
{
68+
quux.SetterWithDefaultOverload = null;
69+
quux.SetSetterWithDefaultOverload();
70+
}
6671
using (ComplexType complexType = TestFlag.Flag1)
6772
{
6873
}

tests/CSharp/CSharp.cpp

+25-4
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,44 @@ const Foo& Bar::operator[](int i) const
9898
}
9999

100100

101-
Quux::Quux()
101+
Quux::Quux() : _setterWithDefaultOverload(0)
102102
{
103103

104104
}
105105

106-
Quux::Quux(int i)
106+
Quux::Quux(int i) : Quux()
107107
{
108108

109109
}
110110

111-
Quux::Quux(char c)
111+
Quux::Quux(char c) : Quux()
112112
{
113113

114114
}
115115

116-
Quux::Quux(Foo f)
116+
Quux::Quux(Foo f) : Quux()
117117
{
118118

119119
}
120120

121+
Quux::~Quux()
122+
{
123+
if (_setterWithDefaultOverload)
124+
{
125+
delete _setterWithDefaultOverload;
126+
_setterWithDefaultOverload = 0;
127+
}
128+
}
129+
130+
Foo* Quux::setterWithDefaultOverload()
131+
{
132+
return _setterWithDefaultOverload;
133+
}
121134

135+
void Quux::setSetterWithDefaultOverload(Foo* value)
136+
{
137+
_setterWithDefaultOverload = value;
138+
}
122139

123140
QColor::QColor()
124141
{
@@ -527,6 +544,10 @@ MethodsWithDefaultValues::MethodsWithDefaultValues(QRect* pointer, float f, int
527544
{
528545
}
529546

547+
MethodsWithDefaultValues::~MethodsWithDefaultValues()
548+
{
549+
}
550+
530551
void MethodsWithDefaultValues::defaultPointer(Foo *ptr)
531552
{
532553
}

tests/CSharp/CSharp.h

+7
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ class DLL_API Quux
5151
Quux(int i);
5252
Quux(char c);
5353
Quux(Foo f);
54+
~Quux();
55+
56+
Foo* setterWithDefaultOverload();
57+
void setSetterWithDefaultOverload(Foo* value = new Foo());
58+
5459
private:
5560
int priv;
61+
Foo* _setterWithDefaultOverload;
5662
};
5763

5864
class Bar;
@@ -384,6 +390,7 @@ class DLL_API MethodsWithDefaultValues : public Quux
384390
MethodsWithDefaultValues(float a, Zero b = 0);
385391
MethodsWithDefaultValues(double d, QList<QColor> list = QList<QColor>());
386392
MethodsWithDefaultValues(QRect* pointer, float f = 1, int i = std::numeric_limits<double>::infinity());
393+
~MethodsWithDefaultValues();
387394
void defaultPointer(Foo* ptr = 0);
388395
void defaultVoidStar(void* ptr = 0);
389396
void defaultValueType(QGenericArgument valueType = QGenericArgument());

0 commit comments

Comments
 (0)