From 62f81e16f1e8167acceb4ca7ff9de4888829ce8d Mon Sep 17 00:00:00 2001 From: triton Date: Thu, 12 Dec 2013 20:39:58 +0000 Subject: [PATCH] Reworked pointer marshaling in C# and CLI. We are now more consistent between backends, specifically we need to make sure to avoid directly visiting the desugared type since we might lose type maps when handling typedefs. --- src/Generator/Generators/CLI/CLIMarshal.cs | 47 +++++++++++++++---- .../Generators/CSharp/CSharpMarshal.cs | 24 ++++------ 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 6f5e439b..c2b44b73 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -15,6 +15,19 @@ namespace CppSharp.Generators.CLI Context.MarshalToManaged = this; } + public override bool VisitType(Type type, TypeQualifiers quals) + { + TypeMap typeMap; + if (Context.Driver.TypeDatabase.FindTypeMap(type, out typeMap)) + { + typeMap.Type = type; + typeMap.CLIMarshalToManaged(Context); + return false; + } + + return true; + } + public override bool VisitTagType(TagType tag, TypeQualifiers quals) { var decl = tag.Declaration; @@ -44,7 +57,10 @@ namespace CppSharp.Generators.CLI public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) { - var pointee = pointer.Pointee; + if (!VisitType(pointer, quals)) + return false; + + var pointee = pointer.Pointee.Desugar(); PrimitiveType primitive; var param = Context.Parameter; @@ -55,7 +71,7 @@ namespace CppSharp.Generators.CLI return true; } - if (pointee.Desugar().IsPrimitiveType(PrimitiveType.Void)) + if (pointee.IsPrimitiveType(PrimitiveType.Void)) { Context.Return.Write("IntPtr({0})", Context.ReturnVarName); return true; @@ -68,14 +84,14 @@ namespace CppSharp.Generators.CLI return true; } - if (pointee.Desugar().IsPrimitiveType(out primitive)) + if (pointee.IsPrimitiveType(out primitive)) { Context.Return.Write("IntPtr({0})", Context.ReturnVarName); return true; } Class @class; - if (pointee.Desugar().IsTagDecl(out @class)) + if (pointee.IsTagDecl(out @class)) { var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName : Context.ReturnVarName; @@ -83,10 +99,7 @@ namespace CppSharp.Generators.CLI return true; } - if (!pointee.Visit(this, quals)) - return false; - - return true; + return pointer.Pointee.Visit(this, quals); } public override bool VisitMemberPointerType(MemberPointerType member, @@ -321,6 +334,19 @@ namespace CppSharp.Generators.CLI Context.MarshalToNative = this; } + public override bool VisitType(Type type, TypeQualifiers quals) + { + TypeMap typeMap; + if (Context.Driver.TypeDatabase.FindTypeMap(type, out typeMap)) + { + typeMap.Type = type; + typeMap.CLIMarshalToNative(Context); + return false; + } + + return true; + } + public override bool VisitTagType(TagType tag, TypeQualifiers quals) { var decl = tag.Declaration; @@ -359,6 +385,9 @@ namespace CppSharp.Generators.CLI public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) { + if (!VisitType(pointer, quals)) + return false; + var pointee = pointer.Pointee.Desugar(); if ((pointee.IsPrimitiveType(PrimitiveType.Char) || @@ -402,7 +431,7 @@ namespace CppSharp.Generators.CLI return true; } - return pointee.Visit(this, quals); + return pointer.Pointee.Visit(this, quals); } public override bool VisitMemberPointerType(MemberPointerType member, diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index a7e2e872..896d9b97 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -144,7 +144,7 @@ namespace CppSharp.Generators.CSharp if (!VisitType(pointer, quals)) return false; - var pointee = pointer.Pointee; + var pointee = pointer.Pointee.Desugar(); if (CSharpTypePrinter.IsConstCharString(pointer)) { @@ -153,7 +153,7 @@ namespace CppSharp.Generators.CSharp } PrimitiveType primitive; - if (pointee.Desugar().IsPrimitiveType(out primitive)) + if (pointee.IsPrimitiveType(out primitive)) { var param = Context.Parameter; if (param != null && (param.IsOut || param.IsInOut)) @@ -166,10 +166,7 @@ namespace CppSharp.Generators.CSharp return true; } - if (!pointee.Visit(this, quals)) - return false; - - return true; + return pointer.Pointee.Visit(this, quals); } private string MarshalStringToManaged(string varName) @@ -413,11 +410,10 @@ namespace CppSharp.Generators.CSharp if (!VisitType(pointer, quals)) return false; - var pointee = pointer.Pointee; + var pointee = pointer.Pointee.Desugar(); - Type type = pointee.Desugar(); - if ((type.IsPrimitiveType(PrimitiveType.Char) || - type.IsPrimitiveType(PrimitiveType.WideChar)) && + if ((pointee.IsPrimitiveType(PrimitiveType.Char) || + pointee.IsPrimitiveType(PrimitiveType.WideChar)) && pointer.QualifiedPointee.Qualifiers.IsConst) { Context.Return.Write(MarshalStringToUnmanaged( @@ -434,7 +430,7 @@ namespace CppSharp.Generators.CSharp } Class @class; - if (type.IsTagDecl(out @class) && @class.IsValueType) + if (pointee.IsTagDecl(out @class) && @class.IsValueType) { if (Context.Parameter.Usage == ParameterUsage.Out) { @@ -454,7 +450,7 @@ namespace CppSharp.Generators.CSharp } PrimitiveType primitive; - if (type.IsPrimitiveType(out primitive)) + if (pointee.IsPrimitiveType(out primitive)) { var param = Context.Parameter; @@ -464,7 +460,7 @@ namespace CppSharp.Generators.CSharp if (param.IsOut || param.IsInOut) { - var typeName = Type.TypePrinterDelegate(type); + var typeName = Type.TypePrinterDelegate(pointee); Context.SupportBefore.WriteLine("{0} _{1};", typeName, Helpers.SafeIdentifier(param.Name)); @@ -477,7 +473,7 @@ namespace CppSharp.Generators.CSharp return true; } - return pointee.Visit(this, quals); + return pointer.Pointee.Visit(this, quals); } private string MarshalStringToUnmanaged(string varName)