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
{ {
Type underlyingType = GetUnderlyingType(type); Type underlyingType = GetUnderlyingType(type);
Class @class = (Class) method.Namespace; Class @class = (Class) method.Namespace;
Property property = @class.Properties.Find( Property property = @class.Properties.Find(
p => p.Field == null && p => p.Field == null &&
((!isSetter && p.HasSetter && p.Name == name) || ((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) ||
(isSetter && p.HasGetter && (isSetter && p.GetMethod?.IsStatic == method.IsStatic)) &&
GetReadWritePropertyName(p.GetMethod, name) == name)) && ((p.HasGetter && GetUnderlyingType(
((p.HasGetter && p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
GetUnderlyingType(p.GetMethod.OriginalReturnType).Equals(underlyingType)) || (p.HasSetter && GetUnderlyingType(
(p.HasSetter && p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) &&
GetUnderlyingType(p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType)))) ?? Match(p, name)) ??
new Property { Name = name, QualifiedType = type }; new Property { Name = name, QualifiedType = type };
if (property.Namespace == null) if (property.Namespace == null)
@ -160,13 +161,43 @@ namespace CppSharp.Passes
(int) method.Access); (int) method.Access);
} }
property.Name = property.OriginalName = name;
method.GenerationKind = GenerationKind.Internal; method.GenerationKind = GenerationKind.Internal;
if (method.ExplicitInterfaceImpl != null) if (method.ExplicitInterfaceImpl != null)
property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl; property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl;
return property; 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) private static void ProcessProperties(Class @class, IEnumerable<Property> newProperties)
{ {
foreach (Property property in newProperties) foreach (Property property in newProperties)
@ -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) private static Type GetUnderlyingType(QualifiedType type)
{ {
TagType tagType = type.Type as TagType; TagType tagType = type.Type as TagType;

9
src/Generator/Passes/MultipleInheritancePass.cs

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

9
tests/Common/Common.cpp

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

3
tests/Common/Common.h

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

Loading…
Cancel
Save