Browse Source

And a new getter to setter property detection mode option.

emscripten-ci
Joao Matos 2 years ago
parent
commit
714f52e683
  1. 5
      src/Generator/Driver.cs
  2. 7
      src/Generator/Options.cs
  3. 71
      src/Generator/Passes/GetterSetterToPropertyPass.cs

5
src/Generator/Driver.cs

@ -290,9 +290,10 @@ namespace CppSharp @@ -290,9 +290,10 @@ namespace CppSharp
passes.AddPass(new DelegatesPass());
}
if (Options.GeneratorKind != GeneratorKind.C)
if (Options.GeneratorKind != GeneratorKind.C &&
Options.PropertyDetectionMode != PropertyDetectionMode.Disable)
{
passes.AddPass(new GetterSetterToPropertyPass());
passes.AddPass(new GetterSetterToPropertyPass(Options.PropertyDetectionMode));
}
passes.AddPass(new StripUnusedSystemTypesPass());

7
src/Generator/Options.cs

@ -5,6 +5,7 @@ using System.Linq; @@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Passes;
namespace CppSharp
{
@ -240,8 +241,14 @@ namespace CppSharp @@ -240,8 +241,14 @@ namespace CppSharp
/// </summary>
public HashSet<string> ExplicitlyPatchedVirtualFunctions { get; }
[Obsolete("Use PropertyDetectionMode instead.")]
public bool UsePropertyDetectionHeuristics { get; set; } = true;
/// <summary>
/// Sets the property detection mode used by GetterSetterToPropertyPass.
/// </summary>
public PropertyDetectionMode PropertyDetectionMode { get; set; } = PropertyDetectionMode.Aggressive;
/// <summary>
/// Experimental option that makes the C/C++ generator generate some extra data storage
/// on the generated instance, for supporting higher-level binding of the code.

71
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -7,12 +7,25 @@ using System.Reflection; @@ -7,12 +7,25 @@ using System.Reflection;
using System.Text;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators;
using Type = CppSharp.AST.Type;
namespace CppSharp.Passes
{
public enum PropertyDetectionMode
{
/// Disable detection for all properties.
Disable,
/// Use conservative detection (only methods starting with get/set).
Conservative,
/// Use advanced heuristics based on builtin english words/verbs list.
Aggressive
}
public class GetterSetterToPropertyPass : TranslationUnitPass
{
public PropertyDetectionMode Policy { get; internal set; }
static GetterSetterToPropertyPass()
{
LoadVerbs();
@ -39,8 +52,11 @@ namespace CppSharp.Passes @@ -39,8 +52,11 @@ namespace CppSharp.Passes
return assembly.GetManifestResourceStream(resources[0]);
}
public GetterSetterToPropertyPass()
=> VisitOptions.ResetFlags(VisitFlags.ClassTemplateSpecializations);
public GetterSetterToPropertyPass(PropertyDetectionMode policy = PropertyDetectionMode.Aggressive)
{
Policy = policy;
VisitOptions.ResetFlags(VisitFlags.ClassTemplateSpecializations);
}
public override bool VisitClassDecl(Class @class)
{
@ -86,30 +102,37 @@ namespace CppSharp.Passes @@ -86,30 +102,37 @@ namespace CppSharp.Passes
private IEnumerable<Property> CleanUp(Class @class, List<Property> properties)
{
if (!Options.UsePropertyDetectionHeuristics)
return properties;
for (int i = properties.Count - 1; i >= 0; i--)
switch (Policy)
{
Property property = properties[i];
if (property.HasSetter || property.IsExplicitlyGenerated)
continue;
string firstWord = GetFirstWord(property.GetMethod.Name);
if (firstWord.Length < property.GetMethod.Name.Length &&
Match(firstWord, new[] { "get", "is", "has" }))
continue;
if (Match(firstWord, new[] { "to", "new", "on" }) ||
Verbs.Contains(firstWord))
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
properties.RemoveAt(i);
}
case PropertyDetectionMode.Disable:
return null;
case PropertyDetectionMode.Conservative:
return properties;
case PropertyDetectionMode.Aggressive:
for (int i = properties.Count - 1; i >= 0; i--)
{
Property property = properties[i];
if (property.HasSetter || property.IsExplicitlyGenerated)
continue;
string firstWord = GetFirstWord(property.GetMethod.Name);
if (firstWord.Length < property.GetMethod.Name.Length &&
Match(firstWord, new[] { "get", "is", "has" }))
continue;
if (Match(firstWord, new[] { "to", "new", "on" }) ||
Verbs.Contains(firstWord))
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
properties.RemoveAt(i);
}
}
return properties;
default:
throw new ArgumentOutOfRangeException();
}
return properties;
}
private static void CreateOrUpdateProperty(List<Property> properties, Method method,

Loading…
Cancel
Save