Browse Source

Generate valid C# for public fields of typedef-ed primitive pointer

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

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1614/head
Dimitar Dobrev 5 years ago
parent
commit
5c58a5993e
  1. 17
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 1
      tests/Common/Common.Tests.cs
  3. 3
      tests/Common/Common.h

17
src/Generator/Generators/CSharp/CSharpSources.cs

@ -1368,7 +1368,8 @@ namespace CppSharp.Generators.CSharp
var name = ((Class) field.Namespace).Layout.Fields.First( var name = ((Class) field.Namespace).Layout.Fields.First(
f => f.FieldPtr == field.OriginalPtr).Name; f => f.FieldPtr == field.OriginalPtr).Name;
string returnVar; string returnVar;
var arrayType = field.Type.Desugar() as ArrayType; Type fieldType = field.Type.Desugar();
var arrayType = fieldType as ArrayType;
if (@class.IsValueType) if (@class.IsValueType)
{ {
if (arrayType != null) if (arrayType != null)
@ -1385,8 +1386,8 @@ namespace CppSharp.Generators.CSharp
// Class field getter should return a reference object instead of a copy. Wrapping `returnVar` in // Class field getter should return a reference object instead of a copy. Wrapping `returnVar` in
// IntPtr ensures that non-copying object constructor is invoked. // IntPtr ensures that non-copying object constructor is invoked.
Class typeClass; Class typeClass;
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType && if (fieldType.TryGetClass(out typeClass) && !typeClass.IsValueType &&
!ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type)) !ASTUtils.IsMappedToPrimitive(Context.TypeMaps, fieldType))
returnVar = $"new {TypePrinter.IntPtrType}(&{returnVar})"; returnVar = $"new {TypePrinter.IntPtrType}(&{returnVar})";
} }
@ -1410,9 +1411,9 @@ namespace CppSharp.Generators.CSharp
Write("return "); Write("return ");
var @return = marshal.Context.Return.ToString(); var @return = marshal.Context.Return.ToString();
if (field.Type.IsPointer()) if (fieldType.IsPointer())
{ {
var final = field.Type.GetFinalPointee().Desugar(resolveTemplateSubstitution: false); var final = fieldType.GetFinalPointee().Desugar(resolveTemplateSubstitution: false);
var templateSubstitution = final as TemplateParameterSubstitutionType; var templateSubstitution = final as TemplateParameterSubstitutionType;
if (templateSubstitution != null && returnType.Type.IsDependent) if (templateSubstitution != null && returnType.Type.IsDependent)
Write($"({templateSubstitution.ReplacedParameter.Parameter.Name}) (object) "); Write($"({templateSubstitution.ReplacedParameter.Parameter.Name}) (object) ");
@ -1422,13 +1423,13 @@ namespace CppSharp.Generators.CSharp
!final.IsPrimitiveType(PrimitiveType.Char16) && !final.IsPrimitiveType(PrimitiveType.Char16) &&
!final.IsPrimitiveType(PrimitiveType.Char32)) || !final.IsPrimitiveType(PrimitiveType.Char32)) ||
(!Context.Options.MarshalCharAsManagedChar && (!Context.Options.MarshalCharAsManagedChar &&
!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst)) && !((PointerType) fieldType).QualifiedPointee.Qualifiers.IsConst)) &&
templateSubstitution == null) || templateSubstitution == null) ||
(!((PointerType) field.Type).QualifiedPointee.Qualifiers.IsConst && (!((PointerType) fieldType).QualifiedPointee.Qualifiers.IsConst &&
(final.IsPrimitiveType(PrimitiveType.WideChar) || (final.IsPrimitiveType(PrimitiveType.WideChar) ||
final.IsPrimitiveType(PrimitiveType.Char16) || final.IsPrimitiveType(PrimitiveType.Char16) ||
final.IsPrimitiveType(PrimitiveType.Char32)))) final.IsPrimitiveType(PrimitiveType.Char32))))
Write($"({field.Type.GetPointee().Desugar()}*) "); Write($"({fieldType.GetPointee().Desugar()}*) ");
} }
WriteLine($"{@return};"); WriteLine($"{@return};");

1
tests/Common/Common.Tests.cs

@ -24,6 +24,7 @@ public class CommonTests
Assert.That(foo.B, Is.EqualTo(5)); Assert.That(foo.B, Is.EqualTo(5));
Bar bar = foo; Bar bar = foo;
Assert.IsTrue(Bar.Item.Item1 == bar); Assert.IsTrue(Bar.Item.Item1 == bar);
foo.FieldOfTypedefPrimitivePointer = null;
using (var hasOverloadsWithDifferentPointerKindsToSameType = using (var hasOverloadsWithDifferentPointerKindsToSameType =
new HasOverloadsWithDifferentPointerKindsToSameType()) new HasOverloadsWithDifferentPointerKindsToSameType())

3
tests/Common/Common.h

@ -108,6 +108,9 @@ public:
typedef Foo* FooPtr; typedef Foo* FooPtr;
typedef uint8_t* typedefPrimitivePointer;
typedefPrimitivePointer fieldOfTypedefPrimitivePointer;
void TakesTypedefedPtr(FooPtr date); void TakesTypedefedPtr(FooPtr date);
int TakesRef(const Foo& other); int TakesRef(const Foo& other);

Loading…
Cancel
Save