Browse Source

Reworked C# type printing and marshaling to keep the root full type in the respective contexts.

This allows us to access the root type in the type maps to do proper handling of pointers. It's a bit of an hacky approach and it will be improved once there is a better more general type matching framework.
pull/28/head
triton 12 years ago
parent
commit
e5ba3ac401
  1. 43
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 12
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 40
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

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

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Types; using CppSharp.Types;
@ -21,6 +20,7 @@ namespace CppSharp.Generators.CSharp
} }
public CSharpMarshalKind Kind { get; set; } public CSharpMarshalKind Kind { get; set; }
public QualifiedType FullType;
public TextGenerator Cleanup { get; private set; } public TextGenerator Cleanup { get; private set; }
} }
@ -436,4 +436,45 @@ namespace CppSharp.Generators.CSharp
return true; return true;
} }
} }
public static class CSharpMarshalExtensions
{
public static void CSharpMarshalToNative(this QualifiedType type,
CSharpMarshalManagedToNativePrinter printer)
{
(printer.Context as CSharpMarshalContext).FullType = type;
type.Visit(printer);
}
public static void CSharpMarshalToNative(this Type type,
CSharpMarshalManagedToNativePrinter printer)
{
CSharpMarshalToNative(new QualifiedType(type), printer);
}
public static void CSharpMarshalToNative(this ITypedDecl decl,
CSharpMarshalManagedToNativePrinter printer)
{
CSharpMarshalToNative(decl.QualifiedType, printer);
}
public static void CSharpMarshalToManaged(this QualifiedType type,
CSharpMarshalNativeToManagedPrinter printer)
{
(printer.Context as CSharpMarshalContext).FullType = type;
type.Visit(printer);
}
public static void CSharpMarshalToManaged(this Type type,
CSharpMarshalNativeToManagedPrinter printer)
{
CSharpMarshalToManaged(new QualifiedType(type), printer);
}
public static void CSharpMarshalToManaged(this ITypedDecl decl,
CSharpMarshalNativeToManagedPrinter printer)
{
CSharpMarshalToManaged(decl.QualifiedType, printer);
}
}
} }

12
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -1673,7 +1673,7 @@ namespace CppSharp.Generators.CSharp
}; };
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx); var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
function.ReturnType.Type.Visit(marshal, function.ReturnType.Qualifiers); function.ReturnType.CSharpMarshalToManaged(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
@ -1788,7 +1788,7 @@ namespace CppSharp.Generators.CSharp
paramMarshal.Context = ctx; paramMarshal.Context = ctx;
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
param.Visit(marshal); param.CSharpMarshalToNative(marshal);
if (string.IsNullOrEmpty(marshal.Context.Return)) if (string.IsNullOrEmpty(marshal.Context.Return))
throw new Exception("Cannot marshal argument of function"); throw new Exception("Cannot marshal argument of function");
@ -2089,10 +2089,8 @@ namespace CppSharp.Generators.CSharp
var typePrinter = TypePrinter as CSharpTypePrinter; var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native); typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
var retType = typePrinter.VisitParameterDecl(new Parameter() var retParam = new Parameter { QualifiedType = function.ReturnType };
{ retType = retParam.CSharpType(typePrinter);
QualifiedType = function.ReturnType
});
var method = function as Method; var method = function as Method;
if (method != null && !method.IsStatic) if (method != null && !method.IsStatic)
@ -2110,7 +2108,7 @@ namespace CppSharp.Generators.CSharp
if (param.Kind == ParameterKind.OperatorParameter) if (param.Kind == ParameterKind.OperatorParameter)
continue; continue;
var typeName = param.Visit(typePrinter); var typeName = param.CSharpType(typePrinter);
var paramName = param.IsSynthetized ? var paramName = param.IsSynthetized ?
GeneratedIdentifier(param.Name) : SafeIdentifier(param.Name); GeneratedIdentifier(param.Name) : SafeIdentifier(param.Name);

40
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -16,6 +16,7 @@ namespace CppSharp.Generators.CSharp
public class CSharpTypePrinterContext : TypePrinterContext public class CSharpTypePrinterContext : TypePrinterContext
{ {
public CSharpTypePrinterContextKind CSharpKind; public CSharpTypePrinterContextKind CSharpKind;
public QualifiedType FullType;
public CSharpTypePrinterContext() public CSharpTypePrinterContext()
{ {
@ -214,14 +215,12 @@ namespace CppSharp.Generators.CSharp
if (TypeMapDatabase.FindTypeMap(decl, out typeMap)) if (TypeMapDatabase.FindTypeMap(decl, out typeMap))
{ {
typeMap.Type = typedef; typeMap.Type = typedef;
var ctx = new CSharpTypePrinterContext Context.CSharpKind = ContextKind;
{ Context.Type = typedef;
CSharpKind = ContextKind,
Type = typedef
};
return new CSharpTypePrinterResult() return new CSharpTypePrinterResult()
{ {
Type = typeMap.CSharpSignature(ctx), Type = typeMap.CSharpSignature(Context),
TypeMap = typeMap TypeMap = typeMap
}; };
} }
@ -255,6 +254,7 @@ namespace CppSharp.Generators.CSharp
typeMap.Type = template; typeMap.Type = template;
Context.Type = template; Context.Type = template;
Context.CSharpKind = ContextKind; Context.CSharpKind = ContextKind;
return new CSharpTypePrinterResult() return new CSharpTypePrinterResult()
{ {
Type = GetCSharpSignature(typeMap), Type = GetCSharpSignature(typeMap),
@ -474,4 +474,32 @@ namespace CppSharp.Generators.CSharp
return type.Visit(this).Type; return type.Visit(this).Type;
} }
} }
public static class CSharpTypePrinterExtensions
{
public static CSharpTypePrinterResult CSharpType(this QualifiedType type,
CSharpTypePrinter printer)
{
printer.Context.FullType = type;
return type.Visit(printer);
}
public static CSharpTypePrinterResult CSharpType(this Type type,
CSharpTypePrinter printer)
{
return CSharpType(new QualifiedType(type), printer);
}
public static CSharpTypePrinterResult CSharpType(this Declaration decl,
CSharpTypePrinter printer)
{
if (decl is ITypedDecl)
{
var type = (decl as ITypedDecl).QualifiedType;
printer.Context.FullType = type;
}
return decl.Visit(printer);
}
}
} }
Loading…
Cancel
Save