Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
TypeMap: refactor CSharp backend into common methods + migration
  • Loading branch information
deadlocklogic committed Dec 5, 2023
1 parent cb764e8 commit 6d434ec
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 160 deletions.
2 changes: 1 addition & 1 deletion src/Generator/AST/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() ||
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/Generator/Generators/CSharp/CSharpTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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()
{
Expand All @@ -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);
}
Expand All @@ -183,15 +183,15 @@ 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,
MarshalKind = MarshalKind,
Type = pointer.Pointee,
Parameter = Parameter
};
return typeMap.CSharpSignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}

var pointee = pointer.Pointee.Desugar();
Expand Down Expand Up @@ -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;

Expand All @@ -270,7 +270,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
Parameter = Parameter
};

return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

FunctionType func;
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -330,7 +330,7 @@ public override TypePrinterResult VisitTemplateSpecializationType(
MarshalKind = MarshalKind
};

return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}

public override TypePrinterResult VisitDependentTemplateSpecializationType(
Expand Down
7 changes: 2 additions & 5 deletions src/Generator/Generators/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Generator/Passes/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
{
Expand All @@ -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;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Passes/ValidateOperatorsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
35 changes: 16 additions & 19 deletions src/Generator/Types/Std/Stdlib.CLI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<clix::{
param}>({ctx.ReturnVarName}))");
$@"({ctx.ReturnVarName} == 0 ? nullptr : clix::marshalString<clix::{param}>({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<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class String : TypeMap
public class String : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
Expand All @@ -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)
{
Expand All @@ -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
{
Expand Down Expand Up @@ -259,16 +257,15 @@ 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; } }

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)
Expand All @@ -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<char, char_traits<char>>", GeneratorKind.CLI_ID)]
public partial class OStream : TypeMap
public class OStream : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
Expand All @@ -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; } }

Expand Down
Loading

0 comments on commit 6d434ec

Please sign in to comment.