Browse Source

And a new getter to setter property detection mode option.

emscripten-ci
Joao Matos 3 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
passes.AddPass(new DelegatesPass()); 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()); passes.AddPass(new StripUnusedSystemTypesPass());

7
src/Generator/Options.cs

@ -5,6 +5,7 @@ using System.Linq;
using System.Text; using System.Text;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Generators; using CppSharp.Generators;
using CppSharp.Passes;
namespace CppSharp namespace CppSharp
{ {
@ -240,8 +241,14 @@ namespace CppSharp
/// </summary> /// </summary>
public HashSet<string> ExplicitlyPatchedVirtualFunctions { get; } public HashSet<string> ExplicitlyPatchedVirtualFunctions { get; }
[Obsolete("Use PropertyDetectionMode instead.")]
public bool UsePropertyDetectionHeuristics { get; set; } = true; public bool UsePropertyDetectionHeuristics { get; set; } = true;
/// <summary>
/// Sets the property detection mode used by GetterSetterToPropertyPass.
/// </summary>
public PropertyDetectionMode PropertyDetectionMode { get; set; } = PropertyDetectionMode.Aggressive;
/// <summary> /// <summary>
/// Experimental option that makes the C/C++ generator generate some extra data storage /// 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. /// 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;
using System.Text; using System.Text;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Generators;
using Type = CppSharp.AST.Type; using Type = CppSharp.AST.Type;
namespace CppSharp.Passes 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 class GetterSetterToPropertyPass : TranslationUnitPass
{ {
public PropertyDetectionMode Policy { get; internal set; }
static GetterSetterToPropertyPass() static GetterSetterToPropertyPass()
{ {
LoadVerbs(); LoadVerbs();
@ -39,8 +52,11 @@ namespace CppSharp.Passes
return assembly.GetManifestResourceStream(resources[0]); return assembly.GetManifestResourceStream(resources[0]);
} }
public GetterSetterToPropertyPass() public GetterSetterToPropertyPass(PropertyDetectionMode policy = PropertyDetectionMode.Aggressive)
=> VisitOptions.ResetFlags(VisitFlags.ClassTemplateSpecializations); {
Policy = policy;
VisitOptions.ResetFlags(VisitFlags.ClassTemplateSpecializations);
}
public override bool VisitClassDecl(Class @class) public override bool VisitClassDecl(Class @class)
{ {
@ -86,30 +102,37 @@ namespace CppSharp.Passes
private IEnumerable<Property> CleanUp(Class @class, List<Property> properties) private IEnumerable<Property> CleanUp(Class @class, List<Property> properties)
{ {
if (!Options.UsePropertyDetectionHeuristics) switch (Policy)
return properties;
for (int i = properties.Count - 1; i >= 0; i--)
{ {
Property property = properties[i]; case PropertyDetectionMode.Disable:
if (property.HasSetter || property.IsExplicitlyGenerated) return null;
continue; case PropertyDetectionMode.Conservative:
return properties;
string firstWord = GetFirstWord(property.GetMethod.Name); case PropertyDetectionMode.Aggressive:
if (firstWord.Length < property.GetMethod.Name.Length && for (int i = properties.Count - 1; i >= 0; i--)
Match(firstWord, new[] { "get", "is", "has" })) {
continue; Property property = properties[i];
if (property.HasSetter || property.IsExplicitlyGenerated)
if (Match(firstWord, new[] { "to", "new", "on" }) || continue;
Verbs.Contains(firstWord))
{ string firstWord = GetFirstWord(property.GetMethod.Name);
property.GetMethod.GenerationKind = GenerationKind.Generate; if (firstWord.Length < property.GetMethod.Name.Length &&
@class.Properties.Remove(property); Match(firstWord, new[] { "get", "is", "has" }))
properties.RemoveAt(i); 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, private static void CreateOrUpdateProperty(List<Property> properties, Method method,

Loading…
Cancel
Save