Browse Source

TypeMap: refactor CLI backend into common methods

pull/1802/head
Deadlocklogic 2 years ago
parent
commit
e667b2cc7b
  1. 14
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 8
      src/Generator/Generators/CLI/CLITypePrinter.cs
  3. 2
      src/Generator/Generators/ExtensionMethods.cs
  4. 45
      src/Generator/Types/Std/Stdlib.CLI.cs
  5. 49
      src/Generator/Types/TypeMap.cs
  6. 14
      tests/dotnet/CLI/CLI.Gen.cs
  7. 16
      tests/dotnet/Common/Common.Gen.cs

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

@ -22,7 +22,7 @@ namespace CppSharp.Generators.CLI @@ -22,7 +22,7 @@ namespace CppSharp.Generators.CLI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -215,7 +215,7 @@ namespace CppSharp.Generators.CLI @@ -215,7 +215,7 @@ namespace CppSharp.Generators.CLI
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -240,7 +240,7 @@ namespace CppSharp.Generators.CLI @@ -240,7 +240,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -406,7 +406,7 @@ namespace CppSharp.Generators.CLI @@ -406,7 +406,7 @@ namespace CppSharp.Generators.CLI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -605,7 +605,7 @@ namespace CppSharp.Generators.CLI @@ -605,7 +605,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -641,7 +641,7 @@ namespace CppSharp.Generators.CLI @@ -641,7 +641,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}
@ -688,7 +688,7 @@ namespace CppSharp.Generators.CLI @@ -688,7 +688,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return;
}

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

@ -21,7 +21,7 @@ namespace CppSharp.Generators.CLI @@ -21,7 +21,7 @@ namespace CppSharp.Generators.CLI
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
{
var typePrinterContext = new TypePrinterContext { Type = tag };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
Declaration decl = tag.Declaration;
@ -112,7 +112,7 @@ namespace CppSharp.Generators.CLI @@ -112,7 +112,7 @@ namespace CppSharp.Generators.CLI
Type = pointer
};
return typeMap.CLISignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}
var pointee = pointer.Pointee.Desugar();
@ -217,7 +217,7 @@ namespace CppSharp.Generators.CLI @@ -217,7 +217,7 @@ namespace CppSharp.Generators.CLI
{
typeMap.Type = typedef;
var typePrinterContext = new TypePrinterContext { Type = typedef };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
FunctionType func;
@ -241,7 +241,7 @@ namespace CppSharp.Generators.CLI @@ -241,7 +241,7 @@ namespace CppSharp.Generators.CLI
if (TypeMapDatabase.FindTypeMap(template, out typeMap) && !typeMap.IsIgnored)
{
var typePrinterContext = new TypePrinterContext { Type = template };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
return decl.Name;

2
src/Generator/Generators/ExtensionMethods.cs

@ -66,7 +66,7 @@ namespace CppSharp.Generators @@ -66,7 +66,7 @@ namespace CppSharp.Generators
switch (generatorKind)
{
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI):
return typeMap.CLISignatureType(typePrinterContext).Desugar();
return typeMap.SignatureType(typePrinterContext).Desugar();
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp):
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
}

45
src/Generator/Types/Std/Stdlib.CLI.cs

@ -12,12 +12,12 @@ namespace CppSharp.Types.Std @@ -12,12 +12,12 @@ namespace CppSharp.Types.Std
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstCharPointer : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
ctx.Before.WriteLine(
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
@ -26,7 +26,7 @@ namespace CppSharp.Types.Std @@ -26,7 +26,7 @@ namespace CppSharp.Types.Std
ctx.Return.Write("_{0}.c_str()", ctx.ArgName);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
!ctx.Parameter.IsInOut)
@ -85,18 +85,18 @@ namespace CppSharp.Types.Std @@ -85,18 +85,18 @@ namespace CppSharp.Types.Std
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class String : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
ctx.Parameter.Name);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
ctx.ReturnVarName);
@ -106,18 +106,18 @@ namespace CppSharp.Types.Std @@ -106,18 +106,18 @@ namespace CppSharp.Types.Std
[TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class WString : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
ctx.Parameter.Name);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
ctx.ReturnVarName);
@ -145,13 +145,13 @@ namespace CppSharp.Types.Std @@ -145,13 +145,13 @@ namespace CppSharp.Types.Std
}
}
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CustomType(
$"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^");
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
var desugared = Type.Desugar();
var templateType = desugared as TemplateSpecializationType;
@ -209,7 +209,7 @@ namespace CppSharp.Types.Std @@ -209,7 +209,7 @@ namespace CppSharp.Types.Std
ctx.Return.Write(tmpVarName);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
var desugared = Type.Desugar();
var templateType = desugared as TemplateSpecializationType;
@ -263,7 +263,7 @@ namespace CppSharp.Types.Std @@ -263,7 +263,7 @@ namespace CppSharp.Types.Std
{
public override bool IsIgnored { get { return true; } }
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
var type = Type as TemplateSpecializationType;
return new CustomType(
@ -271,26 +271,15 @@ namespace CppSharp.Types.Std @@ -271,26 +271,15 @@ namespace CppSharp.Types.Std
type.Arguments[0].Type}, {type.Arguments[1].Type}>^");
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
throw new System.NotImplementedException();
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
throw new System.NotImplementedException();
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Native)
return new CustomType("Std.Map");
var type = Type as TemplateSpecializationType;
return new CustomType(
$@"System.Collections.Generic.Dictionary<{
type.Arguments[0].Type}, {type.Arguments[1].Type}>");
}
}
[TypeMap("std::list", GeneratorKindID = GeneratorKind.CLI_ID)]
@ -308,12 +297,12 @@ namespace CppSharp.Types.Std @@ -308,12 +297,12 @@ namespace CppSharp.Types.Std
[TypeMap("basic_ostream<char, char_traits<char>>", GeneratorKind.CLI_ID)]
public partial class OStream : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CILType(typeof(System.IO.TextWriter));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative;
if (!ctx.Parameter.Type.Desugar().IsPointer())

49
src/Generator/Types/TypeMap.cs

@ -51,16 +51,19 @@ namespace CppSharp.Types @@ -51,16 +51,19 @@ namespace CppSharp.Types
/// </summary>
public virtual bool DoesMarshalling => true;
public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind = null)
public Type SignatureType(TypePrinterContext ctx)
{
return SignatureType(ctx, Context.Options.GeneratorKind);
}
public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
kind ??= Context.Options.GeneratorKind;
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return new CILType(typeof(object));
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return CLISignatureType(ctx);
return new CILType(typeof(object));
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
return CSharpSignatureType(ctx);
default:
@ -68,17 +71,20 @@ namespace CppSharp.Types @@ -68,17 +71,20 @@ namespace CppSharp.Types
}
}
public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = null)
public void MarshalToNative(MarshalContext ctx)
{
MarshalToNative(ctx, Context.Options.GeneratorKind);
}
public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
kind ??= Context.Options.GeneratorKind;
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
ctx.Return.Write(ctx.Parameter.Name);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToNative(ctx);
ctx.Return.Write(ctx.Parameter.Name);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToNative(ctx as CSharpMarshalContext);
@ -88,17 +94,19 @@ namespace CppSharp.Types @@ -88,17 +94,19 @@ namespace CppSharp.Types
}
}
public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind = null)
public void MarshalToManaged(MarshalContext ctx)
{
MarshalToManaged(ctx, Context.Options.GeneratorKind);
}
public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
kind ??= Context.Options.GeneratorKind;
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
ctx.Return.Write(ctx.ReturnVarName);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToManaged(ctx);
ctx.Return.Write(ctx.ReturnVarName);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToManaged(ctx as CSharpMarshalContext);
@ -138,25 +146,10 @@ namespace CppSharp.Types @@ -138,25 +146,10 @@ namespace CppSharp.Types
#region C++/CLI backend
public virtual Type CLISignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(object));
}
public virtual void CLITypeReference(CLITypeReferenceCollector collector, ASTRecord<Declaration> 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
}

14
tests/dotnet/CLI/CLI.Gen.cs

@ -7,34 +7,34 @@ using CppSharp.Utils; @@ -7,34 +7,34 @@ using CppSharp.Utils;
namespace CppSharp.Tests
{
[TypeMap("IgnoredClassTemplateForEmployee")]
[TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)]
public class IgnoredClassTemplateForEmployeeMap : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CustomType("CLI::Employee^");
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)");
}
}
[TypeMap("TestMappedTypeNonConstRefParam")]
[TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)]
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
if (ctx.Parameter.Usage == ParameterUsage.InOut)
{

16
tests/dotnet/Common/Common.Gen.cs

@ -7,24 +7,28 @@ using CppSharp.Utils; @@ -7,24 +7,28 @@ using CppSharp.Utils;
namespace CppSharp.Tests
{
[TypeMap("TypeMappedIndex")]
public class TypeMappedIndex : TypeMap
[TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)]
public class CLITypeMappedIndex : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
{
return new BuiltinType(PrimitiveType.UShort);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write(ctx.ReturnVarName);
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
{
ctx.Return.Write("::TypeMappedIndex()");
ctx.Return.Write("::TypeMappedIndex1()");
}
}
[TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class TypeMappedIndex : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
{
return new BuiltinType(PrimitiveType.UShort);

Loading…
Cancel
Save