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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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,

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

@ -144,7 +144,7 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)

Loading…
Cancel
Save