Browse Source

Added a type map for char.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1148/head
Dimitar Dobrev 7 years ago
parent
commit
43bb2ee4bb
  1. 2
      src/AST/CppTypePrinter.cs
  2. 2
      src/Generator/Driver.cs
  3. 3
      src/Generator/Generators/CLI/CLIMarshal.cs
  4. 2
      src/Generator/Generators/CLI/CLITypePrinter.cs
  5. 20
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  6. 9
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  7. 66
      src/Generator/Types/Std/Stdlib.cs
  8. 17
      src/Generator/Types/TypeMap.cs
  9. 21
      src/Generator/Types/TypeMapDatabase.cs

2
src/AST/CppTypePrinter.cs

@ -65,7 +65,7 @@ namespace CppSharp.AST @@ -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 "&&";

2
src/Generator/Driver.cs

@ -73,7 +73,7 @@ namespace CppSharp @@ -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<string> files, ParserResult result)
{

3
src/Generator/Generators/CLI/CLIMarshal.cs

@ -3,7 +3,6 @@ using System.Linq; @@ -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 @@ -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 @@ -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;
}

2
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -25,7 +25,6 @@ namespace CppSharp.Generators.CLI @@ -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 @@ -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();
}

20
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -53,7 +53,6 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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 @@ -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 @@ -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)
{

9
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -106,13 +106,8 @@ namespace CppSharp.Passes @@ -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)

66
src/Generator/Types/Std/Stdlib.cs

@ -15,6 +15,72 @@ namespace CppSharp.Types.Std @@ -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<char, char_traits<char>, allocator<char>>")]
public class String : TypeMap
{

17
src/Generator/Types/TypeMap.cs

@ -1,7 +1,5 @@ @@ -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 @@ -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 @@ -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;
/// <summary>
/// Determines if the type map performs marshalling or only injects custom code.
/// </summary>
public virtual bool DoesMarshalling { get { return true; } }
public virtual bool DoesMarshalling => true;
#region C# backend

21
src/Generator/Types/TypeMapDatabase.cs

@ -2,7 +2,6 @@ using System; @@ -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 @@ -11,15 +10,18 @@ namespace CppSharp.Types
{
public IDictionary<string, TypeMap> TypeMaps { get; set; }
public TypeMapDatabase(ASTContext astContext, GeneratorKind generatorKind)
public DriverOptions Options { get; }
public TypeMapDatabase(ASTContext astContext, DriverOptions options)
{
TypeMaps = new Dictionary<string, TypeMap>();
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 @@ -29,18 +31,18 @@ namespace CppSharp.Types
}
}
private void SetupTypeMaps(IEnumerable<System.Type> types,
GeneratorKind generatorKind, ASTContext astContext)
private void SetupTypeMaps(IEnumerable<System.Type> 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 @@ -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
};

Loading…
Cancel
Save