Browse Source

Merge pull request #1028 from rokups/feature/customize-getter-setter-pass

GetterSetterToPropertyPass customization
pull/1033/head
João Matos 8 years ago committed by GitHub
parent
commit
9830302c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/AST/Method.cs
  2. 50
      src/Generator/Library.cs
  3. 2
      src/Generator/Options.cs
  4. 24
      src/Generator/Passes/GetterSetterToPropertyPass.cs
  5. 3
      src/Generator/Passes/verbs.txt

5
src/AST/Method.cs

@ -105,6 +105,7 @@ namespace CppSharp.AST @@ -105,6 +105,7 @@ namespace CppSharp.AST
SynthKind = method.SynthKind;
AdjustedOffset = method.AdjustedOffset;
OverriddenMethods.AddRange(method.OverriddenMethods);
ConvertToProperty = method.ConvertToProperty;
}
public Method(Function function)
@ -172,6 +173,8 @@ namespace CppSharp.AST @@ -172,6 +173,8 @@ namespace CppSharp.AST
public List<Method> OverriddenMethods { get; } = new List<Method>();
public bool ConvertToProperty { get; set; }
public Method GetRootBaseMethod()
{
return BaseMethod == null || BaseMethod.BaseMethod == null ?
@ -185,4 +188,4 @@ namespace CppSharp.AST @@ -185,4 +188,4 @@ namespace CppSharp.AST
private bool? isOverride;
}
}
}

50
src/Generator/Library.cs

@ -4,6 +4,7 @@ using System.Globalization; @@ -4,6 +4,7 @@ using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using CppSharp.AST;
using CppSharp.Passes;
namespace CppSharp
{
@ -351,6 +352,53 @@ namespace CppSharp @@ -351,6 +352,53 @@ namespace CppSharp
}
}
private static IEnumerable<Class> GetClasses(DeclarationContext decl)
{
foreach (var @class in decl.Classes)
{
yield return @class;
foreach (var class2 in GetClasses(@class))
yield return class2;
}
foreach (var ns in decl.Namespaces)
{
foreach (var @class in GetClasses(ns))
yield return @class;
}
}
public static void IgnoreConversionToProperty(this ASTContext context, string pattern)
{
foreach (var unit in context.TranslationUnits)
{
foreach (var @class in GetClasses(unit))
{
foreach (var method in @class.Methods)
{
if (Regex.Match(method.QualifiedLogicalOriginalName, pattern).Success)
method.ExcludeFromPasses.Add(typeof(GetterSetterToPropertyPass));
}
}
}
}
public static void ForceConversionToProperty(this ASTContext context, string pattern)
{
foreach (var unit in context.TranslationUnits)
{
foreach (var @class in GetClasses(unit))
{
foreach (var method in @class.Methods)
{
if (Regex.Match(method.QualifiedLogicalOriginalName, pattern).Success)
method.ConvertToProperty = true;
}
}
}
}
#endregion
#region Module Helpers
@ -379,4 +427,4 @@ namespace CppSharp @@ -379,4 +427,4 @@ namespace CppSharp
#endregion
}
}
}

2
src/Generator/Options.cs

@ -151,6 +151,8 @@ namespace CppSharp @@ -151,6 +151,8 @@ namespace CppSharp
/// </summary>
public HashSet<string> ExplicitlyPatchedVirtualFunctions { get; }
public bool UsePropertyDetectionHeuristics { get; set; } = true;
#endregion
}

24
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -19,11 +19,14 @@ namespace CppSharp.Passes @@ -19,11 +19,14 @@ namespace CppSharp.Passes
private readonly List<Method> setters = new List<Method>();
private readonly List<Method> setMethods = new List<Method>();
private readonly List<Method> nonSetters = new List<Method>();
private bool useHeuristics = true;
public PropertyGenerator(Class @class)
public PropertyGenerator(Class @class, bool useHeuristics)
{
this.useHeuristics = useHeuristics;
foreach (var method in @class.Methods.Where(
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated))
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
!m.ExcludeFromPasses.Contains(typeof(GetterSetterToPropertyPass))))
DistributeMethod(method);
}
@ -259,23 +262,28 @@ namespace CppSharp.Passes @@ -259,23 +262,28 @@ namespace CppSharp.Passes
}
else
{
if (IsGetter(method))
if (method.ConvertToProperty || IsGetter(method))
getters.Add(method);
if (method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType))
nonSetters.Add(method);
}
}
private static bool IsGetter(Method method)
private bool IsGetter(Method method)
{
if (method.IsDestructor ||
(method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) ||
method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType))
return false;
var firstWord = GetFirstWord(method.Name);
return (firstWord.Length < method.Name.Length &&
Match(firstWord, new[] { "get", "is", "has" })) ||
(!Match(firstWord, new[] { "to", "new" }) && !verbs.Contains(firstWord));
if (firstWord.Length < method.Name.Length && Match(firstWord, new[] {"get", "is", "has"}))
return true;
if (useHeuristics && !Match(firstWord, new[] {"to", "new"}) && !verbs.Contains(firstWord))
return true;
return false;
}
private static bool Match(string prefix, IEnumerable<string> prefixes)
@ -354,7 +362,7 @@ namespace CppSharp.Passes @@ -354,7 +362,7 @@ namespace CppSharp.Passes
public override bool VisitClassDecl(Class @class)
{
if (base.VisitClassDecl(@class))
new PropertyGenerator(@class).GenerateProperties();
new PropertyGenerator(@class, Options.UsePropertyDetectionHeuristics).GenerateProperties();
return false;
}
}

3
src/Generator/Passes/verbs.txt

@ -7322,6 +7322,7 @@ ruckle @@ -7322,6 +7322,7 @@ ruckle
ruddle
ruggedize
ruminate
run
runtgenize
ruralise
ruralize
@ -8828,4 +8829,4 @@ youthen @@ -8828,4 +8829,4 @@ youthen
zap
zincify
zindabad
zipping
zipping

Loading…
Cancel
Save