Browse Source

Fix properties when a setter precedes a getter

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

58
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -136,15 +136,16 @@ namespace CppSharp.Passes @@ -136,15 +136,16 @@ namespace CppSharp.Passes
{
Type underlyingType = GetUnderlyingType(type);
Class @class = (Class) method.Namespace;
Property property = @class.Properties.Find(
p => p.Field == null &&
((!isSetter && p.HasSetter && p.Name == name) ||
(isSetter && p.HasGetter &&
GetReadWritePropertyName(p.GetMethod, name) == name)) &&
((p.HasGetter &&
GetUnderlyingType(p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
(p.HasSetter &&
GetUnderlyingType(p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType)))) ??
((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) ||
(isSetter && p.GetMethod?.IsStatic == method.IsStatic)) &&
((p.HasGetter && GetUnderlyingType(
p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
(p.HasSetter && GetUnderlyingType(
p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) &&
Match(p, name)) ??
new Property { Name = name, QualifiedType = type };
if (property.Namespace == null)
@ -160,13 +161,43 @@ namespace CppSharp.Passes @@ -160,13 +161,43 @@ namespace CppSharp.Passes
(int) method.Access);
}
property.Name = property.OriginalName = name;
method.GenerationKind = GenerationKind.Internal;
if (method.ExplicitInterfaceImpl != null)
property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl;
return property;
}
private static bool Match(Property property, string name)
{
if (string.IsNullOrEmpty(name))
return false;
if (property.Name == name)
return true;
if (property.Name == RemovePrefix(name))
return true;
if (RemovePrefix(property.Name) == name)
{
property.Name = property.OriginalName = name;
return true;
}
return property.SetMethod != null &&
GetPropertyNameFromSetter(property.SetMethod.Name) == name;
}
private static string RemovePrefix(string identifier)
{
if (string.IsNullOrEmpty(identifier))
return identifier;
string name = GetPropertyName(identifier);
return name.StartsWith("is", StringComparison.Ordinal) && name != "is" ?
char.ToLowerInvariant(name[2]) + name.Substring(3) : name;
}
private static void ProcessProperties(Class @class, IEnumerable<Property> newProperties)
{
foreach (Property property in newProperties)
@ -275,17 +306,6 @@ namespace CppSharp.Passes @@ -275,17 +306,6 @@ namespace CppSharp.Passes
}
}
private static string GetReadWritePropertyName(INamedDecl getter, string afterSet)
{
string name = GetPropertyName(getter.Name);
if (name != afterSet && name.StartsWith("is", StringComparison.Ordinal) &&
name != "is")
{
name = char.ToLowerInvariant(name[2]) + name.Substring(3);
}
return name;
}
private static Type GetUnderlyingType(QualifiedType type)
{
TagType tagType = type.Type as TagType;

9
src/Generator/Passes/MultipleInheritancePass.cs

@ -118,16 +118,19 @@ namespace CppSharp.Passes @@ -118,16 +118,19 @@ namespace CppSharp.Passes
if (@interface.Bases.Count == 0)
{
QualifiedType intPtr = new QualifiedType(
new BuiltinType(PrimitiveType.IntPtr));
var instance = new Property
{
{
Namespace = @interface,
Name = Helpers.InstanceIdentifier,
QualifiedType = new QualifiedType(new BuiltinType(PrimitiveType.IntPtr)),
QualifiedType = intPtr,
GetMethod = new Method
{
Name = Helpers.InstanceIdentifier,
SynthKind = FunctionSynthKind.InterfaceInstance,
Namespace = @interface
Namespace = @interface,
OriginalReturnType = intPtr
}
};

9
tests/Common/Common.cpp

@ -642,6 +642,15 @@ void TestProperties::setStartWithVerb(int value) @@ -642,6 +642,15 @@ void TestProperties::setStartWithVerb(int value)
{
}
void TestProperties::setSetterBeforeGetter(bool value)
{
}
bool TestProperties::isSetterBeforeGetter()
{
return true;
}
bool TestProperties::contains(char c)
{
return true;

3
tests/Common/Common.h

@ -620,6 +620,9 @@ public: @@ -620,6 +620,9 @@ public:
int startWithVerb();
void setStartWithVerb(int value);
void setSetterBeforeGetter(bool value);
bool isSetterBeforeGetter();
bool contains(char c);
bool contains(const char* str);
private:

Loading…
Cancel
Save