Browse Source

Fix generation for fields of type const reference

Fixes https://github.com/mono/CppSharp/issues/1323.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
const-wchar_t
Dimitar Dobrev 5 years ago
parent
commit
2acbf32a96
  1. 4
      src/AST/Property.cs
  2. 4
      src/AST/TypeExtensions.cs
  3. 3
      src/Generator/Generators/CLI/CLITypePrinter.cs
  4. 2
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  5. 1
      tests/Common/Common.Tests.cs
  6. 17
      tests/Common/Common.cpp
  7. 2
      tests/Common/Common.h

4
src/AST/Property.cs

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using CppSharp.AST.Extensions;
namespace CppSharp.AST namespace CppSharp.AST
{ {
@ -86,7 +87,8 @@ namespace CppSharp.AST
return (SetMethod != null && return (SetMethod != null &&
SetMethod.GenerationKind != GenerationKind.None) || SetMethod.GenerationKind != GenerationKind.None) ||
(Field != null && (Field != null &&
!Field.QualifiedType.Qualifiers.IsConst && (!Field.QualifiedType.IsConst() ||
Field.Type.IsConstCharString()) &&
Field.GenerationKind != GenerationKind.None); Field.GenerationKind != GenerationKind.None);
} }
} }

4
src/AST/TypeExtensions.cs

@ -349,8 +349,6 @@
public static bool IsConstRef(this QualifiedType type) public static bool IsConstRef(this QualifiedType type)
{ {
Type desugared = type.Type.Desugar(); Type desugared = type.Type.Desugar();
Type pointee = desugared.GetFinalPointee().Desugar();
pointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
return desugared.IsReference() && type.IsConst(); return desugared.IsReference() && type.IsConst();
} }
@ -363,7 +361,7 @@
(pointee.IsPrimitiveType() || pointee.IsEnum()) && type.IsConst(); (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 || return type.Type != null && (type.Qualifiers.IsConst ||
type.Type.GetQualifiedPointee().IsConst()); type.Type.GetQualifiedPointee().IsConst());

3
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -140,7 +140,8 @@ namespace CppSharp.Generators.CLI
return "::System::IntPtr"; return "::System::IntPtr";
var result = pointer.QualifiedPointee.Visit(this).ToString(); 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; Enumeration @enum;

2
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -201,6 +201,8 @@ namespace CppSharp.Generators.CSharp
return true; return true;
} }
Context.Return.Write("*"); Context.Return.Write("*");
if (Context.MarshalKind == MarshalKind.NativeField)
Context.Return.Write($"({pointer.QualifiedPointee.Visit(typePrinter)}*) ");
} }
Context.Return.Write(Context.ReturnVarName); Context.Return.Write(Context.ReturnVarName);

1
tests/Common/Common.Tests.cs

@ -559,6 +559,7 @@ public class CommonTests : GeneratorTestFixture
Assert.That(prop.nestedEnum(55), Is.EqualTo(55)); Assert.That(prop.nestedEnum(55), Is.EqualTo(55));
Assert.That(prop.Get32Bit, Is.EqualTo(10)); 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.IsEmpty, Is.EqualTo(prop.Empty));
Assert.That(prop.VirtualGetter, Is.EqualTo(15)); Assert.That(prop.VirtualGetter, Is.EqualTo(15));

17
tests/Common/Common.cpp

@ -542,7 +542,8 @@ SomeNamespace::AbstractClass::~AbstractClass()
TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0), TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0),
_getterAndSetterWithTheSameName(0), _setterReturnsBoolean(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), _getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName),
_setterReturnsBoolean(other._setterReturnsBoolean), _setterReturnsBoolean(other._setterReturnsBoolean),
_virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean), _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() int TestProperties::getFieldValue()
{ {
return Field; return Field;

2
tests/Common/Common.h

@ -598,7 +598,9 @@ public:
TestProperties(); TestProperties();
TestProperties(const TestProperties& other); TestProperties(const TestProperties& other);
TestProperties& operator=(const TestProperties& other);
int Field; int Field;
const int& ConstRefField;
int getFieldValue(); int getFieldValue();
void setFieldValue(int Value); void setFieldValue(int Value);

Loading…
Cancel
Save