From 384dac34423d256b62aa4fdcd3a273b6c9ceb2be Mon Sep 17 00:00:00 2001 From: Abhinav Tripathi Date: Wed, 17 Jun 2015 10:09:27 +0530 Subject: [PATCH] Fixed code generation when using arrays in ValueType types. --- src/Generator/Generators/CLI/CLIMarshal.cs | 2 +- .../Generators/CLI/CLISourcesTemplate.cs | 4 +- .../Generators/CSharp/CSharpTextTemplate.cs | 39 ++++++++++++++++++- tests/Basic/Basic.h | 10 +++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index eb007378..04f4fa1c 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -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(); diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 1b59bd51..52d3b079 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -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 ArgName = property.Name, ReturnVarName = nativeField, ReturnType = property.QualifiedType, - Declaration = property.Field + Declaration = property.Field, + ParameterIndex = paramIndex++ }; var marshal = new CLIMarshalNativeToManagedPrinter(ctx); diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index dd2cdbc9..4d54786a 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -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 WriteLine("{0} = {1};", ctx.ReturnVarName, marshal.Context.Return); } + if (arrayType != null && @class.IsValueType) + WriteCloseBraceIndent(); + WriteCloseBraceIndent(); } @@ -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 Write(marshal.Context.SupportBefore); WriteLine("return {0};", marshal.Context.Return); + + if (arrayType != null && @class.IsValueType) + WriteCloseBraceIndent(); } else if (decl is Variable) { diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 0daef970..0738d774 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -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; +};