From 0ae181b2cf838ab61e1bd2c0e2648fad1b195275 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 21 Aug 2013 17:10:20 +0300 Subject: [PATCH] Added suffixes to generated variables to avoid conflicts. Signed-off-by: Dimitar Dobrev --- src/Generator/Generators/CSharp/CSharpMarshal.cs | 6 ++++++ src/Generator/Generators/CSharp/CSharpTextTemplate.cs | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 3fce5e19..6f1bb060 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -59,6 +59,8 @@ namespace CppSharp.Generators.CSharp Context.MarshalToManaged = this; } + public int VarSuffix { get; set; } + public static string QualifiedIdentifier(Declaration decl) { var names = new List { decl.Name }; @@ -207,6 +209,8 @@ namespace CppSharp.Generators.CSharp if (ctx.Kind == CSharpMarshalKind.NativeField) { string copy = Generator.GeneratedIdentifier("copy"); + if (VarSuffix > 0) + copy += VarSuffix; Context.SupportBefore.WriteLine( "var {0} = new global::System.IntPtr(&{1});", copy, instance); instance = copy; @@ -215,6 +219,8 @@ namespace CppSharp.Generators.CSharp if (@class.IsRefType) { var instanceName = Generator.GeneratedIdentifier("instance"); + if (VarSuffix > 0) + instanceName += VarSuffix; // Allocate memory for a new native object and call the ctor. Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});", diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 11b53cf1..1854ceb3 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -503,8 +503,9 @@ namespace CppSharp.Generators.CSharp GenerateStructMarshalingFields(@base.Class); } - foreach (var field in @class.Fields) + for (int i = 0; i < @class.Fields.Count; i++) { + var field = @class.Fields[i]; if (ASTUtils.CheckIgnoreField(field)) continue; var nativeField = string.Format("{0}->{1}", @@ -519,6 +520,7 @@ namespace CppSharp.Generators.CSharp }; var marshal = new CSharpMarshalNativeToManagedPrinter(ctx); + marshal.VarSuffix = i; field.Visit(marshal); if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))