Browse Source

TypeMap: refactor C++ backend into common methods

pull/1802/head
Deadlocklogic 2 years ago
parent
commit
8271415e8a
  1. 14
      src/Generator/Generators/C/CppMarshal.cs
  2. 2
      src/Generator/Generators/C/CppTypePrinter.cs
  3. 12
      src/Generator/Generators/NAPI/NAPIMarshal.cs
  4. 30
      src/Generator/Types/TypeMap.cs
  5. 7
      tests/dotnet/CLI/CLI.Gen.cs

14
src/Generator/Generators/C/CppMarshal.cs

@ -25,7 +25,7 @@ namespace CppSharp.Generators.Cpp @@ -25,7 +25,7 @@ namespace CppSharp.Generators.Cpp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -173,7 +173,7 @@ namespace CppSharp.Generators.Cpp @@ -173,7 +173,7 @@ namespace CppSharp.Generators.Cpp
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -193,7 +193,7 @@ namespace CppSharp.Generators.Cpp @@ -193,7 +193,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -341,7 +341,7 @@ namespace CppSharp.Generators.Cpp @@ -341,7 +341,7 @@ namespace CppSharp.Generators.Cpp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -478,7 +478,7 @@ namespace CppSharp.Generators.Cpp @@ -478,7 +478,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -516,7 +516,7 @@ namespace CppSharp.Generators.Cpp @@ -516,7 +516,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}
@ -563,7 +563,7 @@ namespace CppSharp.Generators.Cpp @@ -563,7 +563,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return;
}

2
src/Generator/Generators/C/CppTypePrinter.cs

@ -68,7 +68,7 @@ namespace CppSharp.Generators.C @@ -68,7 +68,7 @@ namespace CppSharp.Generators.C
typePrinter.PushContext(ContextKind);
typePrinter.PushScope(ScopeKind);
var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter);
var typeName = typeMap.SignatureType(typePrinterContext).Visit(typePrinter);
result = new TypePrinterResult(typeName) { TypeMap = typeMap };
return true;

12
src/Generator/Generators/NAPI/NAPIMarshal.cs

@ -26,7 +26,7 @@ namespace CppSharp.Generators.NAPI @@ -26,7 +26,7 @@ namespace CppSharp.Generators.NAPI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -194,7 +194,7 @@ namespace CppSharp.Generators.NAPI @@ -194,7 +194,7 @@ namespace CppSharp.Generators.NAPI
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -214,7 +214,7 @@ namespace CppSharp.Generators.NAPI @@ -214,7 +214,7 @@ namespace CppSharp.Generators.NAPI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -343,7 +343,7 @@ namespace CppSharp.Generators.NAPI @@ -343,7 +343,7 @@ namespace CppSharp.Generators.NAPI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -591,7 +591,7 @@ namespace CppSharp.Generators.NAPI @@ -591,7 +591,7 @@ namespace CppSharp.Generators.NAPI
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -628,7 +628,7 @@ namespace CppSharp.Generators.NAPI @@ -628,7 +628,7 @@ namespace CppSharp.Generators.NAPI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}

30
src/Generator/Types/TypeMap.cs

@ -58,7 +58,7 @@ namespace CppSharp.Types @@ -58,7 +58,7 @@ namespace CppSharp.Types
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return CppSignatureType(ctx);
return new CILType(typeof(object));
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return CLISignatureType(ctx);
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
@ -75,7 +75,7 @@ namespace CppSharp.Types @@ -75,7 +75,7 @@ namespace CppSharp.Types
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToNative(ctx);
ctx.Return.Write(ctx.Parameter.Name);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToNative(ctx);
@ -95,7 +95,7 @@ namespace CppSharp.Types @@ -95,7 +95,7 @@ namespace CppSharp.Types
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToManaged(ctx);
ctx.Return.Write(ctx.ReturnVarName);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToManaged(ctx);
@ -158,30 +158,6 @@ namespace CppSharp.Types @@ -158,30 +158,6 @@ namespace CppSharp.Types
}
#endregion
#region C++ backend
public virtual Type CppSignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(object));
}
public virtual void CppTypeReference(CLITypeReference collector, ASTRecord<Declaration> record)
{
throw new NotImplementedException();
}
public virtual void CppMarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public virtual void CppMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
#endregion
}
public interface ITypeMapDatabase

7
tests/dotnet/CLI/CLI.Gen.cs

@ -29,13 +29,6 @@ namespace CppSharp.Tests @@ -29,13 +29,6 @@ namespace CppSharp.Tests
return new CILType(typeof(string));
}
public override Type CppSignatureType(TypePrinterContext ctx)
{
var tagType = ctx.Type as TagType;
var typePrinter = new CppTypePrinter(Context);
return new CustomType(tagType.Declaration.Visit(typePrinter));
}
public override void CLIMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);

Loading…
Cancel
Save