Skip to content

Commit

Permalink
Fixed the generated C# for setters with default parameters.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Oct 15, 2018
1 parent 1420bd9 commit 637018f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,15 @@ private string OverloadParamNameWithDefValue(Parameter p, ref int index)

private void GenerateOverloadCall(Function function)
{
if (function.OriginalFunction.GenerationKind == GenerationKind.Internal)
{
var property = ((Class) function.Namespace).Properties.First(
p => p.SetMethod == function.OriginalFunction);
WriteLine($@"{property.Name} = {ExpressionPrinter.VisitParameter(
function.Parameters.First(p => p.Kind == ParameterKind.Regular))};");
return;
}

for (int i = 0, j = 0; i < function.Parameters.Count; i++)
{
var parameter = function.Parameters[i];
Expand Down
3 changes: 1 addition & 2 deletions src/Generator/Passes/HandleDefaultParamValuesPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ private bool CheckForDefaultPointer(Type desugared, ref string result)
return true;
}

result = "null";
return true;
return false;
}

private bool? CheckForDefaultConstruct(Type desugared, Expression expression,
Expand Down
5 changes: 5 additions & 0 deletions tests/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void TestUncompilableCode()
{
new Bar(qux).Dispose();
}
using (var quux = new Quux())
{
quux.SetterWithDefaultOverload = null;
quux.SetSetterWithDefaultOverload();
}
using (ComplexType complexType = TestFlag.Flag1)
{
}
Expand Down
29 changes: 25 additions & 4 deletions tests/CSharp/CSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,44 @@ const Foo& Bar::operator[](int i) const
}


Quux::Quux()
Quux::Quux() : _setterWithDefaultOverload(0)
{

}

Quux::Quux(int i)
Quux::Quux(int i) : Quux()
{

}

Quux::Quux(char c)
Quux::Quux(char c) : Quux()
{

}

Quux::Quux(Foo f)
Quux::Quux(Foo f) : Quux()
{

}

Quux::~Quux()
{
if (_setterWithDefaultOverload)
{
delete _setterWithDefaultOverload;
_setterWithDefaultOverload = 0;
}
}

Foo* Quux::setterWithDefaultOverload()
{
return _setterWithDefaultOverload;
}

void Quux::setSetterWithDefaultOverload(Foo* value)
{
_setterWithDefaultOverload = value;
}

QColor::QColor()
{
Expand Down Expand Up @@ -527,6 +544,10 @@ MethodsWithDefaultValues::MethodsWithDefaultValues(QRect* pointer, float f, int
{
}

MethodsWithDefaultValues::~MethodsWithDefaultValues()
{
}

void MethodsWithDefaultValues::defaultPointer(Foo *ptr)
{
}
Expand Down
7 changes: 7 additions & 0 deletions tests/CSharp/CSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ class DLL_API Quux
Quux(int i);
Quux(char c);
Quux(Foo f);
~Quux();

Foo* setterWithDefaultOverload();
void setSetterWithDefaultOverload(Foo* value = new Foo());

private:
int priv;
Foo* _setterWithDefaultOverload;
};

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

0 comments on commit 637018f

Please sign in to comment.