Browse Source

Added C# marshalling of parameters of type array of primitives.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/944/head
Dimitar Dobrev 8 years ago
parent
commit
ce71a3b8a8
  1. 3
      src/AST/ITypePrinter.cs
  2. 2
      src/Generator/Generators/CLI/CLIMarshal.cs
  3. 9
      src/Generator/Generators/CLI/CLITypeReferences.cs
  4. 9
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  5. 28
      src/Generator/Generators/CSharp/CSharpSources.cs
  6. 6
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  7. 3
      tests/CSharp/CSharp.Tests.cs
  8. 4
      tests/CSharp/CSharp.cpp
  9. 2
      tests/CSharp/CSharp.h

3
src/AST/ITypePrinter.cs

@ -19,7 +19,8 @@ namespace CppSharp.AST @@ -19,7 +19,8 @@ namespace CppSharp.AST
GenericDelegate,
DefaultExpression,
VTableReturnValue,
Variable
Variable,
ReturnVariableArray
}
public class TypePrinterContext

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

@ -528,8 +528,6 @@ namespace CppSharp.Generators.CLI @@ -528,8 +528,6 @@ namespace CppSharp.Generators.CLI
if (pointee is FunctionType)
{
var function = pointee as FunctionType;
var cppTypePrinter = new CppTypePrinter();
var cppTypeName = pointer.Visit(cppTypePrinter, quals);

9
src/Generator/Generators/CLI/CLITypeReferences.cs

@ -148,15 +148,6 @@ namespace CppSharp.Generators.CLI @@ -148,15 +148,6 @@ namespace CppSharp.Generators.CLI
}
return translationUnit.FileName;
var rel = PathHelpers.GetRelativePath(
TranslationUnit.FileRelativeDirectory,
translationUnit.FileRelativeDirectory);
if (string.IsNullOrEmpty(rel))
return translationUnit.FileName;
return Path.Combine(rel, translationUnit.FileName);
}
private bool IsBuiltinTypedef(Declaration decl)

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

@ -555,10 +555,7 @@ namespace CppSharp.Generators.CSharp @@ -555,10 +555,7 @@ namespace CppSharp.Generators.CSharp
}
if (pointee is FunctionType)
{
var function = pointee as FunctionType;
return VisitDelegateType();
}
Class @class;
if (pointee.TryGetClass(out @class) && @class.IsValueType)
@ -857,6 +854,12 @@ namespace CppSharp.Generators.CSharp @@ -857,6 +854,12 @@ namespace CppSharp.Generators.CSharp
private void MarshalVariableArray(Type arrayType)
{
if (arrayType.IsPrimitiveType())
{
Context.Return.Write(Context.Parameter.Name);
return;
}
var intermediateArray = $"__{Context.Parameter.Name}";
var intermediateArrayType = typePrinter.PrintNative(arrayType);
const string intPtrType = CSharpTypePrinter.IntPtrType;

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

@ -1028,21 +1028,28 @@ namespace CppSharp.Generators.CSharp @@ -1028,21 +1028,28 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PushContext(TypePrinterContextKind.Native);
var location = string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")",
GetLibraryOf(decl), var.Mangled);
string library = GetLibraryOf(decl);
var location = $"CppSharp.SymbolResolver.ResolveSymbol(\"{library}\", \"{var.Mangled}\")";
var ptr = Generator.GeneratedIdentifier("ptr");
var arrayType = decl.Type as ArrayType;
var @class = decl.Namespace as Class;
var isRefTypeArray = arrayType != null && @class != null && @class.IsRefType;
if (isRefTypeArray)
WriteLine("var {0} = {1}{2};", Generator.GeneratedIdentifier("ptr"),
arrayType.Type.IsPrimitiveType(PrimitiveType.Char) &&
{
string cast = arrayType.Type.IsPrimitiveType(PrimitiveType.Char) &&
arrayType.QualifiedType.Qualifiers.IsConst
? string.Empty : "(byte*)",
location);
? string.Empty : "(byte*)";
WriteLine($"var {ptr} = {cast}{location};");
}
else
WriteLine("var {0} = ({1}*){2};", Generator.GeneratedIdentifier("ptr"),
@var.Type, location);
{
TypePrinter.PushMarshalKind(MarshalKind.ReturnVariableArray);
var varType = var.Type.Visit(TypePrinter);
TypePrinter.PopMarshalKind();
WriteLine($"var {ptr} = ({varType}*){location};");
}
TypePrinter.PopContext();
@ -1322,7 +1329,10 @@ namespace CppSharp.Generators.CSharp @@ -1322,7 +1329,10 @@ namespace CppSharp.Generators.CSharp
PushBlock(BlockKind.Variable);
GenerateDeclarationCommon(variable);
WriteLine("public static {0} {1}", variable.Type, variable.Name);
TypePrinter.PushMarshalKind(MarshalKind.ReturnVariableArray);
var variableType = variable.Type.Visit(TypePrinter);
TypePrinter.PopMarshalKind();
WriteLine($"public static {variableType} {variable.Name}");
WriteStartBraceIndent();
GeneratePropertyGetter(variable, @class);

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

@ -128,9 +128,9 @@ namespace CppSharp.Generators.CSharp @@ -128,9 +128,9 @@ namespace CppSharp.Generators.CSharp
if (arrayType.IsPointerToPrimitiveType(PrimitiveType.Char))
return "char**";
var arraySuffix = array.SizeType == ArrayType.ArraySize.Constant ||
!arrayType.IsPrimitiveType() ? "[]" :
(ContextKind == TypePrinterContextKind.Managed ? "*" : string.Empty);
var arraySuffix = array.SizeType != ArrayType.ArraySize.Constant &&
MarshalKind == MarshalKind.ReturnVariableArray ?
(ContextKind == TypePrinterContextKind.Managed ? "*" : string.Empty) : "[]";
return $"{arrayType.Visit(this)}{arraySuffix}";
}

3
tests/CSharp/CSharp.Tests.cs

@ -1064,7 +1064,8 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -1064,7 +1064,8 @@ public unsafe class CSharpTests : GeneratorTestFixture
public void TestArrayParams()
{
Foo[] foos = { new Foo { A = 2 }, new Foo { A = 5 } };
Assert.That(CSharp.CSharp.TakeArrayOfPointersToObjects(foos), Is.EqualTo(7));
int[] ints = { 6, 7 };
Assert.That(CSharp.CSharp.TakeArrays(foos, ints), Is.EqualTo(20));
}
private class OverrideVirtualTemplate : VirtualTemplate<int>

4
tests/CSharp/CSharp.cpp

@ -1392,7 +1392,7 @@ void InlineNamespace::FunctionInsideInlineNamespace() @@ -1392,7 +1392,7 @@ void InlineNamespace::FunctionInsideInlineNamespace()
{
}
int takeArrayOfPointersToObjects(Foo* arrayOfPointersToObjects[])
int takeArrays(Foo* arrayOfPointersToObjects[], int arrayOfPrimitives[])
{
return arrayOfPointersToObjects[0]->A + arrayOfPointersToObjects[1]->A;
return arrayOfPointersToObjects[0]->A + arrayOfPointersToObjects[1]->A + arrayOfPrimitives[0] + arrayOfPrimitives[1];
}

2
tests/CSharp/CSharp.h

@ -1237,4 +1237,4 @@ inline namespace InlineNamespace @@ -1237,4 +1237,4 @@ inline namespace InlineNamespace
DLL_API void FunctionInsideInlineNamespace();
}
DLL_API int takeArrayOfPointersToObjects(Foo* arrayOfPointersToObjects[]);
DLL_API int takeArrays(Foo* arrayOfPointersToObjects[], int arrayOfPrimitives[]);

Loading…
Cancel
Save