diff --git a/src/AST/ITypePrinter.cs b/src/AST/ITypePrinter.cs index f5068dd6..e4f3223e 100644 --- a/src/AST/ITypePrinter.cs +++ b/src/AST/ITypePrinter.cs @@ -7,7 +7,9 @@ namespace CppSharp.AST public enum TypePrinterContextKind { Normal, - Template + Template, + Native, + Managed } public abstract class TypePrinterContext diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 9477380a..fafe7827 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -605,7 +605,7 @@ namespace CppSharp.Generators.CSharp Context.Context.Options.MarshalCharAsManagedChar && primitive == PrimitiveType.Char) { - typePrinter.PushContext(CSharpTypePrinterContextKind.Native); + typePrinter.PushContext(TypePrinterContextKind.Native); Context.Return.Write(string.Format("({0}) ", pointer.Visit(typePrinter))); typePrinter.PopContext(); } @@ -779,7 +779,7 @@ namespace CppSharp.Generators.CSharp } var realClass = @class.OriginalClass ?? @class; - typePrinter.PushContext(CSharpTypePrinterContextKind.Native); + typePrinter.PushContext(TypePrinterContextKind.Native); var qualifiedIdentifier = realClass.Visit(typePrinter); typePrinter.PopContext(); Context.Return.Write( diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 39cedb32..f81e52a9 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -577,7 +577,7 @@ namespace CppSharp.Generators.CSharp GenerateClassInternalHead(@class); WriteStartBraceIndent(); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); foreach (var field in @class.Layout.Fields) GenerateClassInternalsField(field); @@ -665,7 +665,7 @@ namespace CppSharp.Generators.CSharp private IEnumerable GatherInternalParams(Function function, out CSharpTypePrinterResult retType) { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); retType = function.ReturnType.CSharpType(TypePrinter); @@ -904,7 +904,7 @@ namespace CppSharp.Generators.CSharp else { var name = @class.Layout.Fields.First(f => f.FieldPtr == field.OriginalPtr).Name; - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); ctx.ReturnVarName = $@"{(@class.IsValueType ? Helpers.InstanceField : $"(({@class.Visit(TypePrinter)}*) {Helpers.InstanceIdentifier})")}{ (@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}"; @@ -938,7 +938,7 @@ namespace CppSharp.Generators.CSharp WriteStartBraceIndent(); var var = decl as Variable; - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var location = $@"CppSharp.SymbolResolver.ResolveSymbol(""{ GetLibraryOf(decl)}"", ""{var.Mangled}"")"; @@ -989,7 +989,7 @@ namespace CppSharp.Generators.CSharp if (Context.Options.MarshalCharAsManagedChar && isChar) { var typePrinter = new CSharpTypePrinter(Context); - typePrinter.PushContext(CSharpTypePrinterContextKind.Native); + typePrinter.PushContext(TypePrinterContextKind.Native); type = originalType.Visit(typePrinter).Type; } else @@ -1104,7 +1104,7 @@ namespace CppSharp.Generators.CSharp WriteStartBraceIndent(); var name = @class.Layout.Fields.First(f => f.FieldPtr == field.OriginalPtr).Name; - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local); var ctx = new CSharpMarshalContext(Context) { @@ -1158,7 +1158,7 @@ namespace CppSharp.Generators.CSharp WriteStartBraceIndent(); var var = decl as Variable; - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var location = string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")", GetLibraryOf(decl), var.Mangled); @@ -1442,7 +1442,7 @@ namespace CppSharp.Generators.CSharp WriteLine("if (__OriginalVTables != null)"); WriteLineIndent("return;"); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); WriteLine($"var native = ({@class.Visit(TypePrinter)}*) {Helpers.InstanceIdentifier}.ToPointer();"); TypePrinter.PopContext(); NewLine(); @@ -1516,7 +1516,7 @@ namespace CppSharp.Generators.CSharp private void SaveOriginalVTablePointers(Class @class, bool cast = false) { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var pointer = cast ? $@"(({@class.Visit(TypePrinter)}*) native)" : "native"; if (Context.ParserOptions.IsMicrosoftAbi) WriteLine("__OriginalVTables = new void*[] {{ {0} }};", @@ -1695,7 +1695,7 @@ namespace CppSharp.Generators.CSharp if (method.HasIndirectReturnTypeParameter) { var retParam = method.Parameters.First(p => p.Kind == ParameterKind.IndirectReturnType); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); WriteLine("*({0}*) {1} = {2};", method.OriginalReturnType.Visit(TypePrinter), retParam.Name, marshal.Context.Return); TypePrinter.PopContext(); @@ -1782,7 +1782,7 @@ namespace CppSharp.Generators.CSharp private void GenerateEvent(Event @event) { PushBlock(CSharpBlockKind.Event, @event); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true); TypePrinter.PopContext(); @@ -1850,7 +1850,7 @@ namespace CppSharp.Generators.CSharp private void GenerateEventRaiseWrapper(Event @event, string delegateInstance) { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true); TypePrinter.PopContext(); @@ -1973,7 +1973,7 @@ namespace CppSharp.Generators.CSharp Helpers.DummyIdentifier); WriteLine("NativeToManagedMap.TryRemove({0}, out {1});", Helpers.InstanceIdentifier, Helpers.DummyIdentifier); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var classInternal = @class.Visit(TypePrinter); TypePrinter.PopContext(); if (@class.IsDynamic && GetUniqueVTableMethodEntries(@class).Count != 0) @@ -2101,7 +2101,7 @@ namespace CppSharp.Generators.CSharp } else { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); WriteLine($"{Helpers.InstanceField} = *({@class.Visit(TypePrinter)}*) native;"); TypePrinter.PopContext(); } @@ -2114,7 +2114,7 @@ namespace CppSharp.Generators.CSharp private void GenerateNativeConstructorByValue(Class @class, string ctorCall) { TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter); TypePrinter.PopContext(); @@ -2141,7 +2141,7 @@ namespace CppSharp.Generators.CSharp { // Allocate memory for a new native object and call the ctor. WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); WriteLine($"{@class.Visit(TypePrinter)}.{GetFunctionNativeIdentifier(copyCtorMethod)}(ret, new global::System.IntPtr(&native));", @class.Visit(TypePrinter), GetFunctionNativeIdentifier(copyCtorMethod)); @@ -2443,7 +2443,7 @@ namespace CppSharp.Generators.CSharp { WriteLine("if ({0} == global::System.IntPtr.Zero)", Helpers.InstanceIdentifier); WriteLineIndent("return global::System.IntPtr.Zero.GetHashCode();"); - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); WriteLine($@"return (*({@class.Visit(TypePrinter)}*) { Helpers.InstanceIdentifier}).GetHashCode();"); TypePrinter.PopContext(); @@ -2614,7 +2614,7 @@ namespace CppSharp.Generators.CSharp GenerateInternalFunctionCall(method); else { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local); var classInternal = @class.Visit(TypePrinter); TypePrinter.PopContext(); @@ -2643,7 +2643,7 @@ namespace CppSharp.Generators.CSharp string @internal = Helpers.InternalStruct; if (@class != null && @class is ClassTemplateSpecialization) { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); @internal = @class.Visit(TypePrinter).Type; TypePrinter.PopContext(); } @@ -2704,7 +2704,7 @@ namespace CppSharp.Generators.CSharp if (construct == null) { var @class = retClass.OriginalClass ?? retClass; - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); WriteLine($"var {Helpers.ReturnIdentifier} = new {@class.Visit(TypePrinter)}();"); TypePrinter.PopContext(); } @@ -3046,7 +3046,7 @@ namespace CppSharp.Generators.CSharp var callingConvention = attributedType == null ? functionType.CallingConvention : ((FunctionType) attributedType.Equivalent.Type).CallingConvention; - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); + TypePrinter.PushContext(TypePrinterContextKind.Native); var interopCallConv = callingConvention.ToInteropCallConv(); if (interopCallConv == System.Runtime.InteropServices.CallingConvention.Winapi) WriteLine("[SuppressUnmanagedCodeSecurity]"); diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 0ccaa3da..d4573bd4 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -10,15 +10,9 @@ using System.Text; namespace CppSharp.Generators.CSharp { - public enum CSharpTypePrinterContextKind - { - Native, - Managed - } - public class CSharpTypePrinterContext : TypePrinterContext { - public CSharpTypePrinterContextKind CSharpKind; + public TypePrinterContextKind CSharpKind; public CSharpMarshalKind MarshalKind; public QualifiedType FullType; } @@ -42,11 +36,11 @@ namespace CppSharp.Generators.CSharp { public const string IntPtrType = "global::System.IntPtr"; - private readonly Stack contexts; + private readonly Stack contexts; private readonly Stack marshalKinds; private readonly Stack printScopeKinds; - public CSharpTypePrinterContextKind ContextKind => contexts.Peek(); + public TypePrinterContextKind ContextKind => contexts.Peek(); public CSharpMarshalKind MarshalKind => marshalKinds.Peek(); @@ -62,22 +56,22 @@ namespace CppSharp.Generators.CSharp public CSharpTypePrinter(BindingContext context) { Context = context; - contexts = new Stack(); + contexts = new Stack(); marshalKinds = new Stack(); printScopeKinds = new Stack(); - PushContext(CSharpTypePrinterContextKind.Managed); + PushContext(TypePrinterContextKind.Managed); PushMarshalKind(CSharpMarshalKind.Unknown); PushPrintScopeKind(TypePrintScopeKind.GlobalQualified); TypePrinterContext = new CSharpTypePrinterContext(); } - public void PushContext(CSharpTypePrinterContextKind contextKind) + public void PushContext(TypePrinterContextKind contextKind) { contexts.Push(contextKind); } - public CSharpTypePrinterContextKind PopContext() => contexts.Pop(); + public TypePrinterContextKind PopContext() => contexts.Pop(); public void PushMarshalKind(CSharpMarshalKind marshalKind) { @@ -123,7 +117,7 @@ namespace CppSharp.Generators.CSharp public CSharpTypePrinterResult VisitArrayType(ArrayType array, TypeQualifiers quals) { - if (ContextKind == CSharpTypePrinterContextKind.Native && + if (ContextKind == TypePrinterContextKind.Native && array.SizeType == ArrayType.ArraySize.Constant) { Type arrayType = array.Type.Desugar(); @@ -187,7 +181,7 @@ namespace CppSharp.Generators.CSharp return string.Format("{0}{1}", array.Type.Visit(this), array.SizeType == ArrayType.ArraySize.Constant ? "[]" : - (ContextKind == CSharpTypePrinterContextKind.Managed ? "*" : string.Empty)); + (ContextKind == TypePrinterContextKind.Managed ? "*" : string.Empty)); // C# only supports fixed arrays in unsafe sections // and they are constrained to a set of built-in types. @@ -207,7 +201,7 @@ namespace CppSharp.Generators.CSharp PopMarshalKind(); - if (ContextKind != CSharpTypePrinterContextKind.Managed) + if (ContextKind != TypePrinterContextKind.Managed) return IntPtrType; if (returnType.Type.IsPrimitiveType(PrimitiveType.Void)) @@ -268,7 +262,7 @@ namespace CppSharp.Generators.CSharp return string.Format("{0}", function.Visit(this, quals)); } - var isManagedContext = ContextKind == CSharpTypePrinterContextKind.Managed; + var isManagedContext = ContextKind == TypePrinterContextKind.Managed; if (allowStrings && IsConstCharString(pointer)) { @@ -333,7 +327,7 @@ namespace CppSharp.Generators.CSharp Class @class; if ((desugared.IsDependent || desugared.TryGetClass(out @class) || (desugared is ArrayType && TypePrinterContext.Parameter != null)) - && ContextKind == CSharpTypePrinterContextKind.Native) + && ContextKind == TypePrinterContextKind.Native) { return IntPtrType; } @@ -386,7 +380,7 @@ namespace CppSharp.Generators.CSharp FunctionType func; if (decl.Type.IsPointerTo(out func)) { - if (ContextKind == CSharpTypePrinterContextKind.Native) + if (ContextKind == TypePrinterContextKind.Native) return IntPtrType; // TODO: Use SafeIdentifier() return VisitDeclaration(decl); @@ -567,7 +561,7 @@ namespace CppSharp.Generators.CSharp case PrimitiveType.Char: // returned structs must be blittable and char isn't return Options.MarshalCharAsManagedChar && - ContextKind != CSharpTypePrinterContextKind.Native + ContextKind != TypePrinterContextKind.Native ? "char" : "sbyte"; case PrimitiveType.SChar: return "sbyte"; @@ -609,7 +603,7 @@ namespace CppSharp.Generators.CSharp public CSharpTypePrinterResult VisitClassDecl(Class @class) { - if (ContextKind == CSharpTypePrinterContextKind.Native) + if (ContextKind == TypePrinterContextKind.Native) { if (PrintScopeKind == TypePrintScopeKind.Local) return Helpers.InternalStruct; @@ -621,7 +615,7 @@ namespace CppSharp.Generators.CSharp public CSharpTypePrinterResult VisitClassTemplateSpecializationDecl(ClassTemplateSpecialization specialization) { - if (ContextKind == CSharpTypePrinterContextKind.Native) + if (ContextKind == TypePrinterContextKind.Native) return $@"{VisitClassDecl(specialization)}{ Helpers.GetSuffixForInternal(specialization)}"; return VisitClassDecl(specialization); @@ -685,7 +679,7 @@ namespace CppSharp.Generators.CSharp Declaration ctx; var specialization = decl as ClassTemplateSpecialization; - if (specialization != null && ContextKind == CSharpTypePrinterContextKind.Native) + if (specialization != null && ContextKind == TypePrinterContextKind.Native) { ctx = specialization.TemplatedDecl.TemplatedClass.Namespace; if (specialization.OriginalNamespace is Class && diff --git a/src/Generator/Passes/DelegatesPass.cs b/src/Generator/Passes/DelegatesPass.cs index a16e9ca6..8acddb55 100644 --- a/src/Generator/Passes/DelegatesPass.cs +++ b/src/Generator/Passes/DelegatesPass.cs @@ -172,7 +172,7 @@ namespace CppSharp.Passes { typePrinter = new CSharpTypePrinter(Context); typePrinter.PushPrintScopeKind(TypePrintScopeKind.Qualified); - typePrinter.PushContext(CSharpTypePrinterContextKind.Native); + typePrinter.PushContext(TypePrinterContextKind.Native); typePrinter.PushMarshalKind(CSharpMarshalKind.GenericDelegate); } return typePrinter; diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index 70dfa48c..9e5981fc 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -49,11 +49,11 @@ namespace CppSharp.Types.Std public override string CSharpSignature(CSharpTypePrinterContext ctx) { - if (ctx.CSharpKind == CSharpTypePrinterContextKind.Managed) + if (ctx.CSharpKind == TypePrinterContextKind.Managed) return "string"; ClassTemplateSpecialization basicString = GetBasicString(ctx.Type); var typePrinter = new CSharpTypePrinter(null); - typePrinter.PushContext(CSharpTypePrinterContextKind.Native); + typePrinter.PushContext(TypePrinterContextKind.Native); return basicString.Visit(typePrinter).Type; } @@ -62,7 +62,7 @@ namespace CppSharp.Types.Std var type = ctx.Parameter.Type.Desugar(); ClassTemplateSpecialization basicString = GetBasicString(type); var typePrinter = new CSharpTypePrinter(ctx.Context); - typePrinter.PushContext(CSharpTypePrinterContextKind.Native); + typePrinter.PushContext(TypePrinterContextKind.Native); if (!ctx.Parameter.Type.Desugar().IsAddress()) ctx.Return.Write("*({0}*) ", basicString.Visit(typePrinter)); typePrinter.PopContext(); @@ -294,7 +294,7 @@ namespace CppSharp.Types.Std public override string CSharpSignature(CSharpTypePrinterContext ctx) { - if (ctx.CSharpKind == CSharpTypePrinterContextKind.Native) + if (ctx.CSharpKind == TypePrinterContextKind.Native) return "Std.Vector"; return string.Format("Std.Vector<{0}>", ctx.GetTemplateParameterList()); @@ -340,7 +340,7 @@ namespace CppSharp.Types.Std public override string CSharpSignature(CSharpTypePrinterContext ctx) { - if (ctx.CSharpKind == CSharpTypePrinterContextKind.Native) + if (ctx.CSharpKind == TypePrinterContextKind.Native) return "Std.Map"; var type = Type as TemplateSpecializationType; diff --git a/tests/CSharp/CSharp.cs b/tests/CSharp/CSharp.cs index d6ac7089..ada9381c 100644 --- a/tests/CSharp/CSharp.cs +++ b/tests/CSharp/CSharp.cs @@ -81,7 +81,7 @@ namespace CppSharp.Tests public override string CSharpSignature(CSharpTypePrinterContext ctx) { - if (ctx.CSharpKind == CSharpTypePrinterContextKind.Native) + if (ctx.CSharpKind == TypePrinterContextKind.Native) return string.Format("QList.{0}{1}", Helpers.InternalStruct, Type.IsAddress() ? "*" : string.Empty);