Browse Source

Fix FieldToProperty pass to ignore non-public properties in C++ generator.

pull/1328/head
João Matos 6 years ago committed by João Matos
parent
commit
23fab707b5
  1. 8
      src/AST/TypeExtensions.cs
  2. 57
      src/Generator/Passes/FieldToPropertyPass.cs

8
src/AST/TypeExtensions.cs

@ -346,6 +346,14 @@
return left.Equals(right); return left.Equals(right);
} }
public static bool IsConstRef(this QualifiedType type)
{
Type desugared = type.Type.Desugar();
Type pointee = desugared.GetFinalPointee().Desugar();
pointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
return desugared.IsReference() && type.IsConst();
}
public static bool IsConstRefToPrimitive(this QualifiedType type) public static bool IsConstRefToPrimitive(this QualifiedType type)
{ {
Type desugared = type.Type.Desugar(); Type desugared = type.Type.Desugar();

57
src/Generator/Passes/FieldToPropertyPass.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators; using CppSharp.Generators;
namespace CppSharp.Passes namespace CppSharp.Passes
@ -38,6 +39,12 @@ namespace CppSharp.Passes
if (ASTUtils.CheckIgnoreField(field)) if (ASTUtils.CheckIgnoreField(field))
return false; return false;
if (Options.GeneratorKind == GeneratorKind.CPlusPlus)
{
if (field.Access != AccessSpecifier.Public)
return false;
}
var @class = field.Namespace as Class; var @class = field.Namespace as Class;
if (@class == null) if (@class == null)
return false; return false;
@ -91,31 +98,35 @@ namespace CppSharp.Passes
SynthKind = FunctionSynthKind.FieldAcessor SynthKind = FunctionSynthKind.FieldAcessor
}; };
var setter = new Method
{
Name = $"set_{field.Name}",
Namespace = @class,
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
Access = field.Access,
AssociatedDeclaration = property,
IsStatic = field.IsStatic,
SynthKind = FunctionSynthKind.FieldAcessor
};
var param = new Parameter
{
Name = "value",
QualifiedType = field.QualifiedType,
Namespace = setter
};
setter.Parameters.Add(param);
property.GetMethod = getter; property.GetMethod = getter;
property.SetMethod = setter;
@class.Methods.Add(getter); @class.Methods.Add(getter);
@class.Methods.Add(setter);
var isSetterInvalid = field.QualifiedType.IsConstRef();
if (!isSetterInvalid)
{
var setter = new Method
{
Name = $"set_{field.Name}",
Namespace = @class,
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
Access = field.Access,
AssociatedDeclaration = property,
IsStatic = field.IsStatic,
SynthKind = FunctionSynthKind.FieldAcessor
};
var param = new Parameter
{
Name = "value",
QualifiedType = field.QualifiedType,
Namespace = setter
};
setter.Parameters.Add(param);
property.SetMethod = setter;
@class.Methods.Add(setter);
}
} }
} }
} }

Loading…
Cancel
Save