Added pass for converting fields to getter/setter pairs.

This commit is contained in:
Joao Matos 2016-08-11 11:58:14 +01:00
Родитель 5b73ca9bb2
Коммит 1c72ffbfdf
1 изменённых файлов: 80 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,80 @@
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Passes;
using System.Linq;
namespace MonoManagedToNative.Passes
{
public class FieldToGetterSetterPass : TranslationUnitPass
{
public override bool VisitClassDecl(Class @class)
{
if (@class.CompleteDeclaration != null)
return VisitClassDecl(@class.CompleteDeclaration as Class);
return base.VisitClassDecl(@class);
}
public override bool VisitFieldDecl(Field field)
{
if (!VisitDeclaration(field))
return false;
var @class = field.Namespace as Class;
if (@class == null)
return false;
if (field.Access == AccessSpecifier.Private)
return true;
// Check if we already have a synthetized getter/setter.
//var existing = @class.Methods.FirstOrDefault(
// method => method.Parameters.SingleOrDefault(
// (Parameter p) =>
// {
// Class paramClass;
// if(!p.Type.TryGetClass(out paramClass))
// return false;
// return paramClass == @class;
// }));
//if (existing != null)
// return false;
field.GenerationKind = GenerationKind.None;
var getter = new Method
{
Name = string.Format("get{0}", field.Name),
Namespace = @class,
ReturnType = field.QualifiedType,
Access = field.Access
};
@class.Methods.Add(getter);
var setter = new Method
{
Name = string.Format("set{0}", field.Name),
Namespace = @class,
Access = field.Access,
};
var param = new Parameter
{
Name = "value",
QualifiedType = field.QualifiedType
};
setter.Parameters.Add(param);
@class.Methods.Add(setter);
Log.Debug("Getter/setter created from field: {0}::{1}",
@class.QualifiedOriginalName, field.Name);
return false;
}
}
}