Browse Source

Fixed code generation when using arrays in ValueType types.

pull/491/head
Abhinav Tripathi 10 years ago
parent
commit
384dac3442
  1. 2
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 4
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  3. 39
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 10
      tests/Basic/Basic.h

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

@ -46,7 +46,7 @@ namespace CppSharp.Generators.CLI @@ -46,7 +46,7 @@ namespace CppSharp.Generators.CLI
{
case ArrayType.ArraySize.Constant:
var supportBefore = Context.SupportBefore;
string value = Generator.GeneratedIdentifier("array");
string value = Generator.GeneratedIdentifier("array") + Context.ParameterIndex;
supportBefore.WriteLine("cli::array<{0}>^ {1} = nullptr;", array.Type, value, array.Size);
supportBefore.WriteLine("if ({0} != 0)", Context.ReturnVarName);
supportBefore.WriteStartBraceIndent();

4
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -666,6 +666,7 @@ namespace CppSharp.Generators.CLI @@ -666,6 +666,7 @@ namespace CppSharp.Generators.CLI
GenerateStructMarshaling(@base.Class, nativeVar);
}
int paramIndex = 0;
foreach (var property in @class.Properties.Where( p => !ASTUtils.CheckIgnoreProperty(p)))
{
if (property.Field == null)
@ -679,7 +680,8 @@ namespace CppSharp.Generators.CLI @@ -679,7 +680,8 @@ namespace CppSharp.Generators.CLI
ArgName = property.Name,
ReturnVarName = nativeField,
ReturnType = property.QualifiedType,
Declaration = property.Field
Declaration = property.Field,
ParameterIndex = paramIndex++
};
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);

39
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -856,12 +856,29 @@ namespace CppSharp.Generators.CSharp @@ -856,12 +856,29 @@ namespace CppSharp.Generators.CSharp
WriteStartBraceIndent();
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.ReturnVarName = string.Format("{0}{1}{2}",
var arrayType = field.Type as ArrayType;
if (arrayType != null && @class.IsValueType)
{
CSharpTypePrinter typePrinter = new CSharpTypePrinter(ctx.Driver);
string type = arrayType.CSharpType(typePrinter).Type;
string arrPtrIden = Helpers.SafeIdentifier("arrPtr");
WriteLine(string.Format("fixed ({0} {1} = {2}.{3})",
type.Replace("[]", "*"), arrPtrIden, Helpers.InstanceField,
Helpers.SafeIdentifier(field.OriginalName)));
WriteStartBraceIndent();
ctx.ReturnVarName = arrPtrIden;
}
else
{
ctx.ReturnVarName = string.Format("{0}{1}{2}",
@class.IsValueType
? Helpers.InstanceField
: string.Format("((Internal*) {0})", Helpers.InstanceIdentifier),
@class.IsValueType ? "." : "->",
Helpers.SafeIdentifier(field.OriginalName));
}
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -872,6 +889,9 @@ namespace CppSharp.Generators.CSharp @@ -872,6 +889,9 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} = {1};", ctx.ReturnVarName, marshal.Context.Return);
}
if (arrayType != null && @class.IsValueType)
WriteCloseBraceIndent();
WriteCloseBraceIndent();
}
@ -962,6 +982,20 @@ namespace CppSharp.Generators.CSharp @@ -962,6 +982,20 @@ namespace CppSharp.Generators.CSharp
ReturnType = decl.QualifiedType
};
var arrayType = field.Type as ArrayType;
if (arrayType != null && @class.IsValueType)
{
CSharpTypePrinter typePrinter = new CSharpTypePrinter(ctx.Driver);
string type = arrayType.CSharpType(typePrinter).Type;
string arrPtrIden = Helpers.SafeIdentifier("arrPtr");
WriteLine(string.Format("fixed ({0} {1} = {2}.{3})",
type.Replace("[]","*"), arrPtrIden, Helpers.InstanceField,
Helpers.SafeIdentifier(field.OriginalName)));
WriteStartBraceIndent();
ctx.ReturnVarName = arrPtrIden;
}
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
decl.CSharpMarshalToManaged(marshal);
@ -969,6 +1003,9 @@ namespace CppSharp.Generators.CSharp @@ -969,6 +1003,9 @@ namespace CppSharp.Generators.CSharp
Write(marshal.Context.SupportBefore);
WriteLine("return {0};", marshal.Context.Return);
if (arrayType != null && @class.IsValueType)
WriteCloseBraceIndent();
}
else if (decl is Variable)
{

10
tests/Basic/Basic.h

@ -752,3 +752,13 @@ public: @@ -752,3 +752,13 @@ public:
void funcTry(CS_OUT ClassPassTry* classTry) { }
void funcTry2(CS_OUT ClassPassTry classTry) { }
#define ARRAY_LENGTH 5
#define CS_VALUE_TYPE
struct CS_VALUE_TYPE ValueTypeArrays
{
float firstValueTypeArrray[ARRAY_LENGTH];
int secondValueTypeArray[ARRAY_LENGTH];
char thirdValueTypeArray[ARRAY_LENGTH];
size_t size;
};

Loading…
Cancel
Save