Browse Source

Array marshalling (#1748)

* Generator: Customization for const char[]

Allow the user to choose whether `const char[]` should be marshalled as
`string` or a normal `char` array in C#.

A new option `MarshalConstCharArrayAsString` is added, and is `true`
by default.

This helps in situations where the original C++ API distinguishes
between C-strings and char arrays using the two different notations.

* CSharpMarshal: Fix unknown length array marshal

For unknown length arrays, also run a conversion loop if the primitive
type encountered needs conversion (e.g. `char` to `sbyte`).

* CSharpTypePrinter: Fix for boolean arrays
pull/1752/head
Trung Nguyen 3 years ago committed by GitHub
parent
commit
357efec91b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 3
      src/Generator/Generators/CLI/CLITypePrinter.cs
  3. 10
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  4. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  5. 10
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  6. 5
      src/Generator/Options.cs

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

@ -61,7 +61,8 @@ namespace CppSharp.Generators.CLI @@ -61,7 +61,8 @@ namespace CppSharp.Generators.CLI
break;
case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string
if (array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
if (Context.Context.Options.MarshalConstCharArrayAsString &&
array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
{
var pointer = new PointerType { QualifiedPointee = array.QualifiedType };

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

@ -35,7 +35,8 @@ namespace CppSharp.Generators.CLI @@ -35,7 +35,8 @@ namespace CppSharp.Generators.CLI
TypeQualifiers quals)
{
// const char* and const char[] are the same so we can use a string
if (array.SizeType == ArrayType.ArraySize.Incomplete &&
if (Context.Options.MarshalConstCharArrayAsString &&
array.SizeType == ArrayType.ArraySize.Incomplete &&
array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
return VisitCILType(new CILType(typeof(string)), quals);

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

@ -116,7 +116,8 @@ namespace CppSharp.Generators.CSharp @@ -116,7 +116,8 @@ namespace CppSharp.Generators.CSharp
break;
case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string
if (array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
if (Context.Context.Options.MarshalConstCharArrayAsString &&
array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
{
var pointer = new PointerType { QualifiedPointee = array.QualifiedType };
@ -874,7 +875,8 @@ namespace CppSharp.Generators.CSharp @@ -874,7 +875,8 @@ namespace CppSharp.Generators.CSharp
var elementType = arrayType.Type.Desugar();
if (elementType.IsPrimitiveType() ||
if ((elementType.IsPrimitiveType() &&
!(elementType.IsPrimitiveType(PrimitiveType.Char) && Context.Context.Options.MarshalCharAsManagedChar)) ||
elementType.IsPointerToPrimitiveType())
{
if (Context.Context.Options.UseSpan && !elementType.IsConstCharString())
@ -916,6 +918,10 @@ namespace CppSharp.Generators.CSharp @@ -916,6 +918,10 @@ namespace CppSharp.Generators.CSharp
Context.Before.WriteLine($@"{intermediateArray}[i] = {
element} is null ? {intPtrZero} : {element}.{Helpers.InstanceIdentifier};");
}
else if (elementType.IsPrimitiveType(PrimitiveType.Char) &&
Context.Context.Options.MarshalCharAsManagedChar)
Context.Before.WriteLine($@"{intermediateArray}[i] = global::System.Convert.ToSByte({
element});");
else
Context.Before.WriteLine($@"{intermediateArray}[i] = {
element} is null ? new {intermediateArrayType}() : *({

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

@ -972,7 +972,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat @@ -972,7 +972,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
string ptr = Generator.GeneratedIdentifier("ptr");
if (arrayType != null)
{
if (arrayType.Type.IsPrimitiveType(PrimitiveType.Char) && arrayType.SizeType != ArrayType.ArraySize.Constant)
if (Context.Options.MarshalConstCharArrayAsString && arrayType.Type.IsPrimitiveType(PrimitiveType.Char) && arrayType.SizeType != ArrayType.ArraySize.Constant)
WriteLine($"var {ptr} = {location};");
else
WriteLine($"var {ptr} = ({arrayType.Type.Visit(TypePrinter)}*){location};");

10
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -111,7 +111,8 @@ namespace CppSharp.Generators.CSharp @@ -111,7 +111,8 @@ namespace CppSharp.Generators.CSharp
}
// const char* and const char[] are the same so we can use a string
if (array.SizeType == ArrayType.ArraySize.Incomplete &&
if (Context.Options.MarshalConstCharArrayAsString &&
array.SizeType == ArrayType.ArraySize.Incomplete &&
arrayType.IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
return "string";
@ -123,6 +124,13 @@ namespace CppSharp.Generators.CSharp @@ -123,6 +124,13 @@ namespace CppSharp.Generators.CSharp
return $"{prefix}string[]";
}
if (arrayType.IsPrimitiveType(PrimitiveType.Bool))
{
var prefix = ContextKind == TypePrinterContextKind.Managed ? string.Empty :
"[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1)] ";
return $"{prefix}bool[]";
}
if (Context.Options.UseSpan && !(array.SizeType != ArrayType.ArraySize.Constant &&
MarshalKind == MarshalKind.ReturnVariableArray))
{

5
src/Generator/Options.cs

@ -115,7 +115,7 @@ namespace CppSharp @@ -115,7 +115,7 @@ namespace CppSharp
/// <summary>
/// Enable this option to enable generation of finalizers. Works in both CLI and
/// C# backends.
/// C# backends.
/// </summary>
/// <remarks>
/// Use <see cref="GenerateFinalizersFilter"/> to specify a filter so that
@ -125,7 +125,7 @@ namespace CppSharp @@ -125,7 +125,7 @@ namespace CppSharp
/// <summary>
/// A filter that can restrict the classes for which finalizers are generated when
/// <see cref="GenerateFinalizers"/> is <c>true</c>.
/// <see cref="GenerateFinalizers"/> is <c>true</c>.
/// </summary>
/// <remarks>
/// The default filter performs no filtering so that whenever <see
@ -189,6 +189,7 @@ namespace CppSharp @@ -189,6 +189,7 @@ namespace CppSharp
public readonly List<string> DependentNameSpaces = new List<string>();
public bool MarshalCharAsManagedChar { get; set; }
public bool MarshalConstCharArrayAsString { get; set; } = true;
/// <summary>
/// Use Span Struct instead of Managed Array

Loading…
Cancel
Save