Browse Source

Fix regressed virtual read-only properties with setters in subclasses

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1261/head
Dimitar Dobrev 6 years ago
parent
commit
e11057d71d
  1. 58
      src/Generator/Passes/GetterSetterToPropertyPass.cs
  2. 7
      tests/Common/Common.Tests.cs
  3. 14
      tests/Common/Common.cpp
  4. 5
      tests/Common/Common.h

58
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -143,27 +143,8 @@ namespace CppSharp.Passes
{ {
foreach (var property in newProperties) foreach (var property in newProperties)
{ {
if (property.IsOverride) ProcessOverridden(@class, property);
{
Property baseProperty = GetBaseProperty(@class, property);
if (baseProperty == null)
{
if (property.SetMethod != null)
{
property.SetMethod.GenerationKind = GenerationKind.Generate;
property.SetMethod = null;
}
else
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
property.GetMethod = null;
}
}
else if (property.GetMethod == null && baseProperty.SetMethod != null)
property.GetMethod = baseProperty.GetMethod;
else if (property.SetMethod == null)
property.SetMethod = baseProperty.SetMethod;
}
if (property.GetMethod == null) if (property.GetMethod == null)
{ {
if (property.SetMethod != null) if (property.SetMethod != null)
@ -185,14 +166,45 @@ namespace CppSharp.Passes
} }
} }
private static void ProcessOverridden(Class @class, Property property)
{
if (!property.IsOverride)
return;
Property baseProperty = GetBaseProperty(@class, property);
if (baseProperty == null)
{
if (property.SetMethod != null)
{
property.SetMethod.GenerationKind = GenerationKind.Generate;
property.SetMethod = null;
}
else
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
property.GetMethod = null;
}
}
else if (property.GetMethod == null && baseProperty.SetMethod != null)
property.GetMethod = baseProperty.GetMethod;
else if (property.SetMethod == null || baseProperty.SetMethod == null)
{
if (property.SetMethod != null)
property.SetMethod.GenerationKind = GenerationKind.Generate;
property.SetMethod = baseProperty.SetMethod;
}
}
private static Property GetBaseProperty(Class @class, Property @override) private static Property GetBaseProperty(Class @class, Property @override)
{ {
foreach (var @base in @class.Bases) foreach (var @base in @class.Bases)
{ {
Class baseClass = @base.Class.OriginalClass ?? @base.Class; Class baseClass = @base.Class.OriginalClass ?? @base.Class;
Property baseProperty = baseClass.Properties.Find(p => Property baseProperty = baseClass.Properties.Find(p =>
(@override.GetMethod != null && @override.GetMethod.BaseMethod == p.GetMethod) || (@override.GetMethod?.IsOverride == true &&
(@override.SetMethod != null && @override.SetMethod.BaseMethod == p.SetMethod) || @override.GetMethod.BaseMethod == p.GetMethod) ||
(@override.SetMethod?.IsOverride == true &&
@override.SetMethod.BaseMethod == p.SetMethod) ||
(@override.Field != null && @override.Field == p.Field)); (@override.Field != null && @override.Field == p.Field));
if (baseProperty != null) if (baseProperty != null)
return baseProperty; return baseProperty;

7
tests/Common/Common.Tests.cs

@ -522,6 +522,13 @@ public class CommonTests : GeneratorTestFixture
Assert.That(prop.Get32Bit, Is.EqualTo(10)); Assert.That(prop.Get32Bit, Is.EqualTo(10));
Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty)); Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty));
Assert.That(prop.VirtualGetter, Is.EqualTo(15));
}
using (var prop = new HasOverridenSetter())
{
Assert.That(prop.VirtualGetter, Is.EqualTo(20));
prop.SetVirtualGetter(5);
} }
} }

14
tests/Common/Common.cpp

@ -628,6 +628,11 @@ bool TestProperties::empty()
return false; return false;
} }
int TestProperties::virtualGetter()
{
return 15;
}
HasOverridenSetter::HasOverridenSetter() HasOverridenSetter::HasOverridenSetter()
{ {
} }
@ -646,6 +651,15 @@ bool HasOverridenSetter::setVirtualSetterReturnsBoolean(int value)
return TestProperties::setVirtualSetterReturnsBoolean(value); return TestProperties::setVirtualSetterReturnsBoolean(value);
} }
int HasOverridenSetter::virtualGetter()
{
return 20;
}
void HasOverridenSetter::setVirtualGetter(int value)
{
}
TypeMappedIndex::TypeMappedIndex() TypeMappedIndex::TypeMappedIndex()
{ {
} }

5
tests/Common/Common.h

@ -614,6 +614,8 @@ public:
int get32Bit(); int get32Bit();
bool isEmpty(); bool isEmpty();
bool empty(); bool empty();
virtual int virtualGetter();
private: private:
int FieldValue; int FieldValue;
double _refToPrimitiveInSetter; double _refToPrimitiveInSetter;
@ -630,6 +632,9 @@ public:
int virtualSetterReturnsBoolean() override; int virtualSetterReturnsBoolean() override;
bool setVirtualSetterReturnsBoolean(int value) override; bool setVirtualSetterReturnsBoolean(int value) override;
int virtualGetter() override;
void setVirtualGetter(int value);
}; };
class DLL_API TypeMappedIndex class DLL_API TypeMappedIndex

Loading…
Cancel
Save