diff --git a/src/AST/Class.cs b/src/AST/Class.cs index aa756db2..cce81845 100644 --- a/src/AST/Class.cs +++ b/src/AST/Class.cs @@ -145,7 +145,7 @@ namespace CppSharp.AST public bool IsRefType { - get { return Type == ClassType.RefType; } + get { return Type == ClassType.RefType && !IsUnion; } } public IEnumerable Constructors diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 6e1e6d90..e9634456 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -180,7 +180,7 @@ namespace CppSharp.Generators.CLI if (@class.IsRefType) { - var name = Helpers.GeneratedIdentifier(Context.ReturnVarName); + var name = Generator.GeneratedIdentifier(Context.ReturnVarName); Context.SupportBefore.WriteLine("auto {0} = new ::{1}({2});", name, @class.QualifiedOriginalName, Context.ReturnVarName); instance = name; diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 1a13b35e..8f87ae90 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -740,7 +740,8 @@ namespace CppSharp.Generators.CLI var @params = GenerateFunctionParamsMarshal(function.Parameters, function); if (needsReturn) - Write("auto {0}ret = ",(function.ReturnType.Type.IsReference())? "&": string.Empty); + Write("auto {0}{1} = ",(function.ReturnType.Type.IsReference())? "&": string.Empty, + Generator.GeneratedIdentifier("ret")); if (!IsNativeFunctionOrStaticMethod(function)) { @@ -799,8 +800,8 @@ namespace CppSharp.Generators.CLI { var ctx = new MarshalContext(Driver) { - ArgName = "ret", - ReturnVarName = "ret", + ArgName = Generator.GeneratedIdentifier("ret"), + ReturnVarName = Generator.GeneratedIdentifier("ret"), ReturnType = retType }; diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 545cd683..08de1719 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -185,7 +185,7 @@ namespace CppSharp.Generators.CSharp FunctionType function; if (decl.Type.IsPointerTo(out function)) { - var ptrName = Helpers.GeneratedIdentifier("ptr") + + var ptrName = Generator.GeneratedIdentifier("ptr") + Context.ParameterIndex; Context.SupportBefore.WriteLine("var {0} = {1};", ptrName, @@ -206,12 +206,15 @@ namespace CppSharp.Generators.CSharp string instance = Context.ReturnVarName; if (ctx.Kind == CSharpMarshalKind.NativeField) { - instance = string.Format("new global::System.IntPtr(&{0})", instance); + string copy = Generator.GeneratedIdentifier("copy"); + Context.SupportBefore.WriteLine( + "var {0} = new global::System.IntPtr(&{1});", copy, instance); + instance = copy; } if (@class.IsRefType) { - var instanceName = Helpers.GeneratedIdentifier("instance"); + var instanceName = Generator.GeneratedIdentifier("instance"); // Allocate memory for a new native object and call the ctor. Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});", @@ -363,17 +366,17 @@ namespace CppSharp.Generators.CSharp if (Context.Parameter.Usage == ParameterUsage.Out) { Context.SupportBefore.WriteLine("var {0} = new {1}.Internal();", - Helpers.GeneratedIdentifier(Context.ArgName), @class.Name); + Generator.GeneratedIdentifier(Context.ArgName), @class.Name); } else { Context.SupportBefore.WriteLine("var {0} = {1}.ToInternal();", - Helpers.GeneratedIdentifier(Context.ArgName), + Generator.GeneratedIdentifier(Context.ArgName), Helpers.SafeIdentifier(Context.Parameter.Name)); } Context.Return.Write("new global::System.IntPtr(&{0})", - Helpers.GeneratedIdentifier(Context.ArgName)); + Generator.GeneratedIdentifier(Context.ArgName)); return true; } diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 97a758ea..2e40d40b 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -26,11 +26,6 @@ namespace CppSharp.Generators.CSharp "void", "partial", "yield", "where" }; - public static string GeneratedIdentifier(string id) - { - return "_" + id; - } - public static string SafeIdentifier(string id) { id = new string(((IEnumerable)id) @@ -59,7 +54,7 @@ namespace CppSharp.Generators.CSharp public static string InstanceIdentifier { - get { return GeneratedIdentifier("Instance"); } + get { return Generator.GeneratedIdentifier("Instance"); } } } @@ -124,7 +119,7 @@ namespace CppSharp.Generators.CSharp public static string GeneratedIdentifier(string id) { - return Helpers.GeneratedIdentifier(id); + return Generator.GeneratedIdentifier(id); } public static string SafeIdentifier(string id) @@ -216,7 +211,7 @@ namespace CppSharp.Generators.CSharp if (context.HasFunctions) { PushBlock(CSharpBlockKind.Functions); - WriteLine("public partial class {0}{1}", SafeIdentifier(Options.OutputNamespace), + WriteLine("public unsafe partial class {0}{1}", SafeIdentifier(Options.OutputNamespace), TranslationUnit.FileNameWithoutExtension); WriteStartBraceIndent(); @@ -324,8 +319,8 @@ namespace CppSharp.Generators.CSharp if (ShouldGenerateClassNativeField(@class)) { PushBlock(CSharpBlockKind.Field); - WriteLine("public global::System.IntPtr {0} {{ get; protected set; }}", - Helpers.InstanceIdentifier); + WriteLine("public global::System.IntPtr {0} {{ get; {1} set; }}", + Helpers.InstanceIdentifier, @class.IsValueType ? "private" : "protected"); PopBlock(NewLineKind.BeforeNextBlock); } @@ -513,7 +508,7 @@ namespace CppSharp.Generators.CSharp if (ASTUtils.CheckIgnoreField(field)) continue; var nativeField = string.Format("{0}->{1}", - Helpers.GeneratedIdentifier("ptr"), field.OriginalName); + Generator.GeneratedIdentifier("ptr"), field.OriginalName); var ctx = new CSharpMarshalContext(Driver) { @@ -535,9 +530,9 @@ namespace CppSharp.Generators.CSharp private void GenerateStructInternalMarshaling(Class @class) { - var marshalVar = Helpers.GeneratedIdentifier("native"); + var marshalVar = Generator.GeneratedIdentifier("native"); - WriteLine("var {0} = new {1}.Internal();", marshalVar, @class.Name); + WriteLine("var {0} = new {1}.Internal();", marshalVar, QualifiedIdentifier(@class)); GenerateStructInternalMarshalingFields(@class, marshalVar); WriteLine("return {0};", marshalVar); @@ -746,7 +741,7 @@ namespace CppSharp.Generators.CSharp var field = decl as Field; WriteLine("var {0} = (Internal*){1}.ToPointer();", - Helpers.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); + Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); var marshal = new CSharpMarshalManagedToNativePrinter(ctx); param.Visit(marshal); @@ -754,7 +749,7 @@ namespace CppSharp.Generators.CSharp if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) Write(marshal.Context.SupportBefore); - Write("{0}->{1} = {2}", Helpers.GeneratedIdentifier("ptr"), + Write("{0}->{1} = {2}", Generator.GeneratedIdentifier("ptr"), Helpers.SafeIdentifier(field.OriginalName), marshal.Context.Return); WriteLine(";"); @@ -781,13 +776,13 @@ namespace CppSharp.Generators.CSharp var field = decl as Field; WriteLine("var {0} = (Internal*){1}.ToPointer();", - Helpers.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); + Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier); var ctx = new CSharpMarshalContext(Driver) { Kind = CSharpMarshalKind.NativeField, ArgName = decl.Name, - ReturnVarName = string.Format("{0}->{1}", Helpers.GeneratedIdentifier("ptr"), + ReturnVarName = string.Format("{0}->{1}", Generator.GeneratedIdentifier("ptr"), Helpers.SafeIdentifier(field.OriginalName)), ReturnType = decl.QualifiedType }; @@ -810,7 +805,7 @@ namespace CppSharp.Generators.CSharp var location = string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")", libSymbol.Item1, libSymbol.Item2); - WriteLine("var {0} = ({1}*){2};", Helpers.GeneratedIdentifier("ptr"), + WriteLine("var {0} = ({1}*){2};", Generator.GeneratedIdentifier("ptr"), @var.Type, location); TypePrinter.PopContext(); @@ -818,7 +813,7 @@ namespace CppSharp.Generators.CSharp var ctx = new CSharpMarshalContext(Driver) { ArgName = decl.Name, - ReturnVarName = "*" + Helpers.GeneratedIdentifier("ptr"), + ReturnVarName = "*" + Generator.GeneratedIdentifier("ptr"), ReturnType = new QualifiedType(var.Type) }; @@ -1185,7 +1180,7 @@ namespace CppSharp.Generators.CSharp var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true); TypePrinter.PopContext(); - delegateInstance = Helpers.GeneratedIdentifier(@event.OriginalName); + delegateInstance = Generator.GeneratedIdentifier(@event.OriginalName); delegateName = delegateInstance + "Delegate"; delegateRaise = delegateInstance + "RaiseInstance"; @@ -1221,7 +1216,7 @@ namespace CppSharp.Generators.CSharp WriteLine("{0} = new {1}(_{2}Raise);", delegateRaise, delegateName, @event.Name); WriteLine("var {0} = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();", - Helpers.GeneratedIdentifier("ptr"), delegateInstance); + Generator.GeneratedIdentifier("ptr"), delegateInstance); // Call type map here. @@ -1362,7 +1357,8 @@ namespace CppSharp.Generators.CSharp PopBlock(NewLineKind.BeforeNextBlock); PushBlock(CSharpBlockKind.Method); - WriteLine("internal {0}(global::System.IntPtr native)", SafeIdentifier(@class.Name)); + WriteLine("internal {0}(global::System.IntPtr native){1}", SafeIdentifier(@class.Name), + @class.IsValueType ? " : this()" : string.Empty); var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType; if (hasBaseClass) @@ -1376,13 +1372,13 @@ namespace CppSharp.Generators.CSharp { WriteLine("{0} = native;", Helpers.InstanceIdentifier); if (Options.GenerateVirtualTables && @class.IsDynamic) - WriteLine("SetupVTables(_Instance);"); + WriteLine("SetupVTables({0});", Generator.GeneratedIdentifier("Instance")); } } else { WriteLine("var {0} = (Internal*){1}.ToPointer();", - Helpers.GeneratedIdentifier("ptr"), "native"); + Generator.GeneratedIdentifier("ptr"), "native"); GenerateStructMarshalingFields(@class); } @@ -1401,7 +1397,7 @@ namespace CppSharp.Generators.CSharp PushBlock(CSharpBlockKind.Method); WriteLine("internal void FromInternal(Internal* native)"); WriteStartBraceIndent(); - WriteLine("var {0} = {1};", Helpers.GeneratedIdentifier("ptr"), "native"); + WriteLine("var {0} = {1};", Generator.GeneratedIdentifier("ptr"), "native"); GenerateStructMarshalingFields(@class); WriteCloseBraceIndent(); PopBlock(NewLineKind.BeforeNextBlock); @@ -1580,7 +1576,7 @@ namespace CppSharp.Generators.CSharp GenerateFunctionParams(@params); WriteLine(");"); if (Options.GenerateVirtualTables && @class.IsDynamic) - WriteLine("SetupVTables(_Instance);"); + WriteLine("SetupVTables({0});", Generator.GeneratedIdentifier("Instance")); } public void GenerateInternalFunctionCall(Function function, @@ -1636,7 +1632,7 @@ namespace CppSharp.Generators.CSharp Class retClass = null; hiddenParam.Type.Desugar().IsTagDecl(out retClass); - WriteLine("var ret = new {0}.Internal();", retClass.OriginalName); + WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"), QualifiedIdentifier(retClass)); } var names = new List(); @@ -1653,7 +1649,7 @@ namespace CppSharp.Generators.CSharp if (function.HasHiddenStructParameter) { - var name = string.Format("new IntPtr(&ret)"); + var name = string.Format("new IntPtr(&{0})", GeneratedIdentifier("ret")); names.Insert(0, name); } @@ -1668,11 +1664,11 @@ namespace CppSharp.Generators.CSharp //WriteLine("fixed({0}* {1} = &this)", @class.QualifiedName, // GeneratedIdentifier("instance")); //WriteStartBraceIndent(); - WriteLine("var {0} = ToInternal();", Helpers.GeneratedIdentifier("instance")); + WriteLine("var {0} = ToInternal();", Generator.GeneratedIdentifier("instance")); } if (needsReturn && !function.HasHiddenStructParameter) - Write("var ret = "); + Write("var {0} = ", GeneratedIdentifier("ret")); WriteLine("{0}({1});", functionName, string.Join(", ", names)); @@ -1696,15 +1692,15 @@ namespace CppSharp.Generators.CSharp if (needsFixedThis) { // WriteCloseBraceIndent(); - WriteLine("FromInternal(&{0});", Helpers.GeneratedIdentifier("instance")); + WriteLine("FromInternal(&{0});", Generator.GeneratedIdentifier("instance")); } if (needsReturn) { var ctx = new CSharpMarshalContext(Driver) { - ArgName = "ret", - ReturnVarName = "ret", + ArgName = GeneratedIdentifier("ret"), + ReturnVarName = GeneratedIdentifier("ret"), ReturnType = retType }; diff --git a/src/Generator/Generators/Generator.cs b/src/Generator/Generators/Generator.cs index 87a05003..9f078639 100644 --- a/src/Generator/Generators/Generator.cs +++ b/src/Generator/Generators/Generator.cs @@ -98,5 +98,10 @@ namespace CppSharp.Generators /// Generates the outputs for a given translation unit. /// public abstract List