From 973fdafdfe06d9ed9aca06650263e0c3b0fb08d8 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Tue, 22 Aug 2017 17:28:14 +0300 Subject: [PATCH] Generated properties from setters returning Booleans. Signed-off-by: Dimitar Dobrev --- src/Generator/Generators/CLI/CLISources.cs | 3 +- .../Generators/CSharp/CSharpSources.cs | 49 +++++++++---------- .../Passes/GetterSetterToPropertyPass.cs | 5 +- tests/Common/Common.Tests.cs | 6 +++ tests/Common/Common.cpp | 37 +++++++++++++- tests/Common/Common.h | 13 ++++- 6 files changed, 83 insertions(+), 30 deletions(-) diff --git a/src/Generator/Generators/CLI/CLISources.cs b/src/Generator/Generators/CLI/CLISources.cs index 065b438f..3b8662cd 100644 --- a/src/Generator/Generators/CLI/CLISources.cs +++ b/src/Generator/Generators/CLI/CLISources.cs @@ -372,7 +372,8 @@ namespace CppSharp.Generators.CLI if (decl is Function && !isIndexer) { var func = decl as Function; - GenerateFunctionCall(func, @class); + var @void = new BuiltinType(PrimitiveType.Void); + GenerateFunctionCall(func, @class, @void); } else { diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 5738ca12..9b96afb6 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -817,15 +817,17 @@ namespace CppSharp.Generators.CSharp param.Name = "&" + param.Name; var parameters = new List { param }; + var @void = new QualifiedType(new BuiltinType(PrimitiveType.Void)); if (property.SetMethod.SynthKind == FunctionSynthKind.AbstractImplCall) GenerateVirtualPropertyCall(property.SetMethod, @class.BaseClass, - property, parameters); + property, parameters, @void); else if (property.SetMethod.IsVirtual) - GenerateVirtualPropertyCall(property.SetMethod, @class, property, parameters); + GenerateVirtualPropertyCall(property.SetMethod, @class, + property, parameters, @void); else if (property.SetMethod.OperatorKind == CXXOperatorKind.Subscript) GenerateIndexerSetter(property.SetMethod); else - GenerateInternalFunctionCall(property.SetMethod, parameters); + GenerateInternalFunctionCall(property.SetMethod, parameters, @void); } private void GenerateFieldSetter(Field field, Class @class) @@ -1597,20 +1599,29 @@ namespace CppSharp.Generators.CSharp } } - var hasReturn = !method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void); + bool isVoid = method.OriginalReturnType.Type.Desugar().IsPrimitiveType( + PrimitiveType.Void); + var property = ((Class) method.Namespace).Properties.Find( + p => p.GetMethod == method || p.SetMethod == method); + bool isSetter = property != null && property.SetMethod == method; + var hasReturn = !isVoid && !isSetter; if (hasReturn) - Write("var {0} = ", Helpers.ReturnIdentifier); + Write($"var {Helpers.ReturnIdentifier} = "); - if (method.IsGenerated) + Write($"{Helpers.TargetIdentifier}."); + string marshalsCode = string.Join(", ", marshals); + if (property == null) { - WriteLine("{0}.{1}({2});", Helpers.TargetIdentifier, - method.Name, string.Join(", ", marshals)); + Write($"{method.Name}({marshalsCode})"); } else { - InvokeProperty(method, marshals); + Write($"{property.Name}"); + if (isSetter) + Write($" = {marshalsCode}"); } + WriteLine(";"); if (hasReturn) { @@ -1649,27 +1660,13 @@ namespace CppSharp.Generators.CSharp } } + if (!isVoid && isSetter) + WriteLine("return false;"); + for (var i = 0; i < numBlocks; ++i) WriteCloseBraceIndent(); } - private void InvokeProperty(Declaration method, IEnumerable marshals) - { - var property = ((Class) method.Namespace).Properties.FirstOrDefault( - p => p.GetMethod == method); - if (property == null) - { - property = ((Class) method.Namespace).Properties.First( - p => p.SetMethod == method); - WriteLine("{0}.{1} = {2};", Helpers.TargetIdentifier, property.Name, - string.Join(", ", marshals)); - } - else - { - WriteLine("{0}.{1};", Helpers.TargetIdentifier, property.Name); - } - } - private void GenerateVTableMethodDelegates(Class @class, Method method) { PushBlock(BlockKind.VTableDelegate); diff --git a/src/Generator/Passes/GetterSetterToPropertyPass.cs b/src/Generator/Passes/GetterSetterToPropertyPass.cs index f5b4183a..4cce316b 100644 --- a/src/Generator/Passes/GetterSetterToPropertyPass.cs +++ b/src/Generator/Passes/GetterSetterToPropertyPass.cs @@ -247,7 +247,10 @@ namespace CppSharp.Passes private void DistributeMethod(Method method) { - if (method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) + Type returnType = method.OriginalReturnType.Type.Desugar(); + if ((returnType.IsPrimitiveType(PrimitiveType.Void) || + returnType.IsPrimitiveType(PrimitiveType.Bool)) && + method.Parameters.Any(p => p.Kind == ParameterKind.Regular)) { if (method.Parameters.Count == 1) setters.Add(method); diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 34eb8c87..9a8ee54f 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -473,6 +473,12 @@ public class CommonTests : GeneratorTestFixture prop.GetterAndSetterWithTheSameName = 25; Assert.That(prop.GetterAndSetterWithTheSameName, Is.EqualTo(25)); + + prop.SetterReturnsBoolean = 35; + Assert.That(prop.SetterReturnsBoolean, Is.EqualTo(35)); + + prop.VirtualSetterReturnsBoolean = 45; + Assert.That(prop.VirtualSetterReturnsBoolean, Is.EqualTo(45)); } [Test] diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 69e09b2f..14da9a4a 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -489,7 +489,8 @@ std::string& HasStdString::getStdString() return s; } -TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0), _getterAndSetterWithTheSameName(0) +TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0), + _getterAndSetterWithTheSameName(0), _setterReturnsBoolean(0), _virtualSetterReturnsBoolean(0) { } @@ -536,6 +537,30 @@ void TestProperties::set(int value) { } +int TestProperties::setterReturnsBoolean() +{ + return _setterReturnsBoolean; +} + +bool TestProperties::setterReturnsBoolean(int value) +{ + bool changed = _setterReturnsBoolean != value; + _setterReturnsBoolean = value; + return changed; +} + +int TestProperties::virtualSetterReturnsBoolean() +{ + return _virtualSetterReturnsBoolean; +} + +bool TestProperties::setVirtualSetterReturnsBoolean(int value) +{ + bool changed = _virtualSetterReturnsBoolean != value; + _virtualSetterReturnsBoolean = value; + return changed; +} + HasOverridenSetter::HasOverridenSetter() { } @@ -544,6 +569,16 @@ void HasOverridenSetter::setVirtual(bool value) { } +int HasOverridenSetter::virtualSetterReturnsBoolean() +{ + return TestProperties::virtualSetterReturnsBoolean(); +} + +bool HasOverridenSetter::setVirtualSetterReturnsBoolean(int value) +{ + return TestProperties::setVirtualSetterReturnsBoolean(value); +} + TypeMappedIndex::TypeMappedIndex() { } diff --git a/tests/Common/Common.h b/tests/Common/Common.h index b265522a..14c7b90e 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -590,10 +590,18 @@ public: void getterAndSetterWithTheSameName(int value); void set(int value); + + int setterReturnsBoolean(); + bool setterReturnsBoolean(int value); + + virtual int virtualSetterReturnsBoolean(); + virtual bool setVirtualSetterReturnsBoolean(int value); private: int FieldValue; double _refToPrimitiveInSetter; int _getterAndSetterWithTheSameName; + int _setterReturnsBoolean; + int _virtualSetterReturnsBoolean; }; class DLL_API HasOverridenSetter : public TestProperties @@ -601,6 +609,9 @@ class DLL_API HasOverridenSetter : public TestProperties public: HasOverridenSetter(); void setVirtual(bool value); + + int virtualSetterReturnsBoolean() override; + bool setVirtualSetterReturnsBoolean(int value) override; }; class DLL_API TypeMappedIndex @@ -1368,4 +1379,4 @@ inline namespace InlineNamespace { } -} \ No newline at end of file +}