From 43bb2ee4bb0063bad979e3fe3594b70d59abecf7 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 2 Jan 2019 01:10:43 +0200 Subject: [PATCH] Added a type map for char. Signed-off-by: Dimitar Dobrev --- src/AST/CppTypePrinter.cs | 2 +- src/Generator/Driver.cs | 2 +- src/Generator/Generators/CLI/CLIMarshal.cs | 3 - .../Generators/CLI/CLITypePrinter.cs | 2 - .../Generators/CSharp/CSharpMarshal.cs | 20 ------ .../Passes/CheckDuplicatedNamesPass.cs | 9 +-- src/Generator/Types/Std/Stdlib.cs | 66 +++++++++++++++++++ src/Generator/Types/TypeMap.cs | 17 ++--- src/Generator/Types/TypeMapDatabase.cs | 21 +++--- 9 files changed, 88 insertions(+), 54 deletions(-) diff --git a/src/AST/CppTypePrinter.cs b/src/AST/CppTypePrinter.cs index 3ab15317..ddafcfd2 100644 --- a/src/AST/CppTypePrinter.cs +++ b/src/AST/CppTypePrinter.cs @@ -65,7 +65,7 @@ namespace CppSharp.AST { switch (modifier) { - case PointerType.TypeModifier.Value: return string.Empty; + case PointerType.TypeModifier.Value: return "[]"; case PointerType.TypeModifier.Pointer: return "*"; case PointerType.TypeModifier.LVReference: return "&"; case PointerType.TypeModifier.RVReference: return "&&"; diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index 84193630..0b1b269f 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -73,7 +73,7 @@ namespace CppSharp } public void SetupTypeMaps() => - Context.TypeMaps = new TypeMapDatabase(Context.ASTContext, Options.GeneratorKind); + Context.TypeMaps = new TypeMapDatabase(Context.ASTContext, Options); void OnSourceFileParsed(IEnumerable files, ParserResult result) { diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index e08b95bd..eb5373a5 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; using CppSharp.AST; using CppSharp.AST.Extensions; -using CppSharp.Generators.CSharp; using CppSharp.Types; using Delegate = CppSharp.AST.Delegate; using Type = CppSharp.AST.Type; @@ -22,7 +21,6 @@ namespace CppSharp.Generators.CLI TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.Type = type; typeMap.CLIMarshalToManaged(Context); return false; } @@ -430,7 +428,6 @@ namespace CppSharp.Generators.CLI TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.Type = type; typeMap.CLIMarshalToNative(Context); return false; } diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index 7dd5200f..32cf234b 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -25,7 +25,6 @@ namespace CppSharp.Generators.CLI TypeMap typeMap = null; if (TypeMapDatabase.FindTypeMap(tag, out typeMap)) { - typeMap.Type = tag; var typePrinterContext = new TypePrinterContext { Type = tag }; return typeMap.CLISignatureType(typePrinterContext).ToString(); } @@ -225,7 +224,6 @@ namespace CppSharp.Generators.CLI TypeMap typeMap = null; if (TypeMapDatabase.FindTypeMap(template, out typeMap) && !typeMap.IsIgnored) { - typeMap.Type = template; var typePrinterContext = new TypePrinterContext { Type = template }; return typeMap.CLISignatureType(typePrinterContext).ToString(); } diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 8c528f31..744a4f00 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -53,7 +53,6 @@ namespace CppSharp.Generators.CSharp TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.Type = type; typeMap.CSharpMarshalToManaged(Context); return false; } @@ -237,15 +236,6 @@ namespace CppSharp.Generators.CSharp { case PrimitiveType.Void: return true; - case PrimitiveType.Char: - // returned structs must be blittable and char isn't - if (Context.Context.Options.MarshalCharAsManagedChar) - { - Context.Return.Write("global::System.Convert.ToChar({0})", - Context.ReturnVarName); - return true; - } - goto default; case PrimitiveType.Bool: if (Context.MarshalKind == MarshalKind.NativeField) { @@ -469,7 +459,6 @@ namespace CppSharp.Generators.CSharp TypeMap typeMap; if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { - typeMap.Type = type; typeMap.CSharpMarshalToNative(Context); return false; } @@ -727,15 +716,6 @@ namespace CppSharp.Generators.CSharp { case PrimitiveType.Void: return true; - case PrimitiveType.Char: - // returned structs must be blittable and char isn't - if (Context.Context.Options.MarshalCharAsManagedChar) - { - Context.Return.Write("global::System.Convert.ToSByte({0})", - Context.Parameter.Name); - return true; - } - goto default; case PrimitiveType.Bool: if (Context.MarshalKind == MarshalKind.NativeField) { diff --git a/src/Generator/Passes/CheckDuplicatedNamesPass.cs b/src/Generator/Passes/CheckDuplicatedNamesPass.cs index 1294d218..26ea8a2c 100644 --- a/src/Generator/Passes/CheckDuplicatedNamesPass.cs +++ b/src/Generator/Passes/CheckDuplicatedNamesPass.cs @@ -106,13 +106,8 @@ namespace CppSharp.Passes // TODO: some target languages might make a difference between values and pointers Type leftPointee = left.GetPointee(); Type rightPointee = right.GetPointee(); - if ((leftPointee != null && leftPointee.Desugar(false).Equals(right)) || - (rightPointee != null && rightPointee.Desugar(false).Equals(left))) - return true; - - return TypePrinter != null && - left.IsPrimitiveType() && right.IsPrimitiveType() && - left.Visit(TypePrinter).Type == right.Visit(TypePrinter).Type; + return (leftPointee != null && leftPointee.Desugar(false).Equals(right)) || + (rightPointee != null && rightPointee.Desugar(false).Equals(left)); } public int GetHashCode(Parameter obj) diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index 5834725f..0bcd522e 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -15,6 +15,72 @@ namespace CppSharp.Types.Std public override bool IsIgnored => true; } + [TypeMap("char", GeneratorKind = GeneratorKind.CSharp)] + public class Char : TypeMap + { + public override Type CSharpSignatureType(TypePrinterContext ctx) + { + return new CILType(ctx.Kind == TypePrinterContextKind.Native || + !Options.MarshalCharAsManagedChar ? typeof(sbyte) : typeof(char)); + } + + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + { + if (Options.MarshalCharAsManagedChar) + ctx.Return.Write("global::System.Convert.ToSByte({0})", + ctx.Parameter.Name); + else + ctx.Return.Write(ctx.Parameter.Name); + } + + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + { + if (Options.MarshalCharAsManagedChar) + ctx.Return.Write("global::System.Convert.ToChar({0})", + ctx.ReturnVarName); + else + ctx.Return.Write(ctx.ReturnVarName); + } + } + + [TypeMap("char16_t", GeneratorKind = GeneratorKind.CSharp)] + public class Char16T : TypeMap + { + public override Type CSharpSignatureType(TypePrinterContext ctx) + { + return new CILType(typeof(char)); + } + + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + { + ctx.Return.Write(ctx.Parameter.Name); + } + + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + } + + [TypeMap("wchar_t", GeneratorKind = GeneratorKind.CSharp)] + public class WCharT : TypeMap + { + public override Type CSharpSignatureType(TypePrinterContext ctx) + { + return new CILType(typeof(char)); + } + + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + { + ctx.Return.Write(ctx.Parameter.Name); + } + + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + } + [TypeMap("basic_string, allocator>")] public class String : TypeMap { diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index adc0fd40..4f1891d5 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using CppSharp.AST; -using CppSharp.AST.Extensions; using CppSharp.Generators; using CppSharp.Generators.AST; using CppSharp.Generators.CLI; @@ -14,7 +12,7 @@ namespace CppSharp.Types [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class TypeMapAttribute : Attribute { - public string Type { get; private set; } + public string Type { get; } public GeneratorKind GeneratorKind { get; set; } public TypeMapAttribute(string type) : this(type, 0) @@ -38,24 +36,19 @@ namespace CppSharp.Types { public Type Type { get; set; } public ASTContext ASTContext { get; set; } + public DriverOptions Options { get; set; } public ITypeMapDatabase TypeMapDatabase { get; set; } public bool IsEnabled { get; set; } = true; - public virtual bool IsIgnored - { - get { return false; } - } + public virtual bool IsIgnored => false; - public virtual bool IsValueType - { - get { return false; } - } + public virtual bool IsValueType => false; /// /// Determines if the type map performs marshalling or only injects custom code. /// - public virtual bool DoesMarshalling { get { return true; } } + public virtual bool DoesMarshalling => true; #region C# backend diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index 5c6937b2..2100984d 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using CppSharp.AST; using CppSharp.AST.Extensions; -using CppSharp.Generators; using Type = CppSharp.AST.Type; namespace CppSharp.Types @@ -11,15 +10,18 @@ namespace CppSharp.Types { public IDictionary TypeMaps { get; set; } - public TypeMapDatabase(ASTContext astContext, GeneratorKind generatorKind) + public DriverOptions Options { get; } + + public TypeMapDatabase(ASTContext astContext, DriverOptions options) { TypeMaps = new Dictionary(); + this.Options = options; foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { try { var types = assembly.FindDerivedTypes(typeof(TypeMap)); - SetupTypeMaps(types, generatorKind, astContext); + SetupTypeMaps(types, astContext); } catch (System.Reflection.ReflectionTypeLoadException ex) { @@ -29,18 +31,18 @@ namespace CppSharp.Types } } - private void SetupTypeMaps(IEnumerable types, - GeneratorKind generatorKind, ASTContext astContext) + private void SetupTypeMaps(IEnumerable types, ASTContext astContext) { foreach (var type in types) { var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true); foreach (TypeMapAttribute attr in attrs) { - if (attr.GeneratorKind == 0 || attr.GeneratorKind == generatorKind) + if (attr.GeneratorKind == 0 || attr.GeneratorKind == Options.GeneratorKind) { var typeMap = (TypeMap) Activator.CreateInstance(type); typeMap.ASTContext = astContext; + typeMap.Options = Options; typeMap.TypeMapDatabase = this; this.TypeMaps[attr.Type] = typeMap; } @@ -108,10 +110,13 @@ namespace CppSharp.Types return typeMap.IsEnabled; } + Type desugared = type.Desugar(); + bool printExtra = desugared.GetPointee() != null && + desugared.GetFinalPointee().Desugar().IsPrimitiveType(); var typePrinter = new CppTypePrinter { - PrintTypeQualifiers = false, - PrintTypeModifiers = false, + PrintTypeQualifiers = printExtra, + PrintTypeModifiers = printExtra, PrintLogicalNames = true };