Browse Source

Rename CSharpMarshalKind to MarshalKind and move it to Marshal.cs.

pull/761/head
Joao Matos 9 years ago
parent
commit
3629eead5d
  1. 26
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 10
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 24
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 10
      src/Generator/Generators/Marshal.cs
  5. 2
      src/Generator/Passes/DelegatesPass.cs
  6. 4
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  7. 2
      tests/CSharp/CSharp.cs

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

@ -8,27 +8,17 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.CSharp namespace CppSharp.Generators.CSharp
{ {
public enum CSharpMarshalKind
{
Unknown,
NativeField,
GenericDelegate,
DefaultExpression,
VTableReturnValue,
Variable
}
public class CSharpMarshalContext : MarshalContext public class CSharpMarshalContext : MarshalContext
{ {
public CSharpMarshalContext(BindingContext context) public CSharpMarshalContext(BindingContext context)
: base(context) : base(context)
{ {
Kind = CSharpMarshalKind.Unknown; Kind = MarshalKind.Unknown;
ArgumentPrefix = new TextGenerator(); ArgumentPrefix = new TextGenerator();
Cleanup = new TextGenerator(); Cleanup = new TextGenerator();
} }
public CSharpMarshalKind Kind { get; set; } public MarshalKind Kind { get; set; }
public QualifiedType FullType; public QualifiedType FullType;
public TextGenerator ArgumentPrefix { get; private set; } public TextGenerator ArgumentPrefix { get; private set; }
@ -231,7 +221,7 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.Char16: case PrimitiveType.Char16:
return false; return false;
case PrimitiveType.Bool: case PrimitiveType.Bool:
if (Context.Kind == CSharpMarshalKind.NativeField) if (Context.Kind == MarshalKind.NativeField)
{ {
// returned structs must be blittable and bool isn't // returned structs must be blittable and bool isn't
Context.Return.Write("{0} != 0", Context.ReturnVarName); Context.Return.Write("{0} != 0", Context.ReturnVarName);
@ -503,7 +493,7 @@ namespace CppSharp.Generators.CSharp
var pointee = pointer.Pointee.Desugar(); var pointee = pointer.Pointee.Desugar();
if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() && if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() &&
Context.Kind != CSharpMarshalKind.VTableReturnValue) Context.Kind != MarshalKind.VTableReturnValue)
{ {
var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex); var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex);
var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType; var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType;
@ -609,9 +599,9 @@ namespace CppSharp.Generators.CSharp
Context.Return.Write(string.Format("({0}) ", pointer.Visit(typePrinter))); Context.Return.Write(string.Format("({0}) ", pointer.Visit(typePrinter)));
typePrinter.PopContext(); typePrinter.PopContext();
} }
if (marshalAsString && (Context.Kind == CSharpMarshalKind.NativeField || if (marshalAsString && (Context.Kind == MarshalKind.NativeField ||
Context.Kind == CSharpMarshalKind.VTableReturnValue || Context.Kind == MarshalKind.VTableReturnValue ||
Context.Kind == CSharpMarshalKind.Variable)) Context.Kind == MarshalKind.Variable))
Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name)); Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
else else
Context.Return.Write(Context.Parameter.Name); Context.Return.Write(Context.Parameter.Name);
@ -656,7 +646,7 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.Char16: case PrimitiveType.Char16:
return false; return false;
case PrimitiveType.Bool: case PrimitiveType.Bool:
if (Context.Kind == CSharpMarshalKind.NativeField) if (Context.Kind == MarshalKind.NativeField)
{ {
// returned structs must be blittable and bool isn't // returned structs must be blittable and bool isn't
Context.Return.Write("(byte) ({0} ? 1 : 0)", Context.Parameter.Name); Context.Return.Write("(byte) ({0} ? 1 : 0)", Context.Parameter.Name);

10
src/Generator/Generators/CSharp/CSharpSources.cs

@ -784,7 +784,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("[FieldOffset({0})]", field.Offset); WriteLine("[FieldOffset({0})]", field.Offset);
TypePrinter.PushMarshalKind(CSharpMarshalKind.NativeField); TypePrinter.PushMarshalKind(MarshalKind.NativeField);
var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter); var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter);
TypePrinter.PopMarshalKind(); TypePrinter.PopMarshalKind();
@ -890,7 +890,7 @@ namespace CppSharp.Generators.CSharp
NewLine(); NewLine();
WriteStartBraceIndent(); WriteStartBraceIndent();
ctx.Kind = CSharpMarshalKind.NativeField; ctx.Kind = MarshalKind.NativeField;
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.Declaration = field; ctx.Declaration = field;
@ -956,7 +956,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PopContext(); TypePrinter.PopContext();
ctx.Kind = CSharpMarshalKind.Variable; ctx.Kind = MarshalKind.Variable;
ctx.ReturnType = new QualifiedType(var.Type); ctx.ReturnType = new QualifiedType(var.Type);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
@ -1108,7 +1108,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local); TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var ctx = new CSharpMarshalContext(Context) var ctx = new CSharpMarshalContext(Context)
{ {
Kind = CSharpMarshalKind.NativeField, Kind = MarshalKind.NativeField,
ArgName = decl.Name, ArgName = decl.Name,
Declaration = decl, Declaration = decl,
ReturnVarName = $@"{(@class.IsValueType ? Helpers.InstanceField : ReturnVarName = $@"{(@class.IsValueType ? Helpers.InstanceField :
@ -1683,7 +1683,7 @@ namespace CppSharp.Generators.CSharp
ArgName = Helpers.ReturnIdentifier, ArgName = Helpers.ReturnIdentifier,
Parameter = param, Parameter = param,
Function = method, Function = method,
Kind = CSharpMarshalKind.VTableReturnValue Kind = MarshalKind.VTableReturnValue
}; };
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);

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

@ -13,7 +13,7 @@ namespace CppSharp.Generators.CSharp
public class CSharpTypePrinterContext : TypePrinterContext public class CSharpTypePrinterContext : TypePrinterContext
{ {
public TypePrinterContextKind CSharpKind; public TypePrinterContextKind CSharpKind;
public CSharpMarshalKind MarshalKind; public MarshalKind MarshalKind;
public QualifiedType FullType; public QualifiedType FullType;
} }
@ -23,12 +23,12 @@ namespace CppSharp.Generators.CSharp
public const string IntPtrType = "global::System.IntPtr"; public const string IntPtrType = "global::System.IntPtr";
private readonly Stack<TypePrinterContextKind> contexts; private readonly Stack<TypePrinterContextKind> contexts;
private readonly Stack<CSharpMarshalKind> marshalKinds; private readonly Stack<MarshalKind> marshalKinds;
private readonly Stack<TypePrintScopeKind> printScopeKinds; private readonly Stack<TypePrintScopeKind> printScopeKinds;
public TypePrinterContextKind ContextKind => contexts.Peek(); public TypePrinterContextKind ContextKind => contexts.Peek();
public CSharpMarshalKind MarshalKind => marshalKinds.Peek(); public MarshalKind MarshalKind => marshalKinds.Peek();
public TypePrintScopeKind PrintScopeKind => printScopeKinds.Peek(); public TypePrintScopeKind PrintScopeKind => printScopeKinds.Peek();
@ -43,10 +43,10 @@ namespace CppSharp.Generators.CSharp
{ {
Context = context; Context = context;
contexts = new Stack<TypePrinterContextKind>(); contexts = new Stack<TypePrinterContextKind>();
marshalKinds = new Stack<CSharpMarshalKind>(); marshalKinds = new Stack<MarshalKind>();
printScopeKinds = new Stack<TypePrintScopeKind>(); printScopeKinds = new Stack<TypePrintScopeKind>();
PushContext(TypePrinterContextKind.Managed); PushContext(TypePrinterContextKind.Managed);
PushMarshalKind(CSharpMarshalKind.Unknown); PushMarshalKind(MarshalKind.Unknown);
PushPrintScopeKind(TypePrintScopeKind.GlobalQualified); PushPrintScopeKind(TypePrintScopeKind.GlobalQualified);
TypePrinterContext = new CSharpTypePrinterContext(); TypePrinterContext = new CSharpTypePrinterContext();
@ -59,12 +59,12 @@ namespace CppSharp.Generators.CSharp
public TypePrinterContextKind PopContext() => contexts.Pop(); public TypePrinterContextKind PopContext() => contexts.Pop();
public void PushMarshalKind(CSharpMarshalKind marshalKind) public void PushMarshalKind(MarshalKind marshalKind)
{ {
marshalKinds.Push(marshalKind); marshalKinds.Push(marshalKind);
} }
public CSharpMarshalKind PopMarshalKind() => marshalKinds.Pop(); public MarshalKind PopMarshalKind() => marshalKinds.Pop();
public void PushPrintScopeKind(TypePrintScopeKind printScopeKind) public void PushPrintScopeKind(TypePrintScopeKind printScopeKind)
{ {
@ -111,7 +111,7 @@ namespace CppSharp.Generators.CSharp
PrimitiveType primitiveType; PrimitiveType primitiveType;
if ((arrayType.IsPointerToPrimitiveType(out primitiveType) && if ((arrayType.IsPointerToPrimitiveType(out primitiveType) &&
!(arrayType is FunctionType)) || !(arrayType is FunctionType)) ||
(arrayType.IsPrimitiveType() && MarshalKind != CSharpMarshalKind.NativeField)) (arrayType.IsPrimitiveType() && MarshalKind != MarshalKind.NativeField))
{ {
if (primitiveType == PrimitiveType.Void) if (primitiveType == PrimitiveType.Void)
{ {
@ -180,7 +180,7 @@ namespace CppSharp.Generators.CSharp
var returnType = function.ReturnType; var returnType = function.ReturnType;
var args = string.Empty; var args = string.Empty;
PushMarshalKind(CSharpMarshalKind.GenericDelegate); PushMarshalKind(MarshalKind.GenericDelegate);
if (arguments.Count > 0) if (arguments.Count > 0)
args = VisitParameters(function.Parameters, hasNames: false).Type; args = VisitParameters(function.Parameters, hasNames: false).Type;
@ -200,7 +200,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrEmpty(args)) if (!string.IsNullOrEmpty(args))
args = string.Format(", {0}", args); args = string.Format(", {0}", args);
PushMarshalKind(CSharpMarshalKind.GenericDelegate); PushMarshalKind(MarshalKind.GenericDelegate);
var returnTypePrinterResult = returnType.Visit(this); var returnTypePrinterResult = returnType.Visit(this);
@ -252,7 +252,7 @@ namespace CppSharp.Generators.CSharp
if (allowStrings && IsConstCharString(pointer)) if (allowStrings && IsConstCharString(pointer))
{ {
if (isManagedContext || MarshalKind == CSharpMarshalKind.GenericDelegate) if (isManagedContext || MarshalKind == MarshalKind.GenericDelegate)
return "string"; return "string";
if (TypePrinterContext.Parameter == null || TypePrinterContext.Parameter.Name == Helpers.ReturnIdentifier) if (TypePrinterContext.Parameter == null || TypePrinterContext.Parameter.Name == Helpers.ReturnIdentifier)
return IntPtrType; return IntPtrType;
@ -538,7 +538,7 @@ namespace CppSharp.Generators.CSharp
{ {
case PrimitiveType.Bool: case PrimitiveType.Bool:
// returned structs must be blittable and bool isn't // returned structs must be blittable and bool isn't
return MarshalKind == CSharpMarshalKind.NativeField ? return MarshalKind == MarshalKind.NativeField ?
"byte" : "bool"; "byte" : "bool";
case PrimitiveType.Void: return "void"; case PrimitiveType.Void: return "void";
case PrimitiveType.Char16: case PrimitiveType.Char16:

10
src/Generator/Generators/Marshal.cs

@ -2,6 +2,16 @@
namespace CppSharp.Generators namespace CppSharp.Generators
{ {
public enum MarshalKind
{
Unknown,
NativeField,
GenericDelegate,
DefaultExpression,
VTableReturnValue,
Variable
}
public class MarshalContext public class MarshalContext
{ {
public MarshalContext(BindingContext context) public MarshalContext(BindingContext context)

2
src/Generator/Passes/DelegatesPass.cs

@ -173,7 +173,7 @@ namespace CppSharp.Passes
typePrinter = new CSharpTypePrinter(Context); typePrinter = new CSharpTypePrinter(Context);
typePrinter.PushPrintScopeKind(TypePrintScopeKind.Qualified); typePrinter.PushPrintScopeKind(TypePrintScopeKind.Qualified);
typePrinter.PushContext(TypePrinterContextKind.Native); typePrinter.PushContext(TypePrinterContextKind.Native);
typePrinter.PushMarshalKind(CSharpMarshalKind.GenericDelegate); typePrinter.PushMarshalKind(MarshalKind.GenericDelegate);
} }
return typePrinter; return typePrinter;
} }

4
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -152,7 +152,7 @@ namespace CppSharp.Passes
TypeMap typeMap; TypeMap typeMap;
var typePrinter = new CSharpTypePrinter(Context); var typePrinter = new CSharpTypePrinter(Context);
typePrinter.PushMarshalKind(CSharpMarshalKind.DefaultExpression); typePrinter.PushMarshalKind(MarshalKind.DefaultExpression);
var typePrinterResult = type.Visit(typePrinter).Type; var typePrinterResult = type.Visit(typePrinter).Type;
if (TypeMaps.FindTypeMap(decl, type, out typeMap)) if (TypeMaps.FindTypeMap(decl, type, out typeMap))
{ {
@ -306,7 +306,7 @@ namespace CppSharp.Passes
{ {
var typeInSignature = typeMap.CSharpSignatureType(new CSharpTypePrinterContext var typeInSignature = typeMap.CSharpSignatureType(new CSharpTypePrinterContext
{ {
MarshalKind = CSharpMarshalKind.DefaultExpression, MarshalKind = MarshalKind.DefaultExpression,
Type = desugared Type = desugared
}).SkipPointerRefs().Desugar(); }).SkipPointerRefs().Desugar();
Enumeration @enum; Enumeration @enum;

2
tests/CSharp/CSharp.cs

@ -86,7 +86,7 @@ namespace CppSharp.Tests
Type.IsAddress() ? "*" : string.Empty); Type.IsAddress() ? "*" : string.Empty);
return string.Format("System.Collections.Generic.{0}<{1}>", return string.Format("System.Collections.Generic.{0}<{1}>",
ctx.MarshalKind == CSharpMarshalKind.DefaultExpression ? "List" : "IList", ctx.MarshalKind == MarshalKind.DefaultExpression ? "List" : "IList",
ctx.GetTemplateParameterList()); ctx.GetTemplateParameterList());
} }

Loading…
Cancel
Save