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

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

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

2
src/Generator/Generators/ExtensionMethods.cs

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

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

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

49
src/Generator/Types/TypeMap.cs

@ -51,16 +51,19 @@ namespace CppSharp.Types
/// </summary> /// </summary>
public virtual bool DoesMarshalling => true; 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) switch (kind)
{ {
case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return new CILType(typeof(object));
case var _ when ReferenceEquals(kind, GeneratorKind.CLI): case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return CLISignatureType(ctx); return new CILType(typeof(object));
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
return CSharpSignatureType(ctx); return CSharpSignatureType(ctx);
default: default:
@ -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; kind ??= Context.Options.GeneratorKind;
switch (kind) switch (kind)
{ {
case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
ctx.Return.Write(ctx.Parameter.Name);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI): case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToNative(ctx); ctx.Return.Write(ctx.Parameter.Name);
return; return;
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToNative(ctx as CSharpMarshalContext); CSharpMarshalToNative(ctx as CSharpMarshalContext);
@ -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) switch (kind)
{ {
case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
ctx.Return.Write(ctx.ReturnVarName);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI): case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToManaged(ctx); ctx.Return.Write(ctx.ReturnVarName);
return; return;
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToManaged(ctx as CSharpMarshalContext); CSharpMarshalToManaged(ctx as CSharpMarshalContext);
@ -138,25 +146,10 @@ namespace CppSharp.Types
#region C++/CLI backend #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 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 #endregion
} }

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

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

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

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

Loading…
Cancel
Save