From 0464938640e2c2fe8c2145e1b389a9c349204052 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Mon, 15 Oct 2018 05:18:47 +0300 Subject: [PATCH] Fixed the generated C# for setters with default parameters. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpSources.cs | 9 +++++++ .../Passes/HandleDefaultParamValuesPass.cs | 3 +-- tests/CSharp/CSharp.Tests.cs | 5 ++++ tests/CSharp/CSharp.cpp | 25 ++++++++++++++++--- tests/CSharp/CSharp.h | 6 +++++ 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 222a30c3ce..e4a746d5bd 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -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]; diff --git a/src/Generator/Passes/HandleDefaultParamValuesPass.cs b/src/Generator/Passes/HandleDefaultParamValuesPass.cs index fcd0dbb803..46451180ea 100644 --- a/src/Generator/Passes/HandleDefaultParamValuesPass.cs +++ b/src/Generator/Passes/HandleDefaultParamValuesPass.cs @@ -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, diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 4ed3a4692c..65fc3efcbb 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -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) { } diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 8f6b070d66..fe24011753 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -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() { diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index d84d45ef5e..e925afea91 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -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;