diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index fafe7827..5d1cb0de 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -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 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 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 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 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); diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index f69f5777..3ef688ea 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -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 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 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 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 ArgName = Helpers.ReturnIdentifier, Parameter = param, Function = method, - Kind = CSharpMarshalKind.VTableReturnValue + Kind = MarshalKind.VTableReturnValue }; var marshal = new CSharpMarshalManagedToNativePrinter(ctx); diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 8508a5b5..52f319a3 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -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 public const string IntPtrType = "global::System.IntPtr"; private readonly Stack contexts; - private readonly Stack marshalKinds; + private readonly Stack marshalKinds; private readonly Stack 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 { Context = context; contexts = new Stack(); - marshalKinds = new Stack(); + marshalKinds = new Stack(); printScopeKinds = new Stack(); PushContext(TypePrinterContextKind.Managed); - PushMarshalKind(CSharpMarshalKind.Unknown); + PushMarshalKind(MarshalKind.Unknown); PushPrintScopeKind(TypePrintScopeKind.GlobalQualified); TypePrinterContext = new CSharpTypePrinterContext(); @@ -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 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 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 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 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 { 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: diff --git a/src/Generator/Generators/Marshal.cs b/src/Generator/Generators/Marshal.cs index 3fee9940..304bc9c8 100644 --- a/src/Generator/Generators/Marshal.cs +++ b/src/Generator/Generators/Marshal.cs @@ -2,6 +2,16 @@ namespace CppSharp.Generators { + public enum MarshalKind + { + Unknown, + NativeField, + GenericDelegate, + DefaultExpression, + VTableReturnValue, + Variable + } + public class MarshalContext { public MarshalContext(BindingContext context) diff --git a/src/Generator/Passes/DelegatesPass.cs b/src/Generator/Passes/DelegatesPass.cs index 8acddb55..ec768f9c 100644 --- a/src/Generator/Passes/DelegatesPass.cs +++ b/src/Generator/Passes/DelegatesPass.cs @@ -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; } diff --git a/src/Generator/Passes/HandleDefaultParamValuesPass.cs b/src/Generator/Passes/HandleDefaultParamValuesPass.cs index 066de86b..ca23b66a 100644 --- a/src/Generator/Passes/HandleDefaultParamValuesPass.cs +++ b/src/Generator/Passes/HandleDefaultParamValuesPass.cs @@ -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 { var typeInSignature = typeMap.CSharpSignatureType(new CSharpTypePrinterContext { - MarshalKind = CSharpMarshalKind.DefaultExpression, + MarshalKind = MarshalKind.DefaultExpression, Type = desugared }).SkipPointerRefs().Desugar(); Enumeration @enum; diff --git a/tests/CSharp/CSharp.cs b/tests/CSharp/CSharp.cs index ada9381c..ae125bed 100644 --- a/tests/CSharp/CSharp.cs +++ b/tests/CSharp/CSharp.cs @@ -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()); }