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

4
src/AST/TypeExtensions.cs

@ -349,8 +349,6 @@ @@ -349,8 +349,6 @@
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 @@ @@ -363,7 +361,7 @@
(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());

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

@ -140,7 +140,8 @@ namespace CppSharp.Generators.CLI @@ -140,7 +140,8 @@ namespace CppSharp.Generators.CLI
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;

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

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

1
tests/Common/Common.Tests.cs

@ -559,6 +559,7 @@ public class CommonTests : GeneratorTestFixture @@ -559,6 +559,7 @@ public class CommonTests : GeneratorTestFixture
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));

17
tests/Common/Common.cpp

@ -542,7 +542,8 @@ SomeNamespace::AbstractClass::~AbstractClass() @@ -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) @@ -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;

2
tests/Common/Common.h

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

Loading…
Cancel
Save