Skip to content

Commit 2acbf32

Browse files
committed
Fix generation for fields of type const reference
Fixes #1323. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 2b9aeda commit 2acbf32

File tree

8 files changed

+27
-8
lines changed

8 files changed

+27
-8
lines changed

src/AST/Property.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using CppSharp.AST.Extensions;
23

34
namespace CppSharp.AST
45
{
@@ -86,7 +87,8 @@ public bool HasSetter
8687
return (SetMethod != null &&
8788
SetMethod.GenerationKind != GenerationKind.None) ||
8889
(Field != null &&
89-
!Field.QualifiedType.Qualifiers.IsConst &&
90+
(!Field.QualifiedType.IsConst() ||
91+
Field.Type.IsConstCharString()) &&
9092
Field.GenerationKind != GenerationKind.None);
9193
}
9294
}

src/AST/TypeExtensions.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,6 @@ public static bool ResolvesTo(this QualifiedType type, QualifiedType other)
349349
public static bool IsConstRef(this QualifiedType type)
350350
{
351351
Type desugared = type.Type.Desugar();
352-
Type pointee = desugared.GetFinalPointee().Desugar();
353-
pointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
354352
return desugared.IsReference() && type.IsConst();
355353
}
356354

@@ -363,7 +361,7 @@ public static bool IsConstRefToPrimitive(this QualifiedType type)
363361
(pointee.IsPrimitiveType() || pointee.IsEnum()) && type.IsConst();
364362
}
365363

366-
private static bool IsConst(this QualifiedType type)
364+
public static bool IsConst(this QualifiedType type)
367365
{
368366
return type.Type != null && (type.Qualifiers.IsConst ||
369367
type.Type.GetQualifiedPointee().IsConst());

src/Generator/Driver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public void SaveCode(IEnumerable<GeneratorOutput> outputs)
327327
private void WriteGeneratedCodeToFile(string file, string generatedCode)
328328
{
329329
var fi = new FileInfo(file);
330-
330+
331331
if (!fi.Exists || fi.Length != generatedCode.Length ||
332332
File.ReadAllText(file) != generatedCode)
333333
File.WriteAllText(file, generatedCode);

src/Generator/Generators/CLI/CLITypePrinter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public override TypePrinterResult VisitPointerType(PointerType pointer,
140140
return "::System::IntPtr";
141141

142142
var result = pointer.QualifiedPointee.Visit(this).ToString();
143-
return !isRefParam && result == "::System::IntPtr" ? "void**" : result + "*";
143+
return !isRefParam && result == "::System::IntPtr" ? "void**" :
144+
result + (pointer.IsReference ? "" : "*");
144145
}
145146

146147
Enumeration @enum;

src/Generator/Generators/CSharp/CSharpMarshal.cs

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
201201
return true;
202202
}
203203
Context.Return.Write("*");
204+
if (Context.MarshalKind == MarshalKind.NativeField)
205+
Context.Return.Write($"({pointer.QualifiedPointee.Visit(typePrinter)}*) ");
204206
}
205207

206208
Context.Return.Write(Context.ReturnVarName);

tests/Common/Common.Tests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ public void TestProperties()
559559
Assert.That(prop.nestedEnum(55), Is.EqualTo(55));
560560

561561
Assert.That(prop.Get32Bit, Is.EqualTo(10));
562+
Assert.That(prop.ConstRefField, Is.EqualTo(prop.Field));
562563
Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty));
563564

564565
Assert.That(prop.VirtualGetter, Is.EqualTo(15));

tests/Common/Common.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ SomeNamespace::AbstractClass::~AbstractClass()
542542

543543
TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0),
544544
_getterAndSetterWithTheSameName(0), _setterReturnsBoolean(0),
545-
_virtualSetterReturnsBoolean(0), _conflict(Conflict::Value1)
545+
_virtualSetterReturnsBoolean(0), _conflict(Conflict::Value1),
546+
ConstRefField(Field)
546547
{
547548
}
548549

@@ -552,10 +553,22 @@ TestProperties::TestProperties(const TestProperties& other) : Field(other.Field)
552553
_getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName),
553554
_setterReturnsBoolean(other._setterReturnsBoolean),
554555
_virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean),
555-
_conflict(other._conflict)
556+
_conflict(other._conflict), ConstRefField(other.ConstRefField)
556557
{
557558
}
558559

560+
TestProperties& TestProperties::operator=(const TestProperties& other)
561+
{
562+
Field = other.Field;
563+
FieldValue = other.FieldValue;
564+
_refToPrimitiveInSetter = other._refToPrimitiveInSetter;
565+
_getterAndSetterWithTheSameName = other._getterAndSetterWithTheSameName;
566+
_setterReturnsBoolean = other._setterReturnsBoolean;
567+
_virtualSetterReturnsBoolean = other._virtualSetterReturnsBoolean;
568+
_conflict = other._conflict;
569+
return *this;
570+
}
571+
559572
int TestProperties::getFieldValue()
560573
{
561574
return Field;

tests/Common/Common.h

+2
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,9 @@ struct DLL_API TestProperties
598598

599599
TestProperties();
600600
TestProperties(const TestProperties& other);
601+
TestProperties& operator=(const TestProperties& other);
601602
int Field;
603+
const int& ConstRefField;
602604

603605
int getFieldValue();
604606
void setFieldValue(int Value);

0 commit comments

Comments
 (0)