From 6268b1c3c8be7dc5a28290ea52f3c5e5ebec73ab Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Sun, 3 Dec 2023 03:51:05 +0200 Subject: [PATCH 01/12] TypeMap: prepare refactoring into a modular design --- src/Generator/Generators/QuickJS/QuickJSMarshal.cs | 14 +++++++------- src/Generator/Types/TypeMap.cs | 9 ++++++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 5de2ad048a..4937192bfe 100644 --- a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs +++ b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs @@ -25,7 +25,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context); + typeMap.MarshalToManaged(Context); return false; } @@ -210,7 +210,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context); + typeMap.MarshalToManaged(Context); return typeMap.IsValueType; } @@ -232,7 +232,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context); + typeMap.MarshalToManaged(Context); return true; } @@ -385,7 +385,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); + typeMap.MarshalToNative(Context); return false; } @@ -583,7 +583,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); + typeMap.MarshalToNative(Context); return typeMap.IsValueType; } @@ -625,7 +625,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); + typeMap.MarshalToNative(Context); return true; } @@ -672,7 +672,7 @@ private void MarshalRefClass(Class @class) if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); + typeMap.MarshalToNative(Context); return; } diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index e7060192ab..7834552c44 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -51,8 +51,9 @@ public class TypeMap /// public virtual bool DoesMarshalling => true; - public virtual Type SignatureType(GeneratorKind kind, TypePrinterContext ctx) + public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind = null) { + kind ??= Context.Options.GeneratorKind; switch (kind) { case var _ when ReferenceEquals(kind, GeneratorKind.C): @@ -67,8 +68,9 @@ public virtual Type SignatureType(GeneratorKind kind, TypePrinterContext ctx) } } - public virtual void MarshalToNative(GeneratorKind kind, MarshalContext ctx) + public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = null) { + kind ??= Context.Options.GeneratorKind; switch (kind) { case var _ when ReferenceEquals(kind, GeneratorKind.C): @@ -86,8 +88,9 @@ public virtual void MarshalToNative(GeneratorKind kind, MarshalContext ctx) } } - public virtual void MarshalToManaged(GeneratorKind kind, MarshalContext ctx) + public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind = null) { + kind ??= Context.Options.GeneratorKind; switch (kind) { case var _ when ReferenceEquals(kind, GeneratorKind.C): From 8271415e8a3162ae0d98643f9620312e310eeef1 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Sun, 3 Dec 2023 04:00:13 +0200 Subject: [PATCH 02/12] TypeMap: refactor C++ backend into common methods --- src/Generator/Generators/C/CppMarshal.cs | 14 ++++----- src/Generator/Generators/C/CppTypePrinter.cs | 2 +- src/Generator/Generators/NAPI/NAPIMarshal.cs | 12 ++++---- src/Generator/Types/TypeMap.cs | 30 ++------------------ tests/dotnet/CLI/CLI.Gen.cs | 7 ----- 5 files changed, 17 insertions(+), 48 deletions(-) diff --git a/src/Generator/Generators/C/CppMarshal.cs b/src/Generator/Generators/C/CppMarshal.cs index e0425e9f17..d60a5d6564 100644 --- a/src/Generator/Generators/C/CppMarshal.cs +++ b/src/Generator/Generators/C/CppMarshal.cs @@ -25,7 +25,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return false; } @@ -173,7 +173,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.CppMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return typeMap.IsValueType; } @@ -193,7 +193,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.CppMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return true; } @@ -341,7 +341,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return false; } @@ -478,7 +478,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return typeMap.IsValueType; } @@ -516,7 +516,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return true; } @@ -563,7 +563,7 @@ private void MarshalRefClass(Class @class) if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return; } diff --git a/src/Generator/Generators/C/CppTypePrinter.cs b/src/Generator/Generators/C/CppTypePrinter.cs index edfcdcd911..a8ea5caf56 100644 --- a/src/Generator/Generators/C/CppTypePrinter.cs +++ b/src/Generator/Generators/C/CppTypePrinter.cs @@ -68,7 +68,7 @@ public virtual bool FindTypeMap(CppSharp.AST.Type type, out TypePrinterResult re typePrinter.PushContext(ContextKind); typePrinter.PushScope(ScopeKind); - var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter); + var typeName = typeMap.SignatureType(typePrinterContext).Visit(typePrinter); result = new TypePrinterResult(typeName) { TypeMap = typeMap }; return true; diff --git a/src/Generator/Generators/NAPI/NAPIMarshal.cs b/src/Generator/Generators/NAPI/NAPIMarshal.cs index c0cf70082e..3eb23b528d 100644 --- a/src/Generator/Generators/NAPI/NAPIMarshal.cs +++ b/src/Generator/Generators/NAPI/NAPIMarshal.cs @@ -26,7 +26,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return false; } @@ -194,7 +194,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.CppMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return typeMap.IsValueType; } @@ -214,7 +214,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.CppMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return true; } @@ -343,7 +343,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return false; } @@ -591,7 +591,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return typeMap.IsValueType; } @@ -628,7 +628,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.CppMarshalToNative(Context); + typeMap.MarshalToNative(Context); return true; } diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 7834552c44..282abe6005 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -58,7 +58,7 @@ public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind = n { case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - return CppSignatureType(ctx); + return new CILType(typeof(object)); case var _ when ReferenceEquals(kind, GeneratorKind.CLI): return CLISignatureType(ctx); case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): @@ -75,7 +75,7 @@ public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = nul { case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - CppMarshalToNative(ctx); + ctx.Return.Write(ctx.Parameter.Name); return; case var _ when ReferenceEquals(kind, GeneratorKind.CLI): CLIMarshalToNative(ctx); @@ -95,7 +95,7 @@ public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind = nu { case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - CppMarshalToManaged(ctx); + ctx.Return.Write(ctx.ReturnVarName); return; case var _ when ReferenceEquals(kind, GeneratorKind.CLI): CLIMarshalToManaged(ctx); @@ -158,30 +158,6 @@ public virtual void CLIMarshalToManaged(MarshalContext ctx) } #endregion - - #region C++ backend - - public virtual Type CppSignatureType(TypePrinterContext ctx) - { - return new CILType(typeof(object)); - } - - public virtual void CppTypeReference(CLITypeReference collector, ASTRecord record) - { - throw new NotImplementedException(); - } - - public virtual void CppMarshalToNative(MarshalContext ctx) - { - ctx.Return.Write(ctx.Parameter.Name); - } - - public virtual void CppMarshalToManaged(MarshalContext ctx) - { - ctx.Return.Write(ctx.ReturnVarName); - } - - #endregion } public interface ITypeMapDatabase diff --git a/tests/dotnet/CLI/CLI.Gen.cs b/tests/dotnet/CLI/CLI.Gen.cs index 3ec8c1dc88..5bd1a77c04 100644 --- a/tests/dotnet/CLI/CLI.Gen.cs +++ b/tests/dotnet/CLI/CLI.Gen.cs @@ -29,13 +29,6 @@ public override Type CLISignatureType(TypePrinterContext ctx) return new CILType(typeof(string)); } - public override Type CppSignatureType(TypePrinterContext ctx) - { - var tagType = ctx.Type as TagType; - var typePrinter = new CppTypePrinter(Context); - return new CustomType(tagType.Declaration.Visit(typePrinter)); - } - public override void CLIMarshalToManaged(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); From e667b2cc7bb3eedc1ec97354feec81a9162fa4b0 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Mon, 4 Dec 2023 05:45:50 +0200 Subject: [PATCH 03/12] TypeMap: refactor CLI backend into common methods --- src/Generator/Generators/CLI/CLIMarshal.cs | 14 +++--- .../Generators/CLI/CLITypePrinter.cs | 8 +-- src/Generator/Generators/ExtensionMethods.cs | 2 +- src/Generator/Types/Std/Stdlib.CLI.cs | 45 +++++++---------- src/Generator/Types/TypeMap.cs | 49 ++++++++----------- tests/dotnet/CLI/CLI.Gen.cs | 14 +++--- tests/dotnet/Common/Common.Gen.cs | 16 +++--- 7 files changed, 67 insertions(+), 81 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index c641651754..7e52b9bd25 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -22,7 +22,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CLIMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return false; } @@ -215,7 +215,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.CLIMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return typeMap.IsValueType; } @@ -240,7 +240,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.CLIMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return true; } @@ -406,7 +406,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CLIMarshalToNative(Context); + typeMap.MarshalToNative(Context); return false; } @@ -605,7 +605,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CLIMarshalToNative(Context); + typeMap.MarshalToNative(Context); return typeMap.IsValueType; } @@ -641,7 +641,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = template; - typeMap.CLIMarshalToNative(Context); + typeMap.MarshalToNative(Context); return true; } @@ -688,7 +688,7 @@ private void MarshalRefClass(Class @class) if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CLIMarshalToNative(Context); + typeMap.MarshalToNative(Context); return; } diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index 783a44342e..970130f5da 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -21,7 +21,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals if (TypeMapDatabase.FindTypeMap(tag, out typeMap)) { var typePrinterContext = new TypePrinterContext { Type = tag }; - return typeMap.CLISignatureType(typePrinterContext).ToString(); + return typeMap.SignatureType(typePrinterContext).ToString(); } Declaration decl = tag.Declaration; @@ -112,7 +112,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer, Type = pointer }; - return typeMap.CLISignatureType(typePrinterContext).Visit(this); + return typeMap.SignatureType(typePrinterContext).Visit(this); } var pointee = pointer.Pointee.Desugar(); @@ -217,7 +217,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef, { typeMap.Type = typedef; var typePrinterContext = new TypePrinterContext { Type = typedef }; - return typeMap.CLISignatureType(typePrinterContext).ToString(); + return typeMap.SignatureType(typePrinterContext).ToString(); } FunctionType func; @@ -241,7 +241,7 @@ public override TypePrinterResult VisitTemplateSpecializationType( if (TypeMapDatabase.FindTypeMap(template, out typeMap) && !typeMap.IsIgnored) { var typePrinterContext = new TypePrinterContext { Type = template }; - return typeMap.CLISignatureType(typePrinterContext).ToString(); + return typeMap.SignatureType(typePrinterContext).ToString(); } return decl.Name; diff --git a/src/Generator/Generators/ExtensionMethods.cs b/src/Generator/Generators/ExtensionMethods.cs index ee4b1054d0..3e4e9e2743 100644 --- a/src/Generator/Generators/ExtensionMethods.cs +++ b/src/Generator/Generators/ExtensionMethods.cs @@ -66,7 +66,7 @@ public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps, switch (generatorKind) { case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI): - return typeMap.CLISignatureType(typePrinterContext).Desugar(); + return typeMap.SignatureType(typePrinterContext).Desugar(); case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp): return typeMap.CSharpSignatureType(typePrinterContext).Desugar(); } diff --git a/src/Generator/Types/Std/Stdlib.CLI.cs b/src/Generator/Types/Std/Stdlib.CLI.cs index 8db2058f92..554ae09ba4 100644 --- a/src/Generator/Types/Std/Stdlib.CLI.cs +++ b/src/Generator/Types/Std/Stdlib.CLI.cs @@ -12,12 +12,12 @@ namespace CppSharp.Types.Std [TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)] public partial class ConstCharPointer : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(string)); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Before.WriteLine( "auto _{0} = clix::marshalString({1});", @@ -26,7 +26,7 @@ public override void CLIMarshalToNative(MarshalContext ctx) ctx.Return.Write("_{0}.c_str()", ctx.ArgName); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { if (ctx.Parameter != null && !ctx.Parameter.IsOut && !ctx.Parameter.IsInOut) @@ -85,18 +85,18 @@ public partial class ConstChar32TPointer : ConstCharPointer [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CLI_ID)] public partial class String : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(string)); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write("clix::marshalString({0})", ctx.Parameter.Name); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write("clix::marshalString({0})", ctx.ReturnVarName); @@ -106,18 +106,18 @@ public override void CLIMarshalToManaged(MarshalContext ctx) [TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)] public partial class WString : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(string)); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write("clix::marshalString({0})", ctx.Parameter.Name); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write("clix::marshalString({0})", ctx.ReturnVarName); @@ -145,13 +145,13 @@ public override bool IsIgnored } } - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CustomType( $"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^"); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { var desugared = Type.Desugar(); var templateType = desugared as TemplateSpecializationType; @@ -209,7 +209,7 @@ public override void CLIMarshalToNative(MarshalContext ctx) ctx.Return.Write(tmpVarName); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { var desugared = Type.Desugar(); var templateType = desugared as TemplateSpecializationType; @@ -263,7 +263,7 @@ public partial class Map : TypeMap { public override bool IsIgnored { get { return true; } } - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { var type = Type as TemplateSpecializationType; return new CustomType( @@ -271,26 +271,15 @@ public override Type CLISignatureType(TypePrinterContext ctx) type.Arguments[0].Type}, {type.Arguments[1].Type}>^"); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { throw new System.NotImplementedException(); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { throw new System.NotImplementedException(); } - - public override Type CSharpSignatureType(TypePrinterContext ctx) - { - if (ctx.Kind == TypePrinterContextKind.Native) - return new CustomType("Std.Map"); - - var type = Type as TemplateSpecializationType; - return new CustomType( - $@"System.Collections.Generic.Dictionary<{ - type.Arguments[0].Type}, {type.Arguments[1].Type}>"); - } } [TypeMap("std::list", GeneratorKindID = GeneratorKind.CLI_ID)] @@ -308,12 +297,12 @@ public partial class SharedPtr : TypeMap [TypeMap("basic_ostream>", GeneratorKind.CLI_ID)] public partial class OStream : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(System.IO.TextWriter)); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative; if (!ctx.Parameter.Type.Desugar().IsPointer()) diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 282abe6005..04614adc7a 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -51,16 +51,19 @@ public class TypeMap /// public virtual bool DoesMarshalling => true; - public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind = null) + public Type SignatureType(TypePrinterContext ctx) + { + return SignatureType(ctx, Context.Options.GeneratorKind); + } + + public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { - kind ??= Context.Options.GeneratorKind; switch (kind) { case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - return new CILType(typeof(object)); case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - return CLISignatureType(ctx); + return new CILType(typeof(object)); case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): return CSharpSignatureType(ctx); default: @@ -68,17 +71,20 @@ public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind = n } } - public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = null) + public void MarshalToNative(MarshalContext ctx) + { + MarshalToNative(ctx, Context.Options.GeneratorKind); + } + + public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { kind ??= Context.Options.GeneratorKind; switch (kind) { case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - ctx.Return.Write(ctx.Parameter.Name); - return; case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - CLIMarshalToNative(ctx); + ctx.Return.Write(ctx.Parameter.Name); return; case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): CSharpMarshalToNative(ctx as CSharpMarshalContext); @@ -88,17 +94,19 @@ public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = nul } } - public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind = null) + public void MarshalToManaged(MarshalContext ctx) + { + MarshalToManaged(ctx, Context.Options.GeneratorKind); + } + + public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { - kind ??= Context.Options.GeneratorKind; switch (kind) { case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - ctx.Return.Write(ctx.ReturnVarName); - return; case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - CLIMarshalToManaged(ctx); + ctx.Return.Write(ctx.ReturnVarName); return; case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): CSharpMarshalToManaged(ctx as CSharpMarshalContext); @@ -138,25 +146,10 @@ public virtual string CSharpConstruct() #region C++/CLI backend - public virtual Type CLISignatureType(TypePrinterContext ctx) - { - return new CILType(typeof(object)); - } - public virtual void CLITypeReference(CLITypeReferenceCollector collector, ASTRecord loc) { } - public virtual void CLIMarshalToNative(MarshalContext ctx) - { - ctx.Return.Write(ctx.Parameter.Name); - } - - public virtual void CLIMarshalToManaged(MarshalContext ctx) - { - ctx.Return.Write(ctx.ReturnVarName); - } - #endregion } diff --git a/tests/dotnet/CLI/CLI.Gen.cs b/tests/dotnet/CLI/CLI.Gen.cs index 5bd1a77c04..de4b7ee339 100644 --- a/tests/dotnet/CLI/CLI.Gen.cs +++ b/tests/dotnet/CLI/CLI.Gen.cs @@ -7,34 +7,34 @@ namespace CppSharp.Tests { - [TypeMap("IgnoredClassTemplateForEmployee")] + [TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)] public class IgnoredClassTemplateForEmployeeMap : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CustomType("CLI::Employee^"); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)"); } } - [TypeMap("TestMappedTypeNonConstRefParam")] + [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)] public class TestMappedTypeNonConstRefParamTypeMap : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(string)); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { if (ctx.Parameter.Usage == ParameterUsage.InOut) { diff --git a/tests/dotnet/Common/Common.Gen.cs b/tests/dotnet/Common/Common.Gen.cs index 0f1c47d10f..73217b8631 100644 --- a/tests/dotnet/Common/Common.Gen.cs +++ b/tests/dotnet/Common/Common.Gen.cs @@ -7,24 +7,28 @@ namespace CppSharp.Tests { - [TypeMap("TypeMappedIndex")] - public class TypeMappedIndex : TypeMap + [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)] + public class CLITypeMappedIndex : TypeMap { - public override Type CLISignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new BuiltinType(PrimitiveType.UShort); } - public override void CLIMarshalToManaged(MarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.ReturnVarName); } - public override void CLIMarshalToNative(MarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { - ctx.Return.Write("::TypeMappedIndex()"); + ctx.Return.Write("::TypeMappedIndex1()"); } + } + [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)] + public class TypeMappedIndex : TypeMap + { public override Type CSharpSignatureType(TypePrinterContext ctx) { return new BuiltinType(PrimitiveType.UShort); From 473ecf9ed3fb249c0b15d5ed93ec3ae973e40a86 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Mon, 4 Dec 2023 05:59:30 +0200 Subject: [PATCH 04/12] CLI.Gen.cs: fix omitted typemap from previous commit --- tests/dotnet/CLI/CLI.Gen.cs | 54 ++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/tests/dotnet/CLI/CLI.Gen.cs b/tests/dotnet/CLI/CLI.Gen.cs index de4b7ee339..5a5d25c7a1 100644 --- a/tests/dotnet/CLI/CLI.Gen.cs +++ b/tests/dotnet/CLI/CLI.Gen.cs @@ -1,7 +1,6 @@ using CppSharp.AST; using CppSharp.Generators; using CppSharp.Generators.C; -using CppSharp.Passes; using CppSharp.Types; using CppSharp.Utils; @@ -21,32 +20,49 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) } } - [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)] - public class TestMappedTypeNonConstRefParamTypeMap : TypeMap + namespace CLI { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) - { - return new CILType(typeof(string)); - } - - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)] + public class TestMappedTypeNonConstRefParamTypeMap : TypeMap { - ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); - } + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + { + return new CILType(typeof(string)); + } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) - { - if (ctx.Parameter.Usage == ParameterUsage.InOut) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { - ctx.Before.WriteLine($"System::String^ _{ctx.Parameter.Name} = {ctx.Parameter.Name};"); + ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); } - string paramName = ctx.Parameter.Usage == ParameterUsage.InOut ? $"_{ctx.Parameter.Name}" : ctx.Parameter.Name; + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + { + if (ctx.Parameter.Usage == ParameterUsage.InOut) + { + ctx.Before.WriteLine($"System::String^ _{ctx.Parameter.Name} = {ctx.Parameter.Name};"); + } - ctx.Before.WriteLine( - $"::TestMappedTypeNonConstRefParam _{ctx.ArgName} = clix::marshalString({paramName});"); + string paramName = ctx.Parameter.Usage == ParameterUsage.InOut ? $"_{ctx.Parameter.Name}" : ctx.Parameter.Name; - ctx.Return.Write("_{0}", ctx.ArgName); + ctx.Before.WriteLine( + $"::TestMappedTypeNonConstRefParam _{ctx.ArgName} = clix::marshalString({paramName});"); + + ctx.Return.Write("_{0}", ctx.ArgName); + } + } + } + + namespace Cpp + { + [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CPlusPlus_ID)] + public class TestMappedTypeNonConstRefParamTypeMap : TypeMap + { + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + { + var tagType = ctx.Type as TagType; + var typePrinter = new CppTypePrinter(Context); + return new CustomType(tagType.Declaration.Visit(typePrinter)); + } } } From 5cb553a1043fe026dec3e705480afc700b907392 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Mon, 4 Dec 2023 07:26:38 +0200 Subject: [PATCH 05/12] Common.Gen.cs: fixed silly modification while testing --- tests/dotnet/Common/Common.Gen.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dotnet/Common/Common.Gen.cs b/tests/dotnet/Common/Common.Gen.cs index 73217b8631..7ab6cbea8d 100644 --- a/tests/dotnet/Common/Common.Gen.cs +++ b/tests/dotnet/Common/Common.Gen.cs @@ -22,7 +22,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { - ctx.Return.Write("::TypeMappedIndex1()"); + ctx.Return.Write("::TypeMappedIndex()"); } } From c1f392f02f55cefc735bc4c484ff9858cd3a902b Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Tue, 5 Dec 2023 06:41:39 +0200 Subject: [PATCH 06/12] GeneratorKind: add FindGeneratorKindByID method --- src/Generator/GeneratorKind.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Generator/GeneratorKind.cs b/src/Generator/GeneratorKind.cs index 9307452f59..200ed11e49 100644 --- a/src/Generator/GeneratorKind.cs +++ b/src/Generator/GeneratorKind.cs @@ -38,6 +38,11 @@ public GeneratorKind(string id, string name, System.Type generatorType, System.T Registered.Add(this); } + public static GeneratorKind FindGeneratorKindByID(string id) + { + return Registered.Where(kind => kind.ID == id).First(); + } + public Generator CreateGenerator(BindingContext context) { return (Generator)Activator.CreateInstance(GeneratorType, context); From bf5a6ea0ebb3b46757ed005e98ac9838b2687ebf Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Tue, 5 Dec 2023 06:44:39 +0200 Subject: [PATCH 07/12] TypeMapDatabase: heavy refactor: group typemaps by GeneratorKind --- src/Generator/Types/TypeMap.cs | 2 + src/Generator/Types/TypeMapDatabase.cs | 70 +++++++++++++++----------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 04614adc7a..65dc8568d1 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -156,6 +156,8 @@ public virtual void CLITypeReference(CLITypeReferenceCollector collector, ASTRec public interface ITypeMapDatabase { bool FindTypeMap(Type decl, out TypeMap typeMap); + bool FindTypeMap(Type decl, GeneratorKind kind, out TypeMap typeMap); bool FindTypeMap(Declaration declaration, out TypeMap typeMap); + bool FindTypeMap(Declaration declaration, GeneratorKind kind, out TypeMap typeMap); } } diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index 96711ae69a..466a25f02a 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -10,13 +10,16 @@ namespace CppSharp.Types { public class TypeMapDatabase : ITypeMapDatabase { - public IDictionary TypeMaps { get; set; } private readonly BindingContext Context; + private readonly Dictionary> typeMapsCache = new(); + + public Dictionary> GlobalTypeMaps { get; private set; } + public Dictionary TypeMaps => TypeMapsByKind(GlobalTypeMaps, Context.Options.GeneratorKind); public TypeMapDatabase(BindingContext bindingContext) { Context = bindingContext; - TypeMaps = new Dictionary(); + GlobalTypeMaps = new Dictionary>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { try @@ -32,33 +35,22 @@ public TypeMapDatabase(BindingContext bindingContext) } } - private void SetupTypeMaps(IEnumerable types, - BindingContext bindingContext) + public static Dictionary TypeMapsByKind(Dictionary> globalTypeMaps, GeneratorKind kind) { - foreach (var type in types) + if (!globalTypeMaps.TryGetValue(kind, out Dictionary typeMap)) { - var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true); - foreach (TypeMapAttribute attr in attrs) - { - if (string.IsNullOrEmpty(attr.GeneratorKindID) || - attr.GeneratorKindID == bindingContext.Options.GeneratorKind.ID) - { - var typeMap = (TypeMap)Activator.CreateInstance(type); - typeMap.Context = bindingContext; - typeMap.TypeMapDatabase = this; - - // Custom types won't be overwritten by CppSharp ones. - if (!TypeMaps.ContainsKey(attr.Type)) - { - TypeMaps.Add(attr.Type, typeMap); - } - } - } + typeMap = new Dictionary(); + globalTypeMaps.Add(kind, typeMap); } + return typeMap; } - public bool FindTypeMap(Type type, out TypeMap typeMap) + public bool FindTypeMap(Type type, out TypeMap typeMap) => + FindTypeMap(type, Context.Options.GeneratorKind, out typeMap); + + public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap) { + var typeMaps = TypeMapsByKind(typeMapsCache, kind); // Looks up the type in the cache map. if (typeMaps.ContainsKey(type)) { @@ -113,7 +105,7 @@ public bool FindTypeMap(Type type, out TypeMap typeMap) typePrinter.PushScope(typePrintScopeKind); var typeName = type.Visit(typePrinter); typePrinter.PopScope(); - if (FindTypeMap(typeName, out typeMap)) + if (FindTypeMap(typeName, kind, out typeMap)) { typeMap.Type = type; typeMaps[type] = typeMap; @@ -127,11 +119,33 @@ public bool FindTypeMap(Type type, out TypeMap typeMap) } public bool FindTypeMap(Declaration declaration, out TypeMap typeMap) => - FindTypeMap(new TagType(declaration), out typeMap); + FindTypeMap(declaration, Context.Options.GeneratorKind, out typeMap); + + public bool FindTypeMap(Declaration declaration, GeneratorKind kind, out TypeMap typeMap) => + FindTypeMap(new TagType(declaration), kind, out typeMap); - public bool FindTypeMap(string name, out TypeMap typeMap) => - TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled; + public bool FindTypeMap(string name, GeneratorKind kind, out TypeMap typeMap) => + TypeMapsByKind(GlobalTypeMaps, kind).TryGetValue(name, out typeMap) && typeMap.IsEnabled; - private Dictionary typeMaps = new Dictionary(); + private void SetupTypeMaps(IEnumerable types, BindingContext bindingContext) + { + foreach (var type in types) + { + var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true); + foreach (TypeMapAttribute attr in attrs) + { + var kind = string.IsNullOrEmpty(attr.GeneratorKindID) ? Context.Options.GeneratorKind : GeneratorKind.FindGeneratorKindByID(attr.GeneratorKindID); + var typeMaps = TypeMapsByKind(GlobalTypeMaps, kind); + // Custom types won't be overwritten by CppSharp ones. + if (!typeMaps.ContainsKey(attr.Type)) + { + var typeMap = (TypeMap)Activator.CreateInstance(type); + typeMap.Context = bindingContext; + typeMap.TypeMapDatabase = this; + typeMaps.Add(attr.Type, typeMap); + } + } + } + } } } From 15ecf3bbbd9109e9ca59435b7e25968ebec839d9 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Tue, 5 Dec 2023 07:53:44 +0200 Subject: [PATCH 08/12] TypeMap: refactor CSharp backend into common methods + migration --- src/Generator/AST/Utils.cs | 2 +- .../Generators/CSharp/CSharpMarshal.cs | 4 +- .../Generators/CSharp/CSharpSources.cs | 2 +- .../Generators/CSharp/CSharpTypePrinter.cs | 20 ++--- src/Generator/Generators/ExtensionMethods.cs | 7 +- src/Generator/Passes/ExpressionHelper.cs | 12 +-- src/Generator/Passes/ValidateOperatorsPass.cs | 4 +- src/Generator/Types/Std/Stdlib.CLI.cs | 35 ++++---- src/Generator/Types/Std/Stdlib.CSharp.cs | 82 +++++++++---------- src/Generator/Types/TypeMap.cs | 26 +----- tests/dotnet/CSharp/CSharp.Gen.cs | 36 ++++---- tests/dotnet/Common/Common.Gen.cs | 62 +++++++------- 12 files changed, 132 insertions(+), 160 deletions(-) diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 9074614ac9..e5a3107047 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -207,7 +207,7 @@ public static bool IsMappedToPrimitive(ITypeMapDatabase typeMaps, Type type) return false; var typePrinterContext = new TypePrinterContext { Type = type }; - var mappedTo = typeMap.CSharpSignatureType(typePrinterContext); + var mappedTo = typeMap.SignatureType(typePrinterContext); mappedTo = mappedTo.Desugar(); mappedTo = (mappedTo.GetFinalPointee() ?? mappedTo).Desugar(); return (mappedTo.IsPrimitiveType() || diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 09ced7d119..3dfbd1e837 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -45,7 +45,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CSharpMarshalToManaged(Context); + typeMap.MarshalToManaged(Context); return false; } @@ -471,7 +471,7 @@ public override bool VisitType(Type type, TypeQualifiers quals) TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.CSharpMarshalToNative(Context); + typeMap.MarshalToNative(Context); return false; } diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 8623ad3cec..d8152864fc 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -3158,7 +3158,7 @@ public void GenerateFunctionCall(string functionName, Function function, Type = indirectRetType.Type.Desugar() }; - WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext), + WriteLine("{0} {1};", typeMap.SignatureType(typePrinterContext), Helpers.ReturnIdentifier); } else diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 1453adb1fa..9b1b855353 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -36,7 +36,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals return string.Empty; TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(tag, out typeMap)) + if (TypeMapDatabase.FindTypeMap(tag, GeneratorKind.CSharp, out typeMap)) { typeMap.Type = tag; @@ -47,7 +47,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals Type = tag }; - return typeMap.CSharpSignatureType(typePrinterContext).ToString(); + return typeMap.SignatureType(typePrinterContext).ToString(); } return base.VisitTagType(tag, quals); @@ -150,7 +150,7 @@ public override TypePrinterResult VisitArrayType(ArrayType array, public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) { TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(builtin, out typeMap)) + if (TypeMapDatabase.FindTypeMap(builtin, GeneratorKind.CSharp, out typeMap)) { var typePrinterContext = new TypePrinterContext() { @@ -159,7 +159,7 @@ public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQual Type = builtin, Parameter = Parameter }; - return typeMap.CSharpSignatureType(typePrinterContext).Visit(this); + return typeMap.SignatureType(typePrinterContext).Visit(this); } return base.VisitBuiltinType(builtin, quals); } @@ -183,7 +183,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer, if (allowStrings && pointer.IsConstCharString()) { TypeMap typeMap; - TypeMapDatabase.FindTypeMap(pointer, out typeMap); + TypeMapDatabase.FindTypeMap(pointer, GeneratorKind.CSharp, out typeMap); var typePrinterContext = new TypePrinterContext() { Kind = ContextKind, @@ -191,7 +191,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer, Type = pointer.Pointee, Parameter = Parameter }; - return typeMap.CSharpSignatureType(typePrinterContext).Visit(this); + return typeMap.SignatureType(typePrinterContext).Visit(this); } var pointee = pointer.Pointee.Desugar(); @@ -258,7 +258,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef, var decl = typedef.Declaration; TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(typedef, out typeMap)) + if (TypeMapDatabase.FindTypeMap(typedef, GeneratorKind.CSharp, out typeMap)) { typeMap.Type = typedef; @@ -270,7 +270,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef, Parameter = Parameter }; - return typeMap.CSharpSignatureType(typePrinterContext).ToString(); + return typeMap.SignatureType(typePrinterContext).ToString(); } FunctionType func; @@ -299,7 +299,7 @@ public override TypePrinterResult VisitTemplateSpecializationType( template.Template.TemplatedDecl; TypeMap typeMap; - if (!TypeMapDatabase.FindTypeMap(template, out typeMap)) + if (!TypeMapDatabase.FindTypeMap(template, GeneratorKind.CSharp, out typeMap)) { if (ContextKind == TypePrinterContextKind.Managed && decl == template.Template.TemplatedDecl && @@ -330,7 +330,7 @@ public override TypePrinterResult VisitTemplateSpecializationType( MarshalKind = MarshalKind }; - return typeMap.CSharpSignatureType(typePrinterContext).ToString(); + return typeMap.SignatureType(typePrinterContext).ToString(); } public override TypePrinterResult VisitDependentTemplateSpecializationType( diff --git a/src/Generator/Generators/ExtensionMethods.cs b/src/Generator/Generators/ExtensionMethods.cs index 3e4e9e2743..c24a470381 100644 --- a/src/Generator/Generators/ExtensionMethods.cs +++ b/src/Generator/Generators/ExtensionMethods.cs @@ -63,12 +63,9 @@ public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps, Type = typeMap.Type }; - switch (generatorKind) + if (generatorKind == GeneratorKind.CLI || generatorKind == GeneratorKind.CSharp) { - case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI): - return typeMap.SignatureType(typePrinterContext).Desugar(); - case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp): - return typeMap.CSharpSignatureType(typePrinterContext).Desugar(); + return typeMap.SignatureType(typePrinterContext).Desugar(); } } diff --git a/src/Generator/Passes/ExpressionHelper.cs b/src/Generator/Passes/ExpressionHelper.cs index 435dbcf654..be5e924d7c 100644 --- a/src/Generator/Passes/ExpressionHelper.cs +++ b/src/Generator/Passes/ExpressionHelper.cs @@ -68,9 +68,9 @@ internal static class ExpressionHelper public static System.Type GetSystemType(BindingContext context, Type type) { - if (context.TypeMaps.FindTypeMap(type, out TypeMap typeMap)) + if (context.TypeMaps.FindTypeMap(type, GeneratorKind.CSharp, out TypeMap typeMap)) { - var cilType = typeMap.CSharpSignatureType(new TypePrinterContext { Type = type, Kind = TypePrinterContextKind.Managed }) as CILType; + var cilType = typeMap.SignatureType(new TypePrinterContext { Type = type, Kind = TypePrinterContextKind.Managed }) as CILType; if (cilType != null) return cilType.Type; } @@ -239,7 +239,7 @@ private static bool CheckForDefaultPointer(BindingContext context, Type desugare var typePrinterResult = type.Visit(typePrinter); TypeMap typeMap; - if (context.TypeMaps.FindTypeMap(type, out typeMap)) + if (context.TypeMaps.FindTypeMap(type, GeneratorKind.CSharp, out typeMap)) { var typePrinterContext = new TypePrinterContext() { @@ -248,7 +248,7 @@ private static bool CheckForDefaultPointer(BindingContext context, Type desugare Type = type }; - var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext) + var typeInSignature = typeMap.SignatureType(typePrinterContext) .SkipPointerRefs().Desugar(); Enumeration @enum; @@ -413,7 +413,7 @@ private static bool CheckForChar(BindingContext context, Type desugared, ref str private static bool CheckForString(BindingContext context, Type desugared, ref string result) { - if (context.TypeMaps.FindTypeMap(desugared, out TypeMap typeMap)) + if (context.TypeMaps.FindTypeMap(desugared, GeneratorKind.CSharp, out TypeMap typeMap)) { var typePrinterContext = new TypePrinterContext() { @@ -422,7 +422,7 @@ private static bool CheckForString(BindingContext context, Type desugared, ref s Type = desugared }; - var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext) + var typeInSignature = typeMap.SignatureType(typePrinterContext) .SkipPointerRefs().Desugar(); if (typeInSignature is CILType managed && managed.Type == typeof(string)) diff --git a/src/Generator/Passes/ValidateOperatorsPass.cs b/src/Generator/Passes/ValidateOperatorsPass.cs index 49fe8d017e..690bb092a8 100644 --- a/src/Generator/Passes/ValidateOperatorsPass.cs +++ b/src/Generator/Passes/ValidateOperatorsPass.cs @@ -90,9 +90,9 @@ private bool IsValidOperatorOverload(Method @operator) return type.IsPrimitiveType(PrimitiveType.Int); case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): Types.TypeMap typeMap; - if (Context.TypeMaps.FindTypeMap(type, out typeMap)) + if (Context.TypeMaps.FindTypeMap(type, GeneratorKind.CSharp, out typeMap)) { - var mappedTo = typeMap.CSharpSignatureType( + var mappedTo = typeMap.SignatureType( new TypePrinterContext { Parameter = parameter, diff --git a/src/Generator/Types/Std/Stdlib.CLI.cs b/src/Generator/Types/Std/Stdlib.CLI.cs index 554ae09ba4..454a4523d3 100644 --- a/src/Generator/Types/Std/Stdlib.CLI.cs +++ b/src/Generator/Types/Std/Stdlib.CLI.cs @@ -5,12 +5,11 @@ using CppSharp.Generators.AST; using CppSharp.Generators.C; using CppSharp.Generators.CLI; -using CppSharp.Generators.CSharp; -namespace CppSharp.Types.Std +namespace CppSharp.Types.Std.CLI { [TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class ConstCharPointer : TypeMap + public class ConstCharPointer : TypeMap { public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { @@ -57,33 +56,32 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) $"{Context.Options.Encoding.EncodingName} is not supported yet."); ctx.Return.Write( - $@"({ctx.ReturnVarName} == 0 ? nullptr : clix::marshalString({ctx.ReturnVarName}))"); + $@"({ctx.ReturnVarName} == 0 ? nullptr : clix::marshalString({ctx.ReturnVarName}))"); } } [TypeMap("const char[]", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class ConstCharArray : ConstCharPointer + public class ConstCharArray : ConstCharPointer { } [TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class ConstWCharTPointer : ConstCharPointer + public class ConstWCharTPointer : ConstCharPointer { } [TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class ConstChar16TPointer : ConstCharPointer + public class ConstChar16TPointer : ConstCharPointer { } [TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class ConstChar32TPointer : ConstCharPointer + public class ConstChar32TPointer : ConstCharPointer { } [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class String : TypeMap + public class String : TypeMap { public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { @@ -104,7 +102,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) } [TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class WString : TypeMap + public class WString : TypeMap { public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { @@ -125,7 +123,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) } [TypeMap("std::vector", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class Vector : TypeMap + public class Vector : TypeMap { public override bool IsIgnored { @@ -259,7 +257,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) } [TypeMap("std::map", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class Map : TypeMap + public class Map : TypeMap { public override bool IsIgnored { get { return true; } } @@ -267,8 +265,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { var type = Type as TemplateSpecializationType; return new CustomType( - $@"::System::Collections::Generic::Dictionary<{ - type.Arguments[0].Type}, {type.Arguments[1].Type}>^"); + $@"::System::Collections::Generic::Dictionary<{type.Arguments[0].Type}, {type.Arguments[1].Type}>^"); } public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) @@ -283,19 +280,19 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) } [TypeMap("std::list", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class List : TypeMap + public class List : TypeMap { public override bool IsIgnored { get { return true; } } } [TypeMap("std::shared_ptr", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class SharedPtr : TypeMap + public class SharedPtr : TypeMap { public override bool IsIgnored { get { return true; } } } [TypeMap("basic_ostream>", GeneratorKind.CLI_ID)] - public partial class OStream : TypeMap + public class OStream : TypeMap { public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { @@ -315,7 +312,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) } [TypeMap("std::nullptr_t", GeneratorKindID = GeneratorKind.CLI_ID)] - public partial class NullPtr : TypeMap + public class NullPtr : TypeMap { public override bool DoesMarshalling { get { return false; } } diff --git a/src/Generator/Types/Std/Stdlib.CSharp.cs b/src/Generator/Types/Std/Stdlib.CSharp.cs index a3c3d6d5a7..459905134c 100644 --- a/src/Generator/Types/Std/Stdlib.CSharp.cs +++ b/src/Generator/Types/Std/Stdlib.CSharp.cs @@ -9,46 +9,46 @@ using CppSharp.Generators.CSharp; using Type = CppSharp.AST.Type; -namespace CppSharp.Types.Std +namespace CppSharp.Types.Std.CSharp { [TypeMap("int", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class Int : TypeMap + public class Int : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) => + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => CSharpTypePrinter.GetSignedType(Context.TargetInfo.IntWidth); } [TypeMap("unsigned int", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class UnsignedInt : TypeMap + public class UnsignedInt : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) => + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.IntWidth); } [TypeMap("long", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class Long : TypeMap + public class Long : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) => + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => CSharpTypePrinter.GetSignedType(Context.TargetInfo.LongWidth); } [TypeMap("unsigned long", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class UnsignedLong : TypeMap + public class UnsignedLong : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) => + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.LongWidth); } [TypeMap("char", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class Char : TypeMap + public class Char : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(ctx.Kind == TypePrinterContextKind.Native || !Context.Options.MarshalCharAsManagedChar ? typeof(sbyte) : typeof(char)); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { if (Context.Options.MarshalCharAsManagedChar) ctx.Return.Write("global::System.Convert.ToSByte({0})", @@ -57,7 +57,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) ctx.Return.Write(ctx.Parameter.Name); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { if (Context.Options.MarshalCharAsManagedChar) ctx.Return.Write("global::System.Convert.ToChar({0})", @@ -68,27 +68,27 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) } [TypeMap("char16_t", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class Char16T : TypeMap + public class Char16T : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(char)); } } [TypeMap("wchar_t", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class WCharT : TypeMap + public class WCharT : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(char)); } } [TypeMap("const char*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class ConstCharPointer : TypeMap + public class ConstCharPointer : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { if (ctx.Kind == TypePrinterContextKind.Managed) return new CILType(typeof(string)); @@ -117,7 +117,7 @@ public override Type CSharpSignatureType(TypePrinterContext ctx) $"{Context.Options.Encoding.EncodingName} is not supported yet."); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { string param = ctx.Parameter.Name; if (ctx.Parameter.Usage == ParameterUsage.Unknown && @@ -194,7 +194,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) ctx.Return.Write($"{bytePtr}"); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { if (ctx.Parameter != null && !ctx.Parameter.IsOut && !ctx.Parameter.IsInOut) @@ -282,29 +282,29 @@ public uint GetCharPtrWidth(PointerType pointer) } [TypeMap("const char[]", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class ConstCharArray : ConstCharPointer + public class ConstCharArray : ConstCharPointer { } [TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class ConstWCharTPointer : ConstCharPointer + public class ConstWCharTPointer : ConstCharPointer { } [TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class ConstChar16TPointer : ConstCharPointer + public class ConstChar16TPointer : ConstCharPointer { } [TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class ConstChar32TPointer : ConstCharPointer + public class ConstChar32TPointer : ConstCharPointer { } [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class String : TypeMap + public class String : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { if (ctx.Kind == TypePrinterContextKind.Managed) return new CILType(typeof(string)); @@ -316,7 +316,7 @@ public override Type CSharpSignatureType(TypePrinterContext ctx) return new CustomType(basicString.Visit(typePrinter).Type); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { Type type = ctx.Parameter.Type.Desugar(); ClassTemplateSpecialization basicString = GetBasicString(type); @@ -334,15 +334,13 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) var = Generator.GeneratedIdentifier(ctx.ArgName); ctx.Before.WriteLine($"fixed (void* {var} = &{ctx.ReturnVarName})"); ctx.Before.WriteOpenBraceAndIndent(); - ctx.HasCodeBlock = true; + (ctx as CSharpMarshalContext).HasCodeBlock = true; } else { var = $"&{ctx.ReturnVarName}"; } - ctx.Return.Write($@"{qualifiedBasicString}Extensions.{ - Helpers.InternalStruct}.{assign.Name}(new { - typePrinter.IntPtrType}({var}), "); + ctx.Return.Write($@"{qualifiedBasicString}Extensions.{Helpers.InternalStruct}.{assign.Name}(new {typePrinter.IntPtrType}({var}), "); if (ctx.Parameter.Type.IsTemplateParameterType()) ctx.Return.Write("(string) (object) "); ctx.Return.Write($"{ctx.Parameter.Name})"); @@ -351,22 +349,19 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) else { var varBasicString = $"__basicString{ctx.ParameterIndex}"; - ctx.Before.WriteLine($@"var {varBasicString} = new { - basicString.Visit(typePrinter)}();"); + ctx.Before.WriteLine($@"var {varBasicString} = new {basicString.Visit(typePrinter)}();"); - ctx.Before.Write($@"{qualifiedBasicString}Extensions.{ - assign.Name}({varBasicString}, "); + ctx.Before.Write($@"{qualifiedBasicString}Extensions.{assign.Name}({varBasicString}, "); if (ctx.Parameter.Type.IsTemplateParameterType()) ctx.Before.Write("(string) (object) "); ctx.Before.WriteLine($"{ctx.Parameter.Name});"); ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}"); - ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({ - (!Type.IsAddress() || ctx.Parameter?.IsIndirect == true ? "disposing: true, callNativeDtor:false" : string.Empty)});"); + ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({(!Type.IsAddress() || ctx.Parameter?.IsIndirect == true ? "disposing: true, callNativeDtor:false" : string.Empty)});"); } } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { var type = Type.Desugar(resolveTemplateSubstitution: false); ClassTemplateSpecialization basicString = GetBasicString(type); @@ -376,10 +371,7 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) string varBasicString = $"__basicStringRet{ctx.ParameterIndex}"; bool usePointer = type.IsAddress() || ctx.MarshalKind == MarshalKind.NativeField || ctx.MarshalKind == MarshalKind.ReturnVariableArray; - ctx.Before.WriteLine($@"var {varBasicString} = { - basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({ - (usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{ - ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});"); + ctx.Before.WriteLine($@"var {varBasicString} = {basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({(usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});"); string @string = $"{qualifiedBasicString}Extensions.{data.Name}({varBasicString})"; if (usePointer) { @@ -421,9 +413,9 @@ private static ClassTemplateSpecialization GetBasicString(Type type) } [TypeMap("FILE", GeneratorKindID = GeneratorKind.CSharp_ID)] - public partial class FILE : TypeMap + public class FILE : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new CILType(typeof(System.IntPtr)); } diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 65dc8568d1..4506ad7f4c 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -63,9 +63,8 @@ public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - return new CILType(typeof(object)); case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - return CSharpSignatureType(ctx); + return new CILType(typeof(object)); default: throw new System.NotImplementedException(); } @@ -84,10 +83,8 @@ public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind) case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - ctx.Return.Write(ctx.Parameter.Name); - return; case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - CSharpMarshalToNative(ctx as CSharpMarshalContext); + ctx.Return.Write(ctx.Parameter.Name); return; default: throw new System.NotImplementedException(); @@ -106,10 +103,8 @@ public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - ctx.Return.Write(ctx.ReturnVarName); - return; case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - CSharpMarshalToManaged(ctx as CSharpMarshalContext); + ctx.Return.Write(ctx.ReturnVarName); return; default: throw new System.NotImplementedException(); @@ -118,21 +113,6 @@ public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) #region C# backend - public virtual Type CSharpSignatureType(TypePrinterContext ctx) - { - return new CILType(typeof(object)); - } - - public virtual void CSharpMarshalToNative(CSharpMarshalContext ctx) - { - ctx.Return.Write(ctx.Parameter.Name); - } - - public virtual void CSharpMarshalToManaged(CSharpMarshalContext ctx) - { - ctx.Return.Write(ctx.ReturnVarName); - } - /// /// Used to construct a new instance of the mapped type. /// diff --git a/tests/dotnet/CSharp/CSharp.Gen.cs b/tests/dotnet/CSharp/CSharp.Gen.cs index 38c2e5d670..362b8cdb42 100644 --- a/tests/dotnet/CSharp/CSharp.Gen.cs +++ b/tests/dotnet/CSharp/CSharp.Gen.cs @@ -120,17 +120,17 @@ public override bool VisitFunctionDecl(Function function) [TypeMap("boolean_t")] public class BooleanTypeMap : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new BuiltinType(PrimitiveType.Bool); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.Parameter.Name); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.ReturnVarName); } @@ -144,12 +144,12 @@ public override string CSharpConstruct() return string.Empty; } - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return GetEnumType(ctx.Type); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { if (ctx.Parameter.Type.Desugar().IsAddress()) ctx.Return.Write("new global::System.IntPtr(&{0})", ctx.Parameter.Name); @@ -157,7 +157,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) ctx.Return.Write(ctx.Parameter.Name); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { if (ctx.ReturnType.Type.Desugar().IsAddress()) { @@ -196,17 +196,17 @@ public override string CSharpConstruct() return string.Empty; } - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { return new TagType(flags ?? (flags = Context.ASTContext.FindEnum("Flags").First())); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.Parameter.Name); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.ReturnVarName); } @@ -229,7 +229,7 @@ public override bool IsIgnored } } - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { if (ctx.Kind == TypePrinterContextKind.Native) { @@ -249,7 +249,7 @@ public override Type CSharpSignatureType(TypePrinterContext ctx) ctx.GetTemplateParameterList()}>"); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { // pointless, put just so that the generated code compiles var type = (TemplateSpecializationType)ctx.Parameter.Type.Desugar(); @@ -259,7 +259,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) ctx.Return.Write("new {0}()", specialization.Visit(typePrinter)); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.ReturnVarName); } @@ -268,18 +268,18 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) [TypeMap("TypeMappedWithOperator")] public class TypeMappedWithOperator : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { // doesn't matter, we just need it to compile return new BuiltinType(PrimitiveType.Int); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.Parameter.Name); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.ReturnVarName); } @@ -288,7 +288,7 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) [TypeMap("QString")] public class QString : TypeMap { - public override Type CSharpSignatureType(TypePrinterContext ctx) + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) { if (ctx.Kind == TypePrinterContextKind.Native) { @@ -299,13 +299,13 @@ public override Type CSharpSignatureType(TypePrinterContext ctx) return new CILType(typeof(string)); } - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ? "global::System.IntPtr.Zero" : "\"test\""); } - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) { ctx.Return.Write("\"test\""); } diff --git a/tests/dotnet/Common/Common.Gen.cs b/tests/dotnet/Common/Common.Gen.cs index 7ab6cbea8d..338f01b373 100644 --- a/tests/dotnet/Common/Common.Gen.cs +++ b/tests/dotnet/Common/Common.Gen.cs @@ -7,41 +7,47 @@ namespace CppSharp.Tests { - [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)] - public class CLITypeMappedIndex : TypeMap + namespace CLI { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)] + public class TypeMappedIndex : TypeMap { - return new BuiltinType(PrimitiveType.UShort); - } - - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) - { - ctx.Return.Write(ctx.ReturnVarName); - } - - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) - { - ctx.Return.Write("::TypeMappedIndex()"); + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + { + return new BuiltinType(PrimitiveType.UShort); + } + + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + { + ctx.Return.Write(ctx.ReturnVarName); + } + + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + { + ctx.Return.Write("::TypeMappedIndex()"); + } } } - [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class TypeMappedIndex : TypeMap + namespace CSharp { - public override Type CSharpSignatureType(TypePrinterContext ctx) - { - return new BuiltinType(PrimitiveType.UShort); - } - - public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) - { - ctx.Return.Write(ctx.ReturnVarName); - } - - public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)] + public class TypeMappedIndex : TypeMap { - ctx.Return.Write("IntPtr.Zero"); + public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + { + return new BuiltinType(PrimitiveType.UShort); + } + + public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + { + ctx.Return.Write(ctx.ReturnVarName); + } + + public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + { + ctx.Return.Write("IntPtr.Zero"); + } } } From 64c3b240a36e07eab9a810cfc42be3308778d3ba Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Tue, 5 Dec 2023 08:07:14 +0200 Subject: [PATCH 09/12] TypeMap: cleanup patches from previous commits --- src/Generator/Types/Std/Stdlib.CLI.cs | 34 +++++++------- src/Generator/Types/Std/Stdlib.CSharp.cs | 32 ++++++------- src/Generator/Types/TypeMap.cs | 60 +++--------------------- tests/dotnet/CLI/CLI.Gen.cs | 12 ++--- tests/dotnet/CSharp/CSharp.Gen.cs | 36 +++++++------- tests/dotnet/Common/Common.Gen.cs | 12 ++--- 6 files changed, 69 insertions(+), 117 deletions(-) diff --git a/src/Generator/Types/Std/Stdlib.CLI.cs b/src/Generator/Types/Std/Stdlib.CLI.cs index 454a4523d3..2f12e5b416 100644 --- a/src/Generator/Types/Std/Stdlib.CLI.cs +++ b/src/Generator/Types/Std/Stdlib.CLI.cs @@ -11,12 +11,12 @@ namespace CppSharp.Types.Std.CLI [TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)] public class ConstCharPointer : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Before.WriteLine( "auto _{0} = clix::marshalString({1});", @@ -25,7 +25,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) ctx.Return.Write("_{0}.c_str()", ctx.ArgName); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { if (ctx.Parameter != null && !ctx.Parameter.IsOut && !ctx.Parameter.IsInOut) @@ -83,18 +83,18 @@ public class ConstChar32TPointer : ConstCharPointer [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CLI_ID)] public class String : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.ReturnVarName); @@ -104,18 +104,18 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) [TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)] public class WString : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.ReturnVarName); @@ -143,13 +143,13 @@ public override bool IsIgnored } } - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CustomType( $"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^"); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { var desugared = Type.Desugar(); var templateType = desugared as TemplateSpecializationType; @@ -207,7 +207,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) ctx.Return.Write(tmpVarName); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { var desugared = Type.Desugar(); var templateType = desugared as TemplateSpecializationType; @@ -261,19 +261,19 @@ public class Map : TypeMap { public override bool IsIgnored { get { return true; } } - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { var type = Type as TemplateSpecializationType; return new CustomType( $@"::System::Collections::Generic::Dictionary<{type.Arguments[0].Type}, {type.Arguments[1].Type}>^"); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { throw new System.NotImplementedException(); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { throw new System.NotImplementedException(); } @@ -294,12 +294,12 @@ public class SharedPtr : TypeMap [TypeMap("basic_ostream>", GeneratorKind.CLI_ID)] public class OStream : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(System.IO.TextWriter)); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative; if (!ctx.Parameter.Type.Desugar().IsPointer()) diff --git a/src/Generator/Types/Std/Stdlib.CSharp.cs b/src/Generator/Types/Std/Stdlib.CSharp.cs index 459905134c..e69de8e7e2 100644 --- a/src/Generator/Types/Std/Stdlib.CSharp.cs +++ b/src/Generator/Types/Std/Stdlib.CSharp.cs @@ -14,41 +14,41 @@ namespace CppSharp.Types.Std.CSharp [TypeMap("int", GeneratorKindID = GeneratorKind.CSharp_ID)] public class Int : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => + public override Type SignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetSignedType(Context.TargetInfo.IntWidth); } [TypeMap("unsigned int", GeneratorKindID = GeneratorKind.CSharp_ID)] public class UnsignedInt : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => + public override Type SignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.IntWidth); } [TypeMap("long", GeneratorKindID = GeneratorKind.CSharp_ID)] public class Long : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => + public override Type SignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetSignedType(Context.TargetInfo.LongWidth); } [TypeMap("unsigned long", GeneratorKindID = GeneratorKind.CSharp_ID)] public class UnsignedLong : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) => + public override Type SignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.LongWidth); } [TypeMap("char", GeneratorKindID = GeneratorKind.CSharp_ID)] public class Char : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(ctx.Kind == TypePrinterContextKind.Native || !Context.Options.MarshalCharAsManagedChar ? typeof(sbyte) : typeof(char)); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { if (Context.Options.MarshalCharAsManagedChar) ctx.Return.Write("global::System.Convert.ToSByte({0})", @@ -57,7 +57,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { if (Context.Options.MarshalCharAsManagedChar) ctx.Return.Write("global::System.Convert.ToChar({0})", @@ -70,7 +70,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) [TypeMap("char16_t", GeneratorKindID = GeneratorKind.CSharp_ID)] public class Char16T : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(char)); } @@ -79,7 +79,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) [TypeMap("wchar_t", GeneratorKindID = GeneratorKind.CSharp_ID)] public class WCharT : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(char)); } @@ -88,7 +88,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) [TypeMap("const char*", GeneratorKindID = GeneratorKind.CSharp_ID)] public class ConstCharPointer : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Managed) return new CILType(typeof(string)); @@ -117,7 +117,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) $"{Context.Options.Encoding.EncodingName} is not supported yet."); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { string param = ctx.Parameter.Name; if (ctx.Parameter.Usage == ParameterUsage.Unknown && @@ -194,7 +194,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) ctx.Return.Write($"{bytePtr}"); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { if (ctx.Parameter != null && !ctx.Parameter.IsOut && !ctx.Parameter.IsInOut) @@ -304,7 +304,7 @@ public class ConstChar32TPointer : ConstCharPointer [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CSharp_ID)] public class String : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Managed) return new CILType(typeof(string)); @@ -316,7 +316,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) return new CustomType(basicString.Visit(typePrinter).Type); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { Type type = ctx.Parameter.Type.Desugar(); ClassTemplateSpecialization basicString = GetBasicString(type); @@ -361,7 +361,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) } } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { var type = Type.Desugar(resolveTemplateSubstitution: false); ClassTemplateSpecialization basicString = GetBasicString(type); @@ -415,7 +415,7 @@ private static ClassTemplateSpecialization GetBasicString(Type type) [TypeMap("FILE", GeneratorKindID = GeneratorKind.CSharp_ID)] public class FILE : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(System.IntPtr)); } diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 4506ad7f4c..5f701c6c4a 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -2,10 +2,7 @@ using CppSharp.AST; using CppSharp.Generators; using CppSharp.Generators.AST; -using CppSharp.Generators.C; using CppSharp.Generators.CLI; -using CppSharp.Generators.Cpp; -using CppSharp.Generators.CSharp; using Attribute = System.Attribute; using Type = CppSharp.AST.Type; @@ -51,64 +48,19 @@ public class TypeMap /// public virtual bool DoesMarshalling => true; - public Type SignatureType(TypePrinterContext ctx) + public virtual Type SignatureType(TypePrinterContext ctx) { - return SignatureType(ctx, Context.Options.GeneratorKind); + return new CILType(typeof(object)); } - public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public virtual void MarshalToNative(MarshalContext ctx) { - switch (kind) - { - case var _ when ReferenceEquals(kind, GeneratorKind.C): - case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - return new CILType(typeof(object)); - default: - throw new System.NotImplementedException(); - } + ctx.Return.Write(ctx.Parameter.Name); } - public void MarshalToNative(MarshalContext ctx) + public virtual void MarshalToManaged(MarshalContext ctx) { - MarshalToNative(ctx, Context.Options.GeneratorKind); - } - - public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind) - { - kind ??= Context.Options.GeneratorKind; - switch (kind) - { - case var _ when ReferenceEquals(kind, GeneratorKind.C): - case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - ctx.Return.Write(ctx.Parameter.Name); - return; - default: - throw new System.NotImplementedException(); - } - } - - public void MarshalToManaged(MarshalContext ctx) - { - MarshalToManaged(ctx, Context.Options.GeneratorKind); - } - - public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) - { - switch (kind) - { - case var _ when ReferenceEquals(kind, GeneratorKind.C): - case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - ctx.Return.Write(ctx.ReturnVarName); - return; - default: - throw new System.NotImplementedException(); - } + ctx.Return.Write(ctx.ReturnVarName); } #region C# backend diff --git a/tests/dotnet/CLI/CLI.Gen.cs b/tests/dotnet/CLI/CLI.Gen.cs index 5a5d25c7a1..586fd43397 100644 --- a/tests/dotnet/CLI/CLI.Gen.cs +++ b/tests/dotnet/CLI/CLI.Gen.cs @@ -9,12 +9,12 @@ namespace CppSharp.Tests [TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)] public class IgnoredClassTemplateForEmployeeMap : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CustomType("CLI::Employee^"); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)"); } @@ -25,17 +25,17 @@ namespace CLI [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)] public class TestMappedTypeNonConstRefParamTypeMap : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { if (ctx.Parameter.Usage == ParameterUsage.InOut) { @@ -57,7 +57,7 @@ namespace Cpp [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CPlusPlus_ID)] public class TestMappedTypeNonConstRefParamTypeMap : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { var tagType = ctx.Type as TagType; var typePrinter = new CppTypePrinter(Context); diff --git a/tests/dotnet/CSharp/CSharp.Gen.cs b/tests/dotnet/CSharp/CSharp.Gen.cs index 362b8cdb42..5c9f45cef3 100644 --- a/tests/dotnet/CSharp/CSharp.Gen.cs +++ b/tests/dotnet/CSharp/CSharp.Gen.cs @@ -120,17 +120,17 @@ public override bool VisitFunctionDecl(Function function) [TypeMap("boolean_t")] public class BooleanTypeMap : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new BuiltinType(PrimitiveType.Bool); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -144,12 +144,12 @@ public override string CSharpConstruct() return string.Empty; } - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return GetEnumType(ctx.Type); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { if (ctx.Parameter.Type.Desugar().IsAddress()) ctx.Return.Write("new global::System.IntPtr(&{0})", ctx.Parameter.Name); @@ -157,7 +157,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { if (ctx.ReturnType.Type.Desugar().IsAddress()) { @@ -196,17 +196,17 @@ public override string CSharpConstruct() return string.Empty; } - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new TagType(flags ?? (flags = Context.ASTContext.FindEnum("Flags").First())); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -229,7 +229,7 @@ public override bool IsIgnored } } - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Native) { @@ -249,7 +249,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) ctx.GetTemplateParameterList()}>"); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { // pointless, put just so that the generated code compiles var type = (TemplateSpecializationType)ctx.Parameter.Type.Desugar(); @@ -259,7 +259,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) ctx.Return.Write("new {0}()", specialization.Visit(typePrinter)); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -268,18 +268,18 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) [TypeMap("TypeMappedWithOperator")] public class TypeMappedWithOperator : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { // doesn't matter, we just need it to compile return new BuiltinType(PrimitiveType.Int); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -288,7 +288,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) [TypeMap("QString")] public class QString : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Native) { @@ -299,13 +299,13 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ? "global::System.IntPtr.Zero" : "\"test\""); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write("\"test\""); } diff --git a/tests/dotnet/Common/Common.Gen.cs b/tests/dotnet/Common/Common.Gen.cs index 338f01b373..08bf971f1c 100644 --- a/tests/dotnet/Common/Common.Gen.cs +++ b/tests/dotnet/Common/Common.Gen.cs @@ -12,17 +12,17 @@ namespace CLI [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)] public class TypeMappedIndex : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new BuiltinType(PrimitiveType.UShort); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write("::TypeMappedIndex()"); } @@ -34,17 +34,17 @@ namespace CSharp [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)] public class TypeMappedIndex : TypeMap { - public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) + public override Type SignatureType(TypePrinterContext ctx) { return new BuiltinType(PrimitiveType.UShort); } - public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToManaged(MarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } - public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind) + public override void MarshalToNative(MarshalContext ctx) { ctx.Return.Write("IntPtr.Zero"); } From 502d77e925d3cd9df0339004b5516affdab1e4f0 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Tue, 5 Dec 2023 08:49:49 +0200 Subject: [PATCH 10/12] TypeMapDatabase: fix passing GeneratorKind to FindTypeMap calls --- src/Generator/Types/TypeMapDatabase.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index 466a25f02a..aee8b6bb2d 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -64,13 +64,12 @@ public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap) { var specialization = template.GetClassTemplateSpecialization(); if (specialization != null && - FindTypeMap(specialization, out typeMap)) + FindTypeMap(specialization, kind, out typeMap)) return true; if (template.Template.TemplatedDecl != null) { - if (FindTypeMap(template.Template.TemplatedDecl, - out typeMap)) + if (FindTypeMap(template.Template.TemplatedDecl, kind, out typeMap)) { typeMap.Type = type; return true; From fdf796d7afbee96bc1b7b1bc1f8ff671ee4b593a Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Tue, 5 Dec 2023 09:34:04 +0200 Subject: [PATCH 11/12] Stdlib.CSharp.cs: move std::map typemap from Stdlib.CLI.cs --- src/Generator/Types/Std/Stdlib.CSharp.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Generator/Types/Std/Stdlib.CSharp.cs b/src/Generator/Types/Std/Stdlib.CSharp.cs index e69de8e7e2..a4198bcd1c 100644 --- a/src/Generator/Types/Std/Stdlib.CSharp.cs +++ b/src/Generator/Types/Std/Stdlib.CSharp.cs @@ -412,6 +412,22 @@ private static ClassTemplateSpecialization GetBasicString(Type type) } } + [TypeMap("std::map", GeneratorKindID = GeneratorKind.CSharp_ID)] + public class Map : TypeMap + { + public override bool IsIgnored { get { return true; } } + + public override Type SignatureType(TypePrinterContext ctx) + { + if (ctx.Kind == TypePrinterContextKind.Native) + return new CustomType("Std.Map"); + + var type = Type as TemplateSpecializationType; + return new CustomType( + $@"System.Collections.Generic.Dictionary<{type.Arguments[0].Type}, {type.Arguments[1].Type}>"); + } + } + [TypeMap("FILE", GeneratorKindID = GeneratorKind.CSharp_ID)] public class FILE : TypeMap { From d44b31cbc0bdbf973ee0ca850017ff37e0fc8850 Mon Sep 17 00:00:00 2001 From: Deadlocklogic Date: Thu, 7 Dec 2023 04:22:12 +0200 Subject: [PATCH 12/12] TypeMapDatabase: improve parameter name --- src/Generator/Types/TypeMapDatabase.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index aee8b6bb2d..904d58a929 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -35,12 +35,12 @@ public TypeMapDatabase(BindingContext bindingContext) } } - public static Dictionary TypeMapsByKind(Dictionary> globalTypeMaps, GeneratorKind kind) + public static Dictionary TypeMapsByKind(Dictionary> typeMapsDictionary, GeneratorKind kind) { - if (!globalTypeMaps.TryGetValue(kind, out Dictionary typeMap)) + if (!typeMapsDictionary.TryGetValue(kind, out Dictionary typeMap)) { typeMap = new Dictionary(); - globalTypeMaps.Add(kind, typeMap); + typeMapsDictionary.Add(kind, typeMap); } return typeMap; }