Skip to content

Commit

Permalink
TypeMap: cleanup patches from previous commits
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlocklogic committed Dec 5, 2023
1 parent 15ecf3b commit 64c3b24
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 117 deletions.
34 changes: 17 additions & 17 deletions src/Generator/Types/Std/Stdlib.CLI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace CppSharp.Types.Std.CLI
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)]
public class ConstCharPointer : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Before.WriteLine(
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
Expand All @@ -25,7 +25,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
ctx.Return.Write("_{0}.c_str()", ctx.ArgName);
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
!ctx.Parameter.IsInOut)
Expand Down Expand Up @@ -83,18 +83,18 @@ public class ConstChar32TPointer : ConstCharPointer
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CLI_ID)]
public class String : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
ctx.Parameter.Name);
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
ctx.ReturnVarName);
Expand All @@ -104,18 +104,18 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
[TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)]
public class WString : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
ctx.Parameter.Name);
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
ctx.ReturnVarName);
Expand Down Expand Up @@ -143,13 +143,13 @@ public override bool IsIgnored
}
}

public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CustomType(
$"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^");
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
var desugared = Type.Desugar();
var templateType = desugared as TemplateSpecializationType;
Expand Down Expand Up @@ -207,7 +207,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
ctx.Return.Write(tmpVarName);
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
var desugared = Type.Desugar();
var templateType = desugared as TemplateSpecializationType;
Expand Down Expand Up @@ -261,19 +261,19 @@ public class Map : TypeMap
{
public override bool IsIgnored { get { return true; } }

public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
var type = Type as TemplateSpecializationType;
return new CustomType(
$@"::System::Collections::Generic::Dictionary<{type.Arguments[0].Type}, {type.Arguments[1].Type}>^");
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
throw new System.NotImplementedException();
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
throw new System.NotImplementedException();
}
Expand All @@ -294,12 +294,12 @@ public class SharedPtr : TypeMap
[TypeMap("basic_ostream<char, char_traits<char>>", GeneratorKind.CLI_ID)]
public class OStream : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(System.IO.TextWriter));
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative;
if (!ctx.Parameter.Type.Desugar().IsPointer())
Expand Down
32 changes: 16 additions & 16 deletions src/Generator/Types/Std/Stdlib.CSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ namespace CppSharp.Types.Std.CSharp
[TypeMap("int", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class Int : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetSignedType(Context.TargetInfo.IntWidth);
}

[TypeMap("unsigned int", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class UnsignedInt : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.IntWidth);
}

[TypeMap("long", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class Long : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetSignedType(Context.TargetInfo.LongWidth);
}

[TypeMap("unsigned long", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class UnsignedLong : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.LongWidth);
}

[TypeMap("char", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class Char : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(ctx.Kind == TypePrinterContextKind.Native ||
!Context.Options.MarshalCharAsManagedChar ? typeof(sbyte) : typeof(char));
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
if (Context.Options.MarshalCharAsManagedChar)
ctx.Return.Write("global::System.Convert.ToSByte({0})",
Expand All @@ -57,7 +57,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
ctx.Return.Write(ctx.Parameter.Name);
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
if (Context.Options.MarshalCharAsManagedChar)
ctx.Return.Write("global::System.Convert.ToChar({0})",
Expand All @@ -70,7 +70,7 @@ public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
[TypeMap("char16_t", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class Char16T : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(char));
}
Expand All @@ -79,7 +79,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
[TypeMap("wchar_t", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class WCharT : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(char));
}
Expand All @@ -88,7 +88,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class ConstCharPointer : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Managed)
return new CILType(typeof(string));
Expand Down Expand Up @@ -117,7 +117,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
$"{Context.Options.Encoding.EncodingName} is not supported yet.");
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
string param = ctx.Parameter.Name;
if (ctx.Parameter.Usage == ParameterUsage.Unknown &&
Expand Down Expand Up @@ -194,7 +194,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
ctx.Return.Write($"{bytePtr}");
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
!ctx.Parameter.IsInOut)
Expand Down Expand Up @@ -304,7 +304,7 @@ public class ConstChar32TPointer : ConstCharPointer
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class String : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Managed)
return new CILType(typeof(string));
Expand All @@ -316,7 +316,7 @@ public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
return new CustomType(basicString.Visit(typePrinter).Type);
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
Type type = ctx.Parameter.Type.Desugar();
ClassTemplateSpecialization basicString = GetBasicString(type);
Expand Down Expand Up @@ -361,7 +361,7 @@ public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
}
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
var type = Type.Desugar(resolveTemplateSubstitution: false);
ClassTemplateSpecialization basicString = GetBasicString(type);
Expand Down Expand Up @@ -415,7 +415,7 @@ private static ClassTemplateSpecialization GetBasicString(Type type)
[TypeMap("FILE", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class FILE : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(System.IntPtr));
}
Expand Down
60 changes: 6 additions & 54 deletions src/Generator/Types/TypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.AST;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.Cpp;
using CppSharp.Generators.CSharp;
using Attribute = System.Attribute;
using Type = CppSharp.AST.Type;

Expand Down Expand Up @@ -51,64 +48,19 @@ public class TypeMap
/// </summary>
public virtual bool DoesMarshalling => true;

public Type SignatureType(TypePrinterContext ctx)
public virtual Type SignatureType(TypePrinterContext ctx)
{
return SignatureType(ctx, Context.Options.GeneratorKind);
return new CILType(typeof(object));
}

public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public virtual void MarshalToNative(MarshalContext ctx)
{
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
return new CILType(typeof(object));
default:
throw new System.NotImplementedException();
}
ctx.Return.Write(ctx.Parameter.Name);
}

public void MarshalToNative(MarshalContext ctx)
public virtual void MarshalToManaged(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):
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
ctx.Return.Write(ctx.Parameter.Name);
return;
default:
throw new System.NotImplementedException();
}
}

public void MarshalToManaged(MarshalContext ctx)
{
MarshalToManaged(ctx, Context.Options.GeneratorKind);
}

public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
{
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
ctx.Return.Write(ctx.ReturnVarName);
return;
default:
throw new System.NotImplementedException();
}
ctx.Return.Write(ctx.ReturnVarName);
}

#region C# backend
Expand Down
12 changes: 6 additions & 6 deletions tests/dotnet/CLI/CLI.Gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace CppSharp.Tests
[TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)]
public class IgnoredClassTemplateForEmployeeMap : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CustomType("CLI::Employee^");
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)");
}
Expand All @@ -25,17 +25,17 @@ namespace CLI
[TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)]
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}

public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);
}

public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
public override void MarshalToNative(MarshalContext ctx)
{
if (ctx.Parameter.Usage == ParameterUsage.InOut)
{
Expand All @@ -57,7 +57,7 @@ namespace Cpp
[TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CPlusPlus_ID)]
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
{
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
public override Type SignatureType(TypePrinterContext ctx)
{
var tagType = ctx.Type as TagType;
var typePrinter = new CppTypePrinter(Context);
Expand Down
Loading

0 comments on commit 64c3b24

Please sign in to comment.