diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index e5a310704..9074614ac 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.SignatureType(typePrinterContext); + var mappedTo = typeMap.CSharpSignatureType(typePrinterContext); mappedTo = mappedTo.Desugar(); mappedTo = (mappedTo.GetFinalPointee() ?? mappedTo).Desugar(); return (mappedTo.IsPrimitiveType() || diff --git a/src/Generator/GeneratorKind.cs b/src/Generator/GeneratorKind.cs index 4101a77cb..0d8d2a865 100644 --- a/src/Generator/GeneratorKind.cs +++ b/src/Generator/GeneratorKind.cs @@ -38,11 +38,6 @@ 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); diff --git a/src/Generator/Generators/C/CppMarshal.cs b/src/Generator/Generators/C/CppMarshal.cs index d60a5d656..e0425e9f1 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.MarshalToManaged(Context); + typeMap.CppMarshalToManaged(Context); return false; } @@ -173,7 +173,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.MarshalToManaged(Context); + typeMap.CppMarshalToManaged(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.MarshalToManaged(Context); + typeMap.CppMarshalToManaged(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.MarshalToNative(Context); + typeMap.CppMarshalToNative(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.MarshalToNative(Context); + typeMap.CppMarshalToNative(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.MarshalToNative(Context); + typeMap.CppMarshalToNative(Context); return true; } @@ -563,7 +563,7 @@ private void MarshalRefClass(Class @class) if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToNative(Context); + typeMap.CppMarshalToNative(Context); return; } diff --git a/src/Generator/Generators/C/CppTypePrinter.cs b/src/Generator/Generators/C/CppTypePrinter.cs index a8ea5caf5..edfcdcd91 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.SignatureType(typePrinterContext).Visit(typePrinter); + var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter); result = new TypePrinterResult(typeName) { TypeMap = typeMap }; return true; diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 7e52b9bd2..c64165175 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.MarshalToManaged(Context); + typeMap.CLIMarshalToManaged(Context); return false; } @@ -215,7 +215,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.MarshalToManaged(Context); + typeMap.CLIMarshalToManaged(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.MarshalToManaged(Context); + typeMap.CLIMarshalToManaged(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.MarshalToNative(Context); + typeMap.CLIMarshalToNative(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.MarshalToNative(Context); + typeMap.CLIMarshalToNative(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.MarshalToNative(Context); + typeMap.CLIMarshalToNative(Context); return true; } @@ -688,7 +688,7 @@ private void MarshalRefClass(Class @class) if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToNative(Context); + typeMap.CLIMarshalToNative(Context); return; } diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index 970130f5d..783a44342 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.SignatureType(typePrinterContext).ToString(); + return typeMap.CLISignatureType(typePrinterContext).ToString(); } Declaration decl = tag.Declaration; @@ -112,7 +112,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer, Type = pointer }; - return typeMap.SignatureType(typePrinterContext).Visit(this); + return typeMap.CLISignatureType(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.SignatureType(typePrinterContext).ToString(); + return typeMap.CLISignatureType(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.SignatureType(typePrinterContext).ToString(); + return typeMap.CLISignatureType(typePrinterContext).ToString(); } return decl.Name; diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 3dfbd1e83..09ced7d11 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.MarshalToManaged(Context); + typeMap.CSharpMarshalToManaged(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.MarshalToNative(Context); + typeMap.CSharpMarshalToNative(Context); return false; } diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index d8152864f..8623ad3ce 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.SignatureType(typePrinterContext), + WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext), Helpers.ReturnIdentifier); } else diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 9b1b85535..1453adb1f 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, GeneratorKind.CSharp, out typeMap)) + if (TypeMapDatabase.FindTypeMap(tag, out typeMap)) { typeMap.Type = tag; @@ -47,7 +47,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals Type = tag }; - return typeMap.SignatureType(typePrinterContext).ToString(); + return typeMap.CSharpSignatureType(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, GeneratorKind.CSharp, out typeMap)) + if (TypeMapDatabase.FindTypeMap(builtin, out typeMap)) { var typePrinterContext = new TypePrinterContext() { @@ -159,7 +159,7 @@ public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQual Type = builtin, Parameter = Parameter }; - return typeMap.SignatureType(typePrinterContext).Visit(this); + return typeMap.CSharpSignatureType(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, GeneratorKind.CSharp, out typeMap); + TypeMapDatabase.FindTypeMap(pointer, 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.SignatureType(typePrinterContext).Visit(this); + return typeMap.CSharpSignatureType(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, GeneratorKind.CSharp, out typeMap)) + if (TypeMapDatabase.FindTypeMap(typedef, out typeMap)) { typeMap.Type = typedef; @@ -270,7 +270,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef, Parameter = Parameter }; - return typeMap.SignatureType(typePrinterContext).ToString(); + return typeMap.CSharpSignatureType(typePrinterContext).ToString(); } FunctionType func; @@ -299,7 +299,7 @@ public override TypePrinterResult VisitTemplateSpecializationType( template.Template.TemplatedDecl; TypeMap typeMap; - if (!TypeMapDatabase.FindTypeMap(template, GeneratorKind.CSharp, out typeMap)) + if (!TypeMapDatabase.FindTypeMap(template, out typeMap)) { if (ContextKind == TypePrinterContextKind.Managed && decl == template.Template.TemplatedDecl && @@ -330,7 +330,7 @@ public override TypePrinterResult VisitTemplateSpecializationType( MarshalKind = MarshalKind }; - return typeMap.SignatureType(typePrinterContext).ToString(); + return typeMap.CSharpSignatureType(typePrinterContext).ToString(); } public override TypePrinterResult VisitDependentTemplateSpecializationType( diff --git a/src/Generator/Generators/ExtensionMethods.cs b/src/Generator/Generators/ExtensionMethods.cs index c24a47038..ee4b1054d 100644 --- a/src/Generator/Generators/ExtensionMethods.cs +++ b/src/Generator/Generators/ExtensionMethods.cs @@ -63,9 +63,12 @@ public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps, Type = typeMap.Type }; - if (generatorKind == GeneratorKind.CLI || generatorKind == GeneratorKind.CSharp) + switch (generatorKind) { - return typeMap.SignatureType(typePrinterContext).Desugar(); + case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI): + return typeMap.CLISignatureType(typePrinterContext).Desugar(); + case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp): + return typeMap.CSharpSignatureType(typePrinterContext).Desugar(); } } diff --git a/src/Generator/Generators/NAPI/NAPIMarshal.cs b/src/Generator/Generators/NAPI/NAPIMarshal.cs index 3eb23b528..c0cf70082 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.MarshalToManaged(Context); + typeMap.CppMarshalToManaged(Context); return false; } @@ -194,7 +194,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.MarshalToManaged(Context); + typeMap.CppMarshalToManaged(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.MarshalToManaged(Context); + typeMap.CppMarshalToManaged(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.MarshalToNative(Context); + typeMap.CppMarshalToNative(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.MarshalToNative(Context); + typeMap.CppMarshalToNative(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.MarshalToNative(Context); + typeMap.CppMarshalToNative(Context); return true; } diff --git a/src/Generator/Generators/QuickJS/QuickJSMarshal.cs b/src/Generator/Generators/QuickJS/QuickJSMarshal.cs index 4937192bf..5de2ad048 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(Context); + typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context); return false; } @@ -210,7 +210,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) typeMap.DoesMarshalling) { typeMap.Type = typedef; - typeMap.MarshalToManaged(Context); + typeMap.MarshalToManaged(GeneratorKind.QuickJS, 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(Context); + typeMap.MarshalToManaged(GeneratorKind.QuickJS, 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(Context); + typeMap.MarshalToNative(GeneratorKind.QuickJS, 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(Context); + typeMap.MarshalToNative(GeneratorKind.QuickJS, 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(Context); + typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); return true; } @@ -672,7 +672,7 @@ private void MarshalRefClass(Class @class) if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.MarshalToNative(Context); + typeMap.MarshalToNative(GeneratorKind.QuickJS, Context); return; } diff --git a/src/Generator/Passes/ExpressionHelper.cs b/src/Generator/Passes/ExpressionHelper.cs index be5e924d7..435dbcf65 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, GeneratorKind.CSharp, out TypeMap typeMap)) + if (context.TypeMaps.FindTypeMap(type, out TypeMap typeMap)) { - var cilType = typeMap.SignatureType(new TypePrinterContext { Type = type, Kind = TypePrinterContextKind.Managed }) as CILType; + var cilType = typeMap.CSharpSignatureType(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, GeneratorKind.CSharp, out typeMap)) + if (context.TypeMaps.FindTypeMap(type, out typeMap)) { var typePrinterContext = new TypePrinterContext() { @@ -248,7 +248,7 @@ private static bool CheckForDefaultPointer(BindingContext context, Type desugare Type = type }; - var typeInSignature = typeMap.SignatureType(typePrinterContext) + var typeInSignature = typeMap.CSharpSignatureType(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, GeneratorKind.CSharp, out TypeMap typeMap)) + if (context.TypeMaps.FindTypeMap(desugared, 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.SignatureType(typePrinterContext) + var typeInSignature = typeMap.CSharpSignatureType(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 690bb092a..49fe8d017 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, GeneratorKind.CSharp, out typeMap)) + if (Context.TypeMaps.FindTypeMap(type, out typeMap)) { - var mappedTo = typeMap.SignatureType( + var mappedTo = typeMap.CSharpSignatureType( new TypePrinterContext { Parameter = parameter, diff --git a/src/Generator/Types/Std/Stdlib.CLI.cs b/src/Generator/Types/Std/Stdlib.CLI.cs index 2f12e5b41..8db2058f9 100644 --- a/src/Generator/Types/Std/Stdlib.CLI.cs +++ b/src/Generator/Types/Std/Stdlib.CLI.cs @@ -5,18 +5,19 @@ using CppSharp.Generators.AST; using CppSharp.Generators.C; using CppSharp.Generators.CLI; +using CppSharp.Generators.CSharp; -namespace CppSharp.Types.Std.CLI +namespace CppSharp.Types.Std { [TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)] - public class ConstCharPointer : TypeMap + public partial class ConstCharPointer : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx) + public override void CLIMarshalToNative(MarshalContext ctx) { ctx.Before.WriteLine( "auto _{0} = clix::marshalString({1});", @@ -25,7 +26,7 @@ public override void MarshalToNative(MarshalContext ctx) ctx.Return.Write("_{0}.c_str()", ctx.ArgName); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CLIMarshalToManaged(MarshalContext ctx) { if (ctx.Parameter != null && !ctx.Parameter.IsOut && !ctx.Parameter.IsInOut) @@ -56,45 +57,46 @@ public override void MarshalToManaged(MarshalContext ctx) $"{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 class ConstCharArray : ConstCharPointer + public partial class ConstCharArray : ConstCharPointer { } [TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CLI_ID)] - public class ConstWCharTPointer : ConstCharPointer + public partial class ConstWCharTPointer : ConstCharPointer { } [TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CLI_ID)] - public class ConstChar16TPointer : ConstCharPointer + public partial class ConstChar16TPointer : ConstCharPointer { } [TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CLI_ID)] - public class ConstChar32TPointer : ConstCharPointer + public partial class ConstChar32TPointer : ConstCharPointer { } [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CLI_ID)] - public class String : TypeMap + public partial class String : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx) + public override void CLIMarshalToNative(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CLIMarshalToManaged(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.ReturnVarName); @@ -102,20 +104,20 @@ public override void MarshalToManaged(MarshalContext ctx) } [TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)] - public class WString : TypeMap + public partial class WString : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx) + public override void CLIMarshalToNative(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CLIMarshalToManaged(MarshalContext ctx) { ctx.Return.Write("clix::marshalString({0})", ctx.ReturnVarName); @@ -123,7 +125,7 @@ public override void MarshalToManaged(MarshalContext ctx) } [TypeMap("std::vector", GeneratorKindID = GeneratorKind.CLI_ID)] - public class Vector : TypeMap + public partial class Vector : TypeMap { public override bool IsIgnored { @@ -143,13 +145,13 @@ public override bool IsIgnored } } - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { return new CustomType( $"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^"); } - public override void MarshalToNative(MarshalContext ctx) + public override void CLIMarshalToNative(MarshalContext ctx) { var desugared = Type.Desugar(); var templateType = desugared as TemplateSpecializationType; @@ -207,7 +209,7 @@ public override void MarshalToNative(MarshalContext ctx) ctx.Return.Write(tmpVarName); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CLIMarshalToManaged(MarshalContext ctx) { var desugared = Type.Desugar(); var templateType = desugared as TemplateSpecializationType; @@ -257,49 +259,61 @@ public override void MarshalToManaged(MarshalContext ctx) } [TypeMap("std::map", GeneratorKindID = GeneratorKind.CLI_ID)] - public class Map : TypeMap + public partial class Map : TypeMap { public override bool IsIgnored { get { return true; } } - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { 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) + public override void CLIMarshalToNative(MarshalContext ctx) { throw new System.NotImplementedException(); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CLIMarshalToManaged(MarshalContext ctx) { 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)] - public class List : TypeMap + public partial class List : TypeMap { public override bool IsIgnored { get { return true; } } } [TypeMap("std::shared_ptr", GeneratorKindID = GeneratorKind.CLI_ID)] - public class SharedPtr : TypeMap + public partial class SharedPtr : TypeMap { public override bool IsIgnored { get { return true; } } } [TypeMap("basic_ostream>", GeneratorKind.CLI_ID)] - public class OStream : TypeMap + public partial class OStream : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { return new CILType(typeof(System.IO.TextWriter)); } - public override void MarshalToNative(MarshalContext ctx) + public override void CLIMarshalToNative(MarshalContext ctx) { var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative; if (!ctx.Parameter.Type.Desugar().IsPointer()) @@ -312,7 +326,7 @@ public override void MarshalToNative(MarshalContext ctx) } [TypeMap("std::nullptr_t", GeneratorKindID = GeneratorKind.CLI_ID)] - public class NullPtr : TypeMap + public partial 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 a4198bcd1..a3c3d6d5a 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.CSharp +namespace CppSharp.Types.Std { [TypeMap("int", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class Int : TypeMap + public partial class Int : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) => + public override Type CSharpSignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetSignedType(Context.TargetInfo.IntWidth); } [TypeMap("unsigned int", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class UnsignedInt : TypeMap + public partial class UnsignedInt : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) => + public override Type CSharpSignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.IntWidth); } [TypeMap("long", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class Long : TypeMap + public partial class Long : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) => + public override Type CSharpSignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetSignedType(Context.TargetInfo.LongWidth); } [TypeMap("unsigned long", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class UnsignedLong : TypeMap + public partial class UnsignedLong : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) => + public override Type CSharpSignatureType(TypePrinterContext ctx) => CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.LongWidth); } [TypeMap("char", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class Char : TypeMap + public partial class Char : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return new CILType(ctx.Kind == TypePrinterContextKind.Native || !Context.Options.MarshalCharAsManagedChar ? typeof(sbyte) : typeof(char)); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { if (Context.Options.MarshalCharAsManagedChar) ctx.Return.Write("global::System.Convert.ToSByte({0})", @@ -57,7 +57,7 @@ public override void MarshalToNative(MarshalContext ctx) ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { if (Context.Options.MarshalCharAsManagedChar) ctx.Return.Write("global::System.Convert.ToChar({0})", @@ -68,27 +68,27 @@ public override void MarshalToManaged(MarshalContext ctx) } [TypeMap("char16_t", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class Char16T : TypeMap + public partial class Char16T : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return new CILType(typeof(char)); } } [TypeMap("wchar_t", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class WCharT : TypeMap + public partial class WCharT : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return new CILType(typeof(char)); } } [TypeMap("const char*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class ConstCharPointer : TypeMap + public partial class ConstCharPointer : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Managed) return new CILType(typeof(string)); @@ -117,7 +117,7 @@ public override Type SignatureType(TypePrinterContext ctx) $"{Context.Options.Encoding.EncodingName} is not supported yet."); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { string param = ctx.Parameter.Name; if (ctx.Parameter.Usage == ParameterUsage.Unknown && @@ -194,7 +194,7 @@ public override void MarshalToNative(MarshalContext ctx) ctx.Return.Write($"{bytePtr}"); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { 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 class ConstCharArray : ConstCharPointer + public partial class ConstCharArray : ConstCharPointer { } [TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class ConstWCharTPointer : ConstCharPointer + public partial class ConstWCharTPointer : ConstCharPointer { } [TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class ConstChar16TPointer : ConstCharPointer + public partial class ConstChar16TPointer : ConstCharPointer { } [TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class ConstChar32TPointer : ConstCharPointer + public partial class ConstChar32TPointer : ConstCharPointer { } [TypeMap("basic_string, allocator>", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class String : TypeMap + public partial class String : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Managed) return new CILType(typeof(string)); @@ -316,7 +316,7 @@ public override Type SignatureType(TypePrinterContext ctx) return new CustomType(basicString.Visit(typePrinter).Type); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { Type type = ctx.Parameter.Type.Desugar(); ClassTemplateSpecialization basicString = GetBasicString(type); @@ -334,13 +334,15 @@ public override void MarshalToNative(MarshalContext ctx) var = Generator.GeneratedIdentifier(ctx.ArgName); ctx.Before.WriteLine($"fixed (void* {var} = &{ctx.ReturnVarName})"); ctx.Before.WriteOpenBraceAndIndent(); - (ctx as CSharpMarshalContext).HasCodeBlock = true; + ctx.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})"); @@ -349,19 +351,22 @@ public override void MarshalToNative(MarshalContext 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 MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { var type = Type.Desugar(resolveTemplateSubstitution: false); ClassTemplateSpecialization basicString = GetBasicString(type); @@ -371,7 +376,10 @@ public override void MarshalToManaged(MarshalContext 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) { @@ -412,26 +420,10 @@ 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 + public partial class FILE : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return new CILType(typeof(System.IntPtr)); } diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 5f701c6c4..e7060192a 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -2,7 +2,10 @@ 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; @@ -48,23 +51,77 @@ public class TypeMap /// public virtual bool DoesMarshalling => true; - public virtual Type SignatureType(TypePrinterContext ctx) + public virtual Type SignatureType(GeneratorKind kind, TypePrinterContext ctx) + { + switch (kind) + { + case var _ when ReferenceEquals(kind, GeneratorKind.C): + case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): + return CppSignatureType(ctx); + case var _ when ReferenceEquals(kind, GeneratorKind.CLI): + return CLISignatureType(ctx); + case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): + return CSharpSignatureType(ctx); + default: + throw new System.NotImplementedException(); + } + } + + public virtual void MarshalToNative(GeneratorKind kind, MarshalContext ctx) + { + switch (kind) + { + case var _ when ReferenceEquals(kind, GeneratorKind.C): + case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): + CppMarshalToNative(ctx); + return; + case var _ when ReferenceEquals(kind, GeneratorKind.CLI): + CLIMarshalToNative(ctx); + return; + case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): + CSharpMarshalToNative(ctx as CSharpMarshalContext); + return; + default: + throw new System.NotImplementedException(); + } + } + + public virtual void MarshalToManaged(GeneratorKind kind, MarshalContext ctx) + { + switch (kind) + { + case var _ when ReferenceEquals(kind, GeneratorKind.C): + case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): + CppMarshalToManaged(ctx); + return; + case var _ when ReferenceEquals(kind, GeneratorKind.CLI): + CLIMarshalToManaged(ctx); + return; + case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): + CSharpMarshalToManaged(ctx as CSharpMarshalContext); + return; + default: + throw new System.NotImplementedException(); + } + } + + #region C# backend + + public virtual Type CSharpSignatureType(TypePrinterContext ctx) { return new CILType(typeof(object)); } - public virtual void MarshalToNative(MarshalContext ctx) + public virtual void CSharpMarshalToNative(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public virtual void MarshalToManaged(MarshalContext ctx) + public virtual void CSharpMarshalToManaged(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } - #region C# backend - /// /// Used to construct a new instance of the mapped type. /// @@ -78,18 +135,55 @@ 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 + + #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 { 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 904d58a92..96711ae69 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -10,16 +10,13 @@ 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; - GlobalTypeMaps = new Dictionary>(); + TypeMaps = new Dictionary(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { try @@ -35,22 +32,33 @@ public TypeMapDatabase(BindingContext bindingContext) } } - public static Dictionary TypeMapsByKind(Dictionary> typeMapsDictionary, GeneratorKind kind) + private void SetupTypeMaps(IEnumerable types, + BindingContext bindingContext) { - if (!typeMapsDictionary.TryGetValue(kind, out Dictionary typeMap)) + foreach (var type in types) { - typeMap = new Dictionary(); - typeMapsDictionary.Add(kind, 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); + } + } + } } - return 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) + public bool FindTypeMap(Type type, out TypeMap typeMap) { - var typeMaps = TypeMapsByKind(typeMapsCache, kind); // Looks up the type in the cache map. if (typeMaps.ContainsKey(type)) { @@ -64,12 +72,13 @@ public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap) { var specialization = template.GetClassTemplateSpecialization(); if (specialization != null && - FindTypeMap(specialization, kind, out typeMap)) + FindTypeMap(specialization, out typeMap)) return true; if (template.Template.TemplatedDecl != null) { - if (FindTypeMap(template.Template.TemplatedDecl, kind, out typeMap)) + if (FindTypeMap(template.Template.TemplatedDecl, + out typeMap)) { typeMap.Type = type; return true; @@ -104,7 +113,7 @@ public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap) typePrinter.PushScope(typePrintScopeKind); var typeName = type.Visit(typePrinter); typePrinter.PopScope(); - if (FindTypeMap(typeName, kind, out typeMap)) + if (FindTypeMap(typeName, out typeMap)) { typeMap.Type = type; typeMaps[type] = typeMap; @@ -118,33 +127,11 @@ public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap) } public bool FindTypeMap(Declaration declaration, out TypeMap 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); + FindTypeMap(new TagType(declaration), out typeMap); - public bool FindTypeMap(string name, GeneratorKind kind, out TypeMap typeMap) => - TypeMapsByKind(GlobalTypeMaps, kind).TryGetValue(name, out typeMap) && typeMap.IsEnabled; + public bool FindTypeMap(string name, out TypeMap typeMap) => + TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled; - 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); - } - } - } - } + private Dictionary typeMaps = new Dictionary(); } } diff --git a/tests/dotnet/CLI/CLI.Gen.cs b/tests/dotnet/CLI/CLI.Gen.cs index 586fd4339..3ec8c1dc8 100644 --- a/tests/dotnet/CLI/CLI.Gen.cs +++ b/tests/dotnet/CLI/CLI.Gen.cs @@ -1,68 +1,59 @@ using CppSharp.AST; using CppSharp.Generators; using CppSharp.Generators.C; +using CppSharp.Passes; using CppSharp.Types; using CppSharp.Utils; namespace CppSharp.Tests { - [TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)] + [TypeMap("IgnoredClassTemplateForEmployee")] public class IgnoredClassTemplateForEmployeeMap : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CLISignatureType(TypePrinterContext ctx) { return new CustomType("CLI::Employee^"); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CLIMarshalToManaged(MarshalContext ctx) { ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)"); } } - namespace CLI + [TypeMap("TestMappedTypeNonConstRefParam")] + public class TestMappedTypeNonConstRefParamTypeMap : TypeMap { - [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)] - public class TestMappedTypeNonConstRefParamTypeMap : TypeMap + public override Type CLISignatureType(TypePrinterContext ctx) { - public override Type SignatureType(TypePrinterContext ctx) - { - return new CILType(typeof(string)); - } - - public override void MarshalToManaged(MarshalContext ctx) - { - ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); - } - - public override void MarshalToNative(MarshalContext ctx) - { - if (ctx.Parameter.Usage == ParameterUsage.InOut) - { - ctx.Before.WriteLine($"System::String^ _{ctx.Parameter.Name} = {ctx.Parameter.Name};"); - } - - string paramName = ctx.Parameter.Usage == ParameterUsage.InOut ? $"_{ctx.Parameter.Name}" : ctx.Parameter.Name; + return new CILType(typeof(string)); + } - ctx.Before.WriteLine( - $"::TestMappedTypeNonConstRefParam _{ctx.ArgName} = clix::marshalString({paramName});"); + public override Type CppSignatureType(TypePrinterContext ctx) + { + var tagType = ctx.Type as TagType; + var typePrinter = new CppTypePrinter(Context); + return new CustomType(tagType.Declaration.Visit(typePrinter)); + } - ctx.Return.Write("_{0}", ctx.ArgName); - } + public override void CLIMarshalToManaged(MarshalContext ctx) + { + ctx.Return.Write("clix::marshalString({0}.m_str)", ctx.ReturnVarName); } - } - namespace Cpp - { - [TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CPlusPlus_ID)] - public class TestMappedTypeNonConstRefParamTypeMap : TypeMap + public override void CLIMarshalToNative(MarshalContext ctx) { - public override Type SignatureType(TypePrinterContext ctx) + if (ctx.Parameter.Usage == ParameterUsage.InOut) { - var tagType = ctx.Type as TagType; - var typePrinter = new CppTypePrinter(Context); - return new CustomType(tagType.Declaration.Visit(typePrinter)); + ctx.Before.WriteLine($"System::String^ _{ctx.Parameter.Name} = {ctx.Parameter.Name};"); } + + string paramName = ctx.Parameter.Usage == ParameterUsage.InOut ? $"_{ctx.Parameter.Name}" : ctx.Parameter.Name; + + ctx.Before.WriteLine( + $"::TestMappedTypeNonConstRefParam _{ctx.ArgName} = clix::marshalString({paramName});"); + + ctx.Return.Write("_{0}", ctx.ArgName); } } diff --git a/tests/dotnet/CSharp/CSharp.Gen.cs b/tests/dotnet/CSharp/CSharp.Gen.cs index 5c9f45cef..38c2e5d67 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) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return new BuiltinType(PrimitiveType.Bool); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -144,12 +144,12 @@ public override string CSharpConstruct() return string.Empty; } - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return GetEnumType(ctx.Type); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext 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) ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { if (ctx.ReturnType.Type.Desugar().IsAddress()) { @@ -196,17 +196,17 @@ public override string CSharpConstruct() return string.Empty; } - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { return new TagType(flags ?? (flags = Context.ASTContext.FindEnum("Flags").First())); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -229,7 +229,7 @@ public override bool IsIgnored } } - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Native) { @@ -249,7 +249,7 @@ public override Type SignatureType(TypePrinterContext ctx) ctx.GetTemplateParameterList()}>"); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext 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) ctx.Return.Write("new {0}()", specialization.Visit(typePrinter)); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -268,18 +268,18 @@ public override void MarshalToManaged(MarshalContext ctx) [TypeMap("TypeMappedWithOperator")] public class TypeMappedWithOperator : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { // doesn't matter, we just need it to compile return new BuiltinType(PrimitiveType.Int); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Name); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.ReturnVarName); } @@ -288,7 +288,7 @@ public override void MarshalToManaged(MarshalContext ctx) [TypeMap("QString")] public class QString : TypeMap { - public override Type SignatureType(TypePrinterContext ctx) + public override Type CSharpSignatureType(TypePrinterContext ctx) { if (ctx.Kind == TypePrinterContextKind.Native) { @@ -299,13 +299,13 @@ public override Type SignatureType(TypePrinterContext ctx) return new CILType(typeof(string)); } - public override void MarshalToNative(MarshalContext ctx) + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) { ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ? "global::System.IntPtr.Zero" : "\"test\""); } - public override void MarshalToManaged(MarshalContext ctx) + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) { ctx.Return.Write("\"test\""); } diff --git a/tests/dotnet/Common/Common.Gen.cs b/tests/dotnet/Common/Common.Gen.cs index 08bf971f1..0f1c47d10 100644 --- a/tests/dotnet/Common/Common.Gen.cs +++ b/tests/dotnet/Common/Common.Gen.cs @@ -7,47 +7,37 @@ namespace CppSharp.Tests { - namespace CLI + [TypeMap("TypeMappedIndex")] + public class TypeMappedIndex : TypeMap { - [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)] - public class TypeMappedIndex : TypeMap + public override Type CLISignatureType(TypePrinterContext ctx) { - public override Type SignatureType(TypePrinterContext ctx) - { - return new BuiltinType(PrimitiveType.UShort); - } - - public override void MarshalToManaged(MarshalContext ctx) - { - ctx.Return.Write(ctx.ReturnVarName); - } - - public override void MarshalToNative(MarshalContext ctx) - { - ctx.Return.Write("::TypeMappedIndex()"); - } + return new BuiltinType(PrimitiveType.UShort); } - } - namespace CSharp - { - [TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)] - public class TypeMappedIndex : TypeMap + public override void CLIMarshalToManaged(MarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + + public override void CLIMarshalToNative(MarshalContext ctx) + { + ctx.Return.Write("::TypeMappedIndex()"); + } + + 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) { - public override Type SignatureType(TypePrinterContext ctx) - { - return new BuiltinType(PrimitiveType.UShort); - } - - public override void MarshalToManaged(MarshalContext ctx) - { - ctx.Return.Write(ctx.ReturnVarName); - } - - public override void MarshalToNative(MarshalContext ctx) - { - ctx.Return.Write("IntPtr.Zero"); - } + ctx.Return.Write("IntPtr.Zero"); } }