Browse Source

Split generation from addition of properties

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1261/head
Dimitar Dobrev 6 years ago
parent
commit
d986132743
  1. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 112
      src/Generator/Passes/GetterSetterToPropertyPass.cs

2
src/Generator/Generators/CSharp/CSharpSources.cs

@ -629,7 +629,7 @@ namespace CppSharp.Generators.CSharp
foreach (var method in @class.Methods) foreach (var method in @class.Methods)
{ {
if (ASTUtils.CheckIgnoreMethod(method)) if (!method.IsGenerated || ASTUtils.CheckIgnoreMethod(method))
continue; continue;
if (method.IsConstructor) if (method.IsConstructor)

112
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -68,7 +68,7 @@ namespace CppSharp.Passes
protected virtual IEnumerable<Property> GenerateProperties(Class @class) protected virtual IEnumerable<Property> GenerateProperties(Class @class)
{ {
var newProperties = new List<Property>(); var properties = new List<Property>();
foreach (Method method in @class.Methods.Where( foreach (Method method in @class.Methods.Where(
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated && m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
m.SynthKind != FunctionSynthKind.DefaultValueOverload && m.SynthKind != FunctionSynthKind.DefaultValueOverload &&
@ -78,39 +78,29 @@ namespace CppSharp.Passes
if (IsGetter(method)) if (IsGetter(method))
{ {
string name = GetPropertyName(method.Name); string name = GetPropertyName(method.Name);
QualifiedType type = method.OriginalReturnType; GetProperty(properties, method, name, method.OriginalReturnType);
Property property = GetProperty(method, name, type);
if (!property.HasGetter)
{
property.GetMethod = method;
property.QualifiedType = method.OriginalReturnType;
newProperties.Add(property);
}
else
method.GenerationKind = GenerationKind.Generate;
continue; continue;
} }
if (IsSetter(method)) if (IsSetter(method))
{ {
string name = GetPropertyNameFromSetter(method.Name); string name = GetPropertyNameFromSetter(method.Name);
QualifiedType type = method.Parameters.First(p => p.Kind == ParameterKind.Regular).QualifiedType; QualifiedType type = method.Parameters.First(
Property property = GetProperty(method, name, type, true); p => p.Kind == ParameterKind.Regular).QualifiedType;
property.SetMethod = method; GetProperty(properties, method, name, type, true);
newProperties.Add(property);
} }
} }
return CleanUp(@class, newProperties); return CleanUp(@class, properties);
} }
private IEnumerable<Property> CleanUp(Class @class, List<Property> newProperties) private IEnumerable<Property> CleanUp(Class @class, List<Property> properties)
{ {
if (!Options.UsePropertyDetectionHeuristics) if (!Options.UsePropertyDetectionHeuristics)
return newProperties; return properties;
for (int i = newProperties.Count - 1; i >= 0; i--) for (int i = properties.Count - 1; i >= 0; i--)
{ {
Property property = newProperties[i]; Property property = properties[i];
if (property.HasSetter) if (property.HasSetter)
continue; continue;
@ -124,47 +114,46 @@ namespace CppSharp.Passes
{ {
property.GetMethod.GenerationKind = GenerationKind.Generate; property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property); @class.Properties.Remove(property);
newProperties.RemoveAt(i); properties.RemoveAt(i);
} }
} }
return newProperties; return properties;
} }
private static Property GetProperty(Method method, string name, private static void GetProperty(List<Property> properties, Method method,
QualifiedType type, bool isSetter = false) string name, QualifiedType type, bool isSetter = false)
{ {
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 = properties.Find(
p => p.Field == null && p => p.Field == null &&
((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) || ((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) ||
(isSetter && p.GetMethod?.IsStatic == method.IsStatic)) && (isSetter && p.GetMethod?.IsStatic == method.IsStatic)) &&
((p.HasGetter && GetUnderlyingType( ((p.HasGetter && GetUnderlyingType(
p.GetMethod.OriginalReturnType).Equals(underlyingType)) || p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
(p.HasSetter && GetUnderlyingType( (p.HasSetter && GetUnderlyingType(
p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) && p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) &&
Match(p, name)) ?? Match(p, name));
new Property { Name = name, QualifiedType = type };
if (property == null)
if (property.Namespace == null) properties.Add(property = new Property { Name = name, QualifiedType = type });
{
property.Namespace = method.Namespace; if (isSetter)
property.Access = method.Access; property.SetMethod = method;
@class.Properties.Add(property);
}
else else
{ {
property.Access = (AccessSpecifier) Math.Max( property.GetMethod = method;
(int) (property.GetMethod ?? property.SetMethod).Access, property.QualifiedType = method.OriginalReturnType;
(int) method.Access);
} }
method.GenerationKind = GenerationKind.Internal; property.Access = (AccessSpecifier) Math.Max(
(int) (property.GetMethod ?? property.SetMethod).Access,
(int) method.Access);
if (method.ExplicitInterfaceImpl != null) if (method.ExplicitInterfaceImpl != null)
property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl; property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl;
return property;
} }
private static bool Match(Property property, string name) private static bool Match(Property property, string name)
@ -198,37 +187,30 @@ namespace CppSharp.Passes
char.ToLowerInvariant(name[2]) + name.Substring(3) : name; 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> properties)
{ {
foreach (Property property in newProperties) foreach (Property property in properties)
{ {
ProcessOverridden(@class, property); ProcessOverridden(@class, property);
if (!property.HasGetter) if (!property.HasGetter)
{
if (property.HasSetter)
property.SetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
continue; continue;
}
if (!property.HasSetter && if (!property.HasSetter &&
@class.GetOverloads(property.GetMethod).Any( @class.GetOverloads(property.GetMethod).Any(
m => m != property.GetMethod && !m.Ignore)) m => m != property.GetMethod && !m.Ignore))
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
continue; continue;
}
Property conflict = newProperties.LastOrDefault( Property conflict = properties.LastOrDefault(
p => p.Name == property.Name && p != property && p => p.Name == property.Name && p != property &&
p.ExplicitInterfaceImpl == property.ExplicitInterfaceImpl); p.ExplicitInterfaceImpl == property.ExplicitInterfaceImpl);
if (conflict != null && conflict.GetMethod != null) if (conflict?.GetMethod != null)
{
conflict.GetMethod.GenerationKind = GenerationKind.Generate;
conflict.GetMethod = null; conflict.GetMethod = null;
}
property.GetMethod.GenerationKind = GenerationKind.Internal;
if (property.SetMethod != null)
property.SetMethod.GenerationKind = GenerationKind.Internal;
property.Namespace = @class;
@class.Properties.Add(property);
RenameConflictingMethods(@class, property); RenameConflictingMethods(@class, property);
CombineComments(property); CombineComments(property);
} }
@ -243,24 +225,14 @@ namespace CppSharp.Passes
if (baseProperty == null) if (baseProperty == null)
{ {
if (property.HasSetter) if (property.HasSetter)
{
property.SetMethod.GenerationKind = GenerationKind.Generate;
property.SetMethod = null; property.SetMethod = null;
}
else else
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
property.GetMethod = null; property.GetMethod = null;
}
} }
else if (!property.HasGetter && baseProperty.HasSetter) else if (!property.HasGetter && baseProperty.HasSetter)
property.GetMethod = baseProperty.GetMethod; property.GetMethod = baseProperty.GetMethod;
else if (!property.HasSetter || !baseProperty.HasSetter) else if (!property.HasSetter || !baseProperty.HasSetter)
{
if (property.HasSetter)
property.SetMethod.GenerationKind = GenerationKind.Generate;
property.SetMethod = baseProperty.SetMethod; property.SetMethod = baseProperty.SetMethod;
}
} }
private static void RenameConflictingMethods(Class @class, Property property) private static void RenameConflictingMethods(Class @class, Property property)

Loading…
Cancel
Save