Skip to content

Commit

Permalink
Fix generation for fields of type const reference
Browse files Browse the repository at this point in the history
Fixes #1323.

Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed May 29, 2020
1 parent 2b9aeda commit 2acbf32
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/AST/Property.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using CppSharp.AST.Extensions;

namespace CppSharp.AST
{
Expand Down Expand Up @@ -86,7 +87,8 @@ public bool HasSetter
return (SetMethod != null &&
SetMethod.GenerationKind != GenerationKind.None) ||
(Field != null &&
!Field.QualifiedType.Qualifiers.IsConst &&
(!Field.QualifiedType.IsConst() ||
Field.Type.IsConstCharString()) &&
Field.GenerationKind != GenerationKind.None);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/AST/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public void SaveCode(IEnumerable<GeneratorOutput> outputs)
private void WriteGeneratedCodeToFile(string file, string generatedCode)
{
var fi = new FileInfo(file);

if (!fi.Exists || fi.Length != generatedCode.Length ||
File.ReadAllText(file) != generatedCode)
File.WriteAllText(file, generatedCode);
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Generators/CLI/CLITypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/Common/Common.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
17 changes: 15 additions & 2 deletions tests/Common/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2acbf32

Please sign in to comment.