Skip to content

Commit 23fab70

Browse files
committed
Fix FieldToProperty pass to ignore non-public properties in C++ generator.
1 parent 534acc5 commit 23fab70

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/AST/TypeExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ public static bool ResolvesTo(this QualifiedType type, QualifiedType other)
346346
return left.Equals(right);
347347
}
348348

349+
public static bool IsConstRef(this QualifiedType type)
350+
{
351+
Type desugared = type.Type.Desugar();
352+
Type pointee = desugared.GetFinalPointee().Desugar();
353+
pointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
354+
return desugared.IsReference() && type.IsConst();
355+
}
356+
349357
public static bool IsConstRefToPrimitive(this QualifiedType type)
350358
{
351359
Type desugared = type.Type.Desugar();

src/Generator/Passes/FieldToPropertyPass.cs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using CppSharp.AST;
4+
using CppSharp.AST.Extensions;
45
using CppSharp.Generators;
56

67
namespace CppSharp.Passes
@@ -38,6 +39,12 @@ public override bool VisitFieldDecl(Field field)
3839
if (ASTUtils.CheckIgnoreField(field))
3940
return false;
4041

42+
if (Options.GeneratorKind == GeneratorKind.CPlusPlus)
43+
{
44+
if (field.Access != AccessSpecifier.Public)
45+
return false;
46+
}
47+
4148
var @class = field.Namespace as Class;
4249
if (@class == null)
4350
return false;
@@ -91,31 +98,35 @@ private void GenerateAcessorMethods(Field field, Property property)
9198
SynthKind = FunctionSynthKind.FieldAcessor
9299
};
93100

94-
var setter = new Method
95-
{
96-
Name = $"set_{field.Name}",
97-
Namespace = @class,
98-
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
99-
Access = field.Access,
100-
AssociatedDeclaration = property,
101-
IsStatic = field.IsStatic,
102-
SynthKind = FunctionSynthKind.FieldAcessor
103-
};
104-
105-
var param = new Parameter
106-
{
107-
Name = "value",
108-
QualifiedType = field.QualifiedType,
109-
Namespace = setter
110-
};
111-
112-
setter.Parameters.Add(param);
113-
114101
property.GetMethod = getter;
115-
property.SetMethod = setter;
116-
117102
@class.Methods.Add(getter);
118-
@class.Methods.Add(setter);
103+
104+
var isSetterInvalid = field.QualifiedType.IsConstRef();
105+
if (!isSetterInvalid)
106+
{
107+
var setter = new Method
108+
{
109+
Name = $"set_{field.Name}",
110+
Namespace = @class,
111+
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
112+
Access = field.Access,
113+
AssociatedDeclaration = property,
114+
IsStatic = field.IsStatic,
115+
SynthKind = FunctionSynthKind.FieldAcessor
116+
};
117+
118+
var param = new Parameter
119+
{
120+
Name = "value",
121+
QualifiedType = field.QualifiedType,
122+
Namespace = setter
123+
};
124+
125+
setter.Parameters.Add(param);
126+
127+
property.SetMethod = setter;
128+
@class.Methods.Add(setter);
129+
}
119130
}
120131
}
121132
}

0 commit comments

Comments
 (0)