From 23fab707b56cce13cf4628d7288d2b4115a641fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Matos?= Date: Sun, 5 Apr 2020 18:05:18 +0100 Subject: [PATCH] Fix FieldToProperty pass to ignore non-public properties in C++ generator. --- src/AST/TypeExtensions.cs | 8 +++ src/Generator/Passes/FieldToPropertyPass.cs | 57 ++++++++++++--------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index 17fbdcb6..4ffc8ea1 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -346,6 +346,14 @@ 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) { Type desugared = type.Type.Desugar(); diff --git a/src/Generator/Passes/FieldToPropertyPass.cs b/src/Generator/Passes/FieldToPropertyPass.cs index 2e144fd9..b22a38e1 100644 --- a/src/Generator/Passes/FieldToPropertyPass.cs +++ b/src/Generator/Passes/FieldToPropertyPass.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using CppSharp.AST; +using CppSharp.AST.Extensions; using CppSharp.Generators; namespace CppSharp.Passes @@ -38,6 +39,12 @@ namespace CppSharp.Passes if (ASTUtils.CheckIgnoreField(field)) return false; + if (Options.GeneratorKind == GeneratorKind.CPlusPlus) + { + if (field.Access != AccessSpecifier.Public) + return false; + } + var @class = field.Namespace as Class; if (@class == null) return false; @@ -91,31 +98,35 @@ namespace CppSharp.Passes 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.SetMethod = setter; - @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); + } } } }