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; @@ -8,27 +8,17 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.CSharp
{
public enum CSharpMarshalKind
{
Unknown,
NativeField,
GenericDelegate,
DefaultExpression,
VTableReturnValue,
Variable
}
public class CSharpMarshalContext : MarshalContext
{
public CSharpMarshalContext(BindingContext context)
: base(context)
{
Kind = CSharpMarshalKind.Unknown;
Kind = MarshalKind.Unknown;
ArgumentPrefix = new TextGenerator();
Cleanup = new TextGenerator();
}
public CSharpMarshalKind Kind { get; set; }
public MarshalKind Kind { get; set; }
public QualifiedType FullType;
public TextGenerator ArgumentPrefix { get; private set; }
@ -231,7 +221,7 @@ namespace CppSharp.Generators.CSharp @@ -231,7 +221,7 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.Char16:
return false;
case PrimitiveType.Bool:
if (Context.Kind == CSharpMarshalKind.NativeField)
if (Context.Kind == MarshalKind.NativeField)
{
// returned structs must be blittable and bool isn't
Context.Return.Write("{0} != 0", Context.ReturnVarName);
@ -503,7 +493,7 @@ namespace CppSharp.Generators.CSharp @@ -503,7 +493,7 @@ namespace CppSharp.Generators.CSharp
var pointee = pointer.Pointee.Desugar();
if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() &&
Context.Kind != CSharpMarshalKind.VTableReturnValue)
Context.Kind != MarshalKind.VTableReturnValue)
{
var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex);
var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType;
@ -609,9 +599,9 @@ namespace CppSharp.Generators.CSharp @@ -609,9 +599,9 @@ namespace CppSharp.Generators.CSharp
Context.Return.Write(string.Format("({0}) ", pointer.Visit(typePrinter)));
typePrinter.PopContext();
}
if (marshalAsString && (Context.Kind == CSharpMarshalKind.NativeField ||
Context.Kind == CSharpMarshalKind.VTableReturnValue ||
Context.Kind == CSharpMarshalKind.Variable))
if (marshalAsString && (Context.Kind == MarshalKind.NativeField ||
Context.Kind == MarshalKind.VTableReturnValue ||
Context.Kind == MarshalKind.Variable))
Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
else
Context.Return.Write(Context.Parameter.Name);
@ -656,7 +646,7 @@ namespace CppSharp.Generators.CSharp @@ -656,7 +646,7 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.Char16:
return false;
case PrimitiveType.Bool:
if (Context.Kind == CSharpMarshalKind.NativeField)
if (Context.Kind == MarshalKind.NativeField)
{
// returned structs must be blittable and bool isn't
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 @@ -784,7 +784,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("[FieldOffset({0})]", field.Offset);
TypePrinter.PushMarshalKind(CSharpMarshalKind.NativeField);
TypePrinter.PushMarshalKind(MarshalKind.NativeField);
var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter);
TypePrinter.PopMarshalKind();
@ -890,7 +890,7 @@ namespace CppSharp.Generators.CSharp @@ -890,7 +890,7 @@ namespace CppSharp.Generators.CSharp
NewLine();
WriteStartBraceIndent();
ctx.Kind = CSharpMarshalKind.NativeField;
ctx.Kind = MarshalKind.NativeField;
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.Declaration = field;
@ -956,7 +956,7 @@ namespace CppSharp.Generators.CSharp @@ -956,7 +956,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PopContext();
ctx.Kind = CSharpMarshalKind.Variable;
ctx.Kind = MarshalKind.Variable;
ctx.ReturnType = new QualifiedType(var.Type);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
@ -1108,7 +1108,7 @@ namespace CppSharp.Generators.CSharp @@ -1108,7 +1108,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var ctx = new CSharpMarshalContext(Context)
{
Kind = CSharpMarshalKind.NativeField,
Kind = MarshalKind.NativeField,
ArgName = decl.Name,
Declaration = decl,
ReturnVarName = $@"{(@class.IsValueType ? Helpers.InstanceField :
@ -1683,7 +1683,7 @@ namespace CppSharp.Generators.CSharp @@ -1683,7 +1683,7 @@ namespace CppSharp.Generators.CSharp
ArgName = Helpers.ReturnIdentifier,
Parameter = param,
Function = method,
Kind = CSharpMarshalKind.VTableReturnValue
Kind = MarshalKind.VTableReturnValue
};
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);

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

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

10
src/Generator/Generators/Marshal.cs

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

2
src/Generator/Passes/DelegatesPass.cs

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

4
src/Generator/Passes/HandleDefaultParamValuesPass.cs

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

2
tests/CSharp/CSharp.cs

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

Loading…
Cancel
Save