Browse Source

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.
pull/137/merge
triton 12 years ago
parent
commit
62f81e16f1
  1. 47
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 24
      src/Generator/Generators/CSharp/CSharpMarshal.cs

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

@ -15,6 +15,19 @@ namespace CppSharp.Generators.CLI
Context.MarshalToManaged = this; 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) public override bool VisitTagType(TagType tag, TypeQualifiers quals)
{ {
var decl = tag.Declaration; var decl = tag.Declaration;
@ -44,7 +57,10 @@ namespace CppSharp.Generators.CLI
public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) 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; PrimitiveType primitive;
var param = Context.Parameter; var param = Context.Parameter;
@ -55,7 +71,7 @@ namespace CppSharp.Generators.CLI
return true; return true;
} }
if (pointee.Desugar().IsPrimitiveType(PrimitiveType.Void)) if (pointee.IsPrimitiveType(PrimitiveType.Void))
{ {
Context.Return.Write("IntPtr({0})", Context.ReturnVarName); Context.Return.Write("IntPtr({0})", Context.ReturnVarName);
return true; return true;
@ -68,14 +84,14 @@ namespace CppSharp.Generators.CLI
return true; return true;
} }
if (pointee.Desugar().IsPrimitiveType(out primitive)) if (pointee.IsPrimitiveType(out primitive))
{ {
Context.Return.Write("IntPtr({0})", Context.ReturnVarName); Context.Return.Write("IntPtr({0})", Context.ReturnVarName);
return true; return true;
} }
Class @class; Class @class;
if (pointee.Desugar().IsTagDecl(out @class)) if (pointee.IsTagDecl(out @class))
{ {
var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName
: Context.ReturnVarName; : Context.ReturnVarName;
@ -83,10 +99,7 @@ namespace CppSharp.Generators.CLI
return true; return true;
} }
if (!pointee.Visit(this, quals)) return pointer.Pointee.Visit(this, quals);
return false;
return true;
} }
public override bool VisitMemberPointerType(MemberPointerType member, public override bool VisitMemberPointerType(MemberPointerType member,
@ -321,6 +334,19 @@ namespace CppSharp.Generators.CLI
Context.MarshalToNative = this; 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) public override bool VisitTagType(TagType tag, TypeQualifiers quals)
{ {
var decl = tag.Declaration; var decl = tag.Declaration;
@ -359,6 +385,9 @@ namespace CppSharp.Generators.CLI
public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
{ {
if (!VisitType(pointer, quals))
return false;
var pointee = pointer.Pointee.Desugar(); var pointee = pointer.Pointee.Desugar();
if ((pointee.IsPrimitiveType(PrimitiveType.Char) || if ((pointee.IsPrimitiveType(PrimitiveType.Char) ||
@ -402,7 +431,7 @@ namespace CppSharp.Generators.CLI
return true; return true;
} }
return pointee.Visit(this, quals); return pointer.Pointee.Visit(this, quals);
} }
public override bool VisitMemberPointerType(MemberPointerType member, public override bool VisitMemberPointerType(MemberPointerType member,

24
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -144,7 +144,7 @@ namespace CppSharp.Generators.CSharp
if (!VisitType(pointer, quals)) if (!VisitType(pointer, quals))
return false; return false;
var pointee = pointer.Pointee; var pointee = pointer.Pointee.Desugar();
if (CSharpTypePrinter.IsConstCharString(pointer)) if (CSharpTypePrinter.IsConstCharString(pointer))
{ {
@ -153,7 +153,7 @@ namespace CppSharp.Generators.CSharp
} }
PrimitiveType primitive; PrimitiveType primitive;
if (pointee.Desugar().IsPrimitiveType(out primitive)) if (pointee.IsPrimitiveType(out primitive))
{ {
var param = Context.Parameter; var param = Context.Parameter;
if (param != null && (param.IsOut || param.IsInOut)) if (param != null && (param.IsOut || param.IsInOut))
@ -166,10 +166,7 @@ namespace CppSharp.Generators.CSharp
return true; return true;
} }
if (!pointee.Visit(this, quals)) return pointer.Pointee.Visit(this, quals);
return false;
return true;
} }
private string MarshalStringToManaged(string varName) private string MarshalStringToManaged(string varName)
@ -413,11 +410,10 @@ namespace CppSharp.Generators.CSharp
if (!VisitType(pointer, quals)) if (!VisitType(pointer, quals))
return false; return false;
var pointee = pointer.Pointee; var pointee = pointer.Pointee.Desugar();
Type type = pointee.Desugar(); if ((pointee.IsPrimitiveType(PrimitiveType.Char) ||
if ((type.IsPrimitiveType(PrimitiveType.Char) || pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
type.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst) pointer.QualifiedPointee.Qualifiers.IsConst)
{ {
Context.Return.Write(MarshalStringToUnmanaged( Context.Return.Write(MarshalStringToUnmanaged(
@ -434,7 +430,7 @@ namespace CppSharp.Generators.CSharp
} }
Class @class; Class @class;
if (type.IsTagDecl(out @class) && @class.IsValueType) if (pointee.IsTagDecl(out @class) && @class.IsValueType)
{ {
if (Context.Parameter.Usage == ParameterUsage.Out) if (Context.Parameter.Usage == ParameterUsage.Out)
{ {
@ -454,7 +450,7 @@ namespace CppSharp.Generators.CSharp
} }
PrimitiveType primitive; PrimitiveType primitive;
if (type.IsPrimitiveType(out primitive)) if (pointee.IsPrimitiveType(out primitive))
{ {
var param = Context.Parameter; var param = Context.Parameter;
@ -464,7 +460,7 @@ namespace CppSharp.Generators.CSharp
if (param.IsOut || param.IsInOut) if (param.IsOut || param.IsInOut)
{ {
var typeName = Type.TypePrinterDelegate(type); var typeName = Type.TypePrinterDelegate(pointee);
Context.SupportBefore.WriteLine("{0} _{1};", typeName, Context.SupportBefore.WriteLine("{0} _{1};", typeName,
Helpers.SafeIdentifier(param.Name)); Helpers.SafeIdentifier(param.Name));
@ -477,7 +473,7 @@ namespace CppSharp.Generators.CSharp
return true; return true;
} }
return pointee.Visit(this, quals); return pointer.Pointee.Visit(this, quals);
} }
private string MarshalStringToUnmanaged(string varName) private string MarshalStringToUnmanaged(string varName)

Loading…
Cancel
Save