From 418c72067ed313bbba0375a3135bf301e1a82d9d Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sat, 30 May 2020 02:10:47 +0300 Subject: [PATCH] Fix generation for fields of type const reference Fixes https://github.com/mono/CppSharp/issues/1323. Signed-off-by: Dimitar Dobrev --- src/AST/Property.cs | 3 ++- src/AST/TypeExtensions.cs | 4 +--- src/Generator/Generators/CLI/CLITypePrinter.cs | 3 ++- .../Generators/CSharp/CSharpMarshal.cs | 2 ++ tests/Common/Common.Tests.cs | 1 + tests/Common/Common.cpp | 17 +++++++++++++++-- tests/Common/Common.h | 2 ++ 7 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/AST/Property.cs b/src/AST/Property.cs index fd961edc2b..e634b414e4 100644 --- a/src/AST/Property.cs +++ b/src/AST/Property.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using CppSharp.AST.Extensions; namespace CppSharp.AST { @@ -86,7 +87,7 @@ public bool HasSetter return (SetMethod != null && SetMethod.GenerationKind != GenerationKind.None) || (Field != null && - !Field.QualifiedType.Qualifiers.IsConst && + !Field.QualifiedType.IsConst() && Field.GenerationKind != GenerationKind.None); } } diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index 4ffc8ea12a..7bf0631c23 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -349,8 +349,6 @@ public static bool ResolvesTo(this QualifiedType type, QualifiedType other) 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(); } @@ -363,7 +361,7 @@ public static bool IsConstRefToPrimitive(this QualifiedType type) (pointee.IsPrimitiveType() || pointee.IsEnum()) && type.IsConst(); } - private static bool IsConst(this QualifiedType type) + public static bool IsConst(this QualifiedType type) { return type.Type != null && (type.Qualifiers.IsConst || type.Type.GetQualifiedPointee().IsConst()); diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index 50c57f2d87..8b1dca12dd 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -140,7 +140,8 @@ public override TypePrinterResult VisitPointerType(PointerType pointer, return "::System::IntPtr"; var result = pointer.QualifiedPointee.Visit(this).ToString(); - return !isRefParam && result == "::System::IntPtr" ? "void**" : result + "*"; + return !isRefParam && result == "::System::IntPtr" ? "void**" : + result + (pointer.IsReference ? "" : "*"); } Enumeration @enum; diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 920adb8c66..4c5c4d23d4 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -201,6 +201,8 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) return true; } Context.Return.Write("*"); + if (Context.MarshalKind == MarshalKind.NativeField) + Context.Return.Write($"({pointer.QualifiedPointee.Visit(typePrinter)}*) "); } Context.Return.Write(Context.ReturnVarName); diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index f54e6de40a..8a5912ce15 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -559,6 +559,7 @@ public void TestProperties() Assert.That(prop.nestedEnum(55), Is.EqualTo(55)); Assert.That(prop.Get32Bit, Is.EqualTo(10)); + Assert.That(prop.ConstRefField, Is.EqualTo(prop.Field)); Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty)); Assert.That(prop.VirtualGetter, Is.EqualTo(15)); diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 040d2d69fa..2a73fe112b 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -542,7 +542,8 @@ SomeNamespace::AbstractClass::~AbstractClass() TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0), _getterAndSetterWithTheSameName(0), _setterReturnsBoolean(0), - _virtualSetterReturnsBoolean(0), _conflict(Conflict::Value1) + _virtualSetterReturnsBoolean(0), _conflict(Conflict::Value1), + ConstRefField(Field) { } @@ -552,10 +553,22 @@ TestProperties::TestProperties(const TestProperties& other) : Field(other.Field) _getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName), _setterReturnsBoolean(other._setterReturnsBoolean), _virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean), - _conflict(other._conflict) + _conflict(other._conflict), ConstRefField(other.ConstRefField) { } +TestProperties& TestProperties::operator=(const TestProperties& other) +{ + Field = other.Field; + FieldValue = other.FieldValue; + _refToPrimitiveInSetter = other._refToPrimitiveInSetter; + _getterAndSetterWithTheSameName = other._getterAndSetterWithTheSameName; + _setterReturnsBoolean = other._setterReturnsBoolean; + _virtualSetterReturnsBoolean = other._virtualSetterReturnsBoolean; + _conflict = other._conflict; + return *this; +} + int TestProperties::getFieldValue() { return Field; diff --git a/tests/Common/Common.h b/tests/Common/Common.h index bb6ec3e915..7299c23d3f 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -598,7 +598,9 @@ struct DLL_API TestProperties TestProperties(); TestProperties(const TestProperties& other); + TestProperties& operator=(const TestProperties& other); int Field; + const int& ConstRefField; int getFieldValue(); void setFieldValue(int Value);